【问题标题】:RedBlackTrees Structure?红黑树结构?
【发布时间】:2012-06-03 17:37:56
【问题描述】:

基于此实现 http://pastebin.com/Gg3YbPUg,我无法理解 redbacktrees。

/**
 * Insert into the tree.
 * @param item the item to insert.
 * @throws DuplicateItemException if item is already present.
 */
public void insert( AnyType item )
{
    current = parent = grand = header;
    nullNode.element = item;

    while( compare( item, current ) != 0 )
    {
        great = grand; grand = parent; parent = current;
        current = compare( item, current ) < 0 ?
                     current.left : current.right;

            // Check if two red children; fix if so
        if( current.left.color == RED && current.right.color == RED )
             handleReorient( item );
    }

        // Insertion fails if already present
    if( current != nullNode )
        throw new DuplicateItemException( item.toString( ) );
    current = new RedBlackNode<AnyType>( item, nullNode, nullNode );

        // Attach to parent
    if( compare( item, parent ) < 0 )
        parent.left = current;
    else
        parent.right = current;
    handleReorient( item );
}

我刚刚用数字 1,14,15,2,11,7,5,8 制作了一棵简单的树

// Test program
public static void main( String [ ] args )
{
    RedBlackTree<Integer> t = new RedBlackTree<Integer>( );

    t.insert(1);
    t.insert(14);
    t.insert(15);
    t.insert(2);
    t.insert(11);
    t.insert(7);
    t.insert(5);
    t.insert(8);

    t.printTree();
}

根据http://www.cs.auckland.ac.nz/software/AlgAnim/red_black.html 的示例,我的树应该看起来像...

还有我的 printTree 函数的控制台输出

/**
 * Print all items.
 */
public void printTree( )
{
    printTree( header.right );
}

/**
 * Internal method to print a subtree in sorted order.
 * @param t the node that roots the tree.
 */
private void printTree( RedBlackNode<AnyType> t )
{
    if( t != nullNode )
    {
        printTree( t.left );
        System.out.println("main element is <" + t.element + "("+t.color+")> left <" + t.left.element +"> right <" + t.right.element + ">" );
        printTree( t.right );
    }
}

是……

main element is <1(COLOR:RED)> left <8> right <8>
main element is <2(COLOR:BLACK)> left <1> right <5>
main element is <5(COLOR:RED)> left <8> right <8>
main element is <7(COLOR:RED)> left <2> right <14>
main element is <8(COLOR:BLACK)> left <8> right <8>
main element is <11(COLOR:RED)> left <8> right <8>
main element is <14(COLOR:BLACK)> left <11> right <15>
main element is <15(COLOR:RED)> left <8> right <8>

(出于阅读目的,我将 0 和 1 重命名为 COLOR RED AND BLACK) 据我了解,“标题”变量是树的根。

如果我要做类似的事情

 public void function(AnyType x)
 {
    RedBlackNode<AnyType> n = find(x);
    //what is the parent of n?
 }

/**
 * Find an item in the tree.
 * @param x the item to search for.
 * @return the matching item or null if not found.
 */
public RedBlackNode<AnyType> find( AnyType x )
{
    nullNode.element = x;
    current = header.right;

    for( ; ; )
    {
        if( x.compareTo( current.element ) < 0 )
            current = current.left;
        else if( x.compareTo( current.element ) > 0 ) 
            current = current.right;
        else if( current != nullNode )
            return current;
        else
            return null;
    }
}

从上面 RedBlackTree 的 pastebin 实现中,我如何知道我当前的父元素对于 n 是什么?

【问题讨论】:

  • 如果结构不正确,则问题出在您的insert(...) 方法中。请把它贴在上面,以便我们查看。
  • @HunterMcMillen 嘿猎人,一切都在提供的 pastebin 网址中。

标签: java red-black-tree


【解决方案1】:

如果是合适的红黑树,遵守规则,那么根必须是黑色的,所以1不可能是根。

我已经用 Java 构建了一个红黑树,如果您有兴趣,它的打印效果会更好:

└── (Black) 8
    ├── (Black) 2
    │   ├── (Red) 1
    │   │   ├── (Black) null
    │   │   └── (Black) null
    │   └── (Red) 5
    │       ├── (Black) null
    │       └── (Black) null
    └── (Red) 15
        ├── (Black) 11
        │   ├── (Black) null
        │   └── (Red) 14
        │       ├── (Black) null
        │       └── (Black) null
        └── (Black) 17
            ├── (Black) null
            └── (Black) null

Here 是红黑树的代码,查看 RedBlackTreePrinter 类以了解我是如何构建控制台输出的。

【讨论】:

  • 如果您可以将其附加到当前问题的末尾...我将不胜感激“从 RedBlackTree 的上述 pastebin 实现中,我如何知道我当前的父元素对于 n ?” (或者我只是花一些时间阅读您的代码)+1 代表也为您的答案。
  • @Killrawr 我不知道您是否注意到,但我通过我的 RedBlack 树代码运行了您的输入“1,14,15,2,11,7,5,8”以获得该输出。您放置的图形上的输出对我来说看起来不对,11->14->15 看起来不对。
  • @Killrawr 看起来如果不跟踪向下的信息,就无法获取给定 n 的父节点。本质上,如果您在树中搜索节点“n”,请跟踪前一个节点“p”。当您在树中找到“n”时,“p”应该是它的父级。这就是我在实现中明确使用父对象的原因。此外,查看代码,标题看起来是树的根。
  • @Justin Aw 好的,这很酷,谢谢!无论如何,很多精彩的答案:)和建议
猜你喜欢
  • 2011-02-04
  • 2010-09-06
  • 2011-01-31
  • 1970-01-01
  • 2012-02-04
  • 2018-10-10
  • 2011-03-11
  • 2011-08-23
  • 2011-08-03
相关资源
最近更新 更多