Java 红黑树结构?
基于此实现,我无法设法绕过redbacktreesJava 红黑树结构?,java,red-black-tree,Java,Red Black Tree,基于此实现,我无法设法绕过redbacktrees /** *插入到树中。 *@param item要插入的项。 *@如果项目已存在,则引发DuplicateItemException。 */ 公共作废插入(任意类型项) { 当前=父级=总=标题; nullNode.element=项; while(比较(项目,当前)!=0) { great=grand;grand=parent;parent=current; 当前=比较(项目,当前)14->15就是不正确。@Killrawr看起来,如果在向下
/**
*插入到树中。
*@param item要插入的项。
*@如果项目已存在,则引发DuplicateItemException。
*/
公共作废插入(任意类型项)
{
当前=父级=总=标题;
nullNode.element=项;
while(比较(项目,当前)!=0)
{
great=grand;grand=parent;parent=current;
当前=比较(项目,当前)<0?
current.left:current.right;
//检查是否有两个红色子项;如果是,请修复
if(current.left.color==RED&¤t.right.color==RED)
搬运方向(项目);
}
//如果已经存在,则插入失败
if(当前!=nullNode)
抛出新的DuplicateItemException(item.toString());
当前=新的RedBlackNode,我的树应该看起来像
以及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 );
}
}
/**
*打印所有项目。
*/
公共void打印树()
{
打印树(header.right);
}
/**
*按排序顺序打印子树的内部方法。
*@param t树的根节点。
*/
私有void打印树(RedBlackNode t)
{
if(t!=nullNode)
{
printree(t.left);
System.out.println(“主元素为左-右”);
printree(t.right);
}
}
是
主元素为左-右
主要元素是左-右
主要元素是左-右
主要元素是左-右
主要元素是左-右
主要元素是左-右
主要元素是左-右
主要元素是左-右
(出于阅读目的,我已将0和1重命名为红色和黑色)
据我所知,“header”变量是树的根
如果我要做这样的事
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;
}
}
public void函数(AnyType x)
{
RedBlackNode n=find(x);
//n的父代是什么?
}
/**
*在树中查找项目。
*@param x要搜索的项目。
*@返回匹配项,如果未找到,则返回null。
*/
公共RedBlackNode查找(AnyType x)
{
nullNode.element=x;
当前=标题。右侧;
对于(;;)
{
如果(x.compareTo(current.element)<0)
current=current.left;
如果(x.compareTo(current.element)>0,则为else
current=current.right;
else if(当前!=nullNode)
回流;
其他的
返回null;
}
}
从上述RedBlackTree的pastebin实现中,我如何知道我当前的父元素对于n是什么?如果它是正确的红黑树,遵守规则,那么根必须是黑色的,因此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
是红黑树的代码,请查看RedBlackTreePrinter类以了解我是如何构建控制台输出的。如果结构不正确,则问题必须出在您的插入(…)
method。请将其张贴在上面,以便我们可以查看。@HunterMcMillen嘿hunter,所有内容都在提供的pastebin url中。如果您能将其附加到当前问题的末尾……我将非常感谢“从上述RedBlackTree的pastebin实现中,我如何知道我当前的父元素是用于n的?”(或者我会花点时间阅读你的代码)+1次重复回答。@Killrawr我不知道你是否注意到了,但我运行了你的输入“1,14,15,2,11,7,5,8”通过我的红黑树代码来获得输出。您在图形上的输出在我看来并不正确,11->14->15就是不正确。@Killrawr看起来,如果在向下搜索时不跟踪该信息,就无法获得给定的父节点n。基本上,如果您在树中搜索节点“n”,跟踪上一个节点“p”。当您在树中找到“n”时,“p”应该是它的父对象。这就是为什么我在实现中显式使用父对象的原因。另外,查看代码,header看起来是树的根。@Justin Aw好的,很酷,谢谢!非常感谢您的精彩回答:)和建议
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>
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;
}
}
└── (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