Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
红黑树节点插入的不寻常Java实现_Java_Red Black Tree_Red Black Tree Insertion - Fatal编程技术网

红黑树节点插入的不寻常Java实现

红黑树节点插入的不寻常Java实现,java,red-black-tree,red-black-tree-insertion,Java,Red Black Tree,Red Black Tree Insertion,我正在用Java编写一个关于红/黑树的类程序。我已经很好地理解了它们通常是如何工作的,并且应该使用递归插入方法。我通常会使用下面的内容来匹配我教授的节点类。就颜色而言,0是黑色,1是红色。提供给我们的节点类根本不处理键 private static void put(int val, int col) { root = put(root, val, col); } private static Node put(Node n, Integer val, int col) { if (

我正在用Java编写一个关于红/黑树的类程序。我已经很好地理解了它们通常是如何工作的,并且应该使用递归插入方法。我通常会使用下面的内容来匹配我教授的节点类。就颜色而言,0是黑色,1是红色。提供给我们的节点类根本不处理键

private static void put(int val,  int col)
{ root = put(root, val, col); }

private static Node put(Node n, Integer val, int col)
{
    if (n == null){
        Node t=new Node(val);
        t.setColor(1);
        return t;
    }
    int cmp = val.compareTo(n.getValue());

    if (cmp < 0) n.setLeft(put(n.getLeft(), val, col));
    else if (cmp > 0) n.setRight(put(n.getRight(), val, col));
    else n.setColor(col);

    if (isRed(n.getRight()) && !isRed(n.getLeft())) n = rotateLeft(n);
    if (isRed(n.getLeft()) && isRed(n.getLeft().getLeft())) n = rotateRight(n);
    if (isRed(n.getLeft()) && isRed(n.getRight())) flipColors(n);
    return n;
}
private静态无效put(int val,int col)
{root=put(root,val,col);}
专用静态节点put(节点n,整数val,整数col)
{
如果(n==null){
节点t=新节点(val);
t、 setColor(1);
返回t;
}
int cmp=val.compareTo(n.getValue());
if(cmp<0)n.setLeft(put(n.getLeft(),val,col));
否则如果(cmp>0)n.setRight(put(n.getRight(),val,col));
else n.setColor(col);
if(isRed(n.getRight())&&!isRed(n.getLeft())n=rotateLeft(n);
if(isRed(n.getLeft())&&isRed(n.getLeft().getLeft()))n=rotateRight(n);
if(isRed(n.getLeft())&isRed(n.getRight()))flipColors(n);
返回n;
}
然而,关键是我们应该返回一个布尔值——如果用户插入一个重复的值,就像树上已经存在的一样,我们将返回false,并且不附加节点。否则,我们附加它们并返回true;下面是为我们提供的代码,但不是递归的(项目需求的一部分)。虽然我还没有实现一种平衡或正确旋转的方法,但返回的布尔部分仍然有效

public boolean insertNode(Node node) {

    //Here is just an example of setting colors for a node. So far, it is in green color. But you need to modify the code to dynamically adjust the color to
    //either RED or BLACK according to the red-black logic 
    Node current_node;
    // if the root exists
    if (root == null) {
        root = node; // let the root point to the current node
        root.setColor(Node.BLACK);
        return true;
    } else {
        current_node = root;
        node.setColor(1);
        while (current_node != null) {
            int value = current_node.getValue();

            if (node.getValue() < value){ // go to the left sub-tree
                if (current_node.getLeft() != null) // if the left node is not empty
                    current_node = current_node.getLeft();
                else{ // put node as the left child of current_node
                    current_node.setLeft(node);
                    node.setParent(current_node);
                    current_node = null;    }   
                //System.out.println("Left:"+current_node); 
                }

            else if (node.getValue() > value){ // go to the right
                if (current_node.getRight() != null) // if the right node is not empty
                    current_node = current_node.getRight();
                else{ // put node as the right child of current_node
                    current_node.setRight(node);
                    node.setParent(current_node);
                    current_node = null;    }   
                //System.out.println("Right: "+current_node);   
                }

            else{
                //System.out.println("Else: "+current_node);
                return false;   }


            //if(current_node!=null&&current_node.getLeft()!=null&&current_node.getRight()!=null&&current_node.getLeft().isRed()&&current_node.getRight().isRed())
            //  flipColors(node);

        }
    }

    if(node.getParent()!=null){
        node=node.getParent();
        System.out.println("Case: node has parent, val="+node.getValue());
    }

    if(node.getLeft()!=null&&node.getRight()!=null){
        if((node.getRight().isRed())&&!node.getLeft().isRed())
            node=rotateLeft(node);
        if((node.getLeft().isRed())&&(node.getParent()!=null)&&(node.getParent().getLeft().getLeft()!=null)&&(node.getParent().getLeft().getLeft().isRed()))
            node=rotateRight(node);
        if((node.getLeft().isRed()) && (node.getRight().isRed()))
            flipColors(node);
    }
    return true;
}
公共布尔插入节点(节点){
//这里只是一个为节点设置颜色的示例。到目前为止,它是绿色的。但是您需要修改代码以动态地调整颜色
//根据红黑逻辑,红色或黑色
节点当前_节点;
//如果根存在
if(root==null){
root=node;//让根指向当前节点
setColor(Node.BLACK);
返回true;
}否则{
当前_节点=根;
node.setColor(1);
while(当前_节点!=null){
int value=current_node.getValue();
如果(node.getValue()value){//则转到右侧
if(current_node.getRight()!=null)//如果右节点不是空的
当前节点=当前节点。getRight();
else{//put节点作为当前_节点的右子节点
当前_node.setRight(节点);
node.setParent(当前_节点);
当前_节点=null;}
//System.out.println(“右:+当前_节点);
}
否则{
//System.out.println(“Else:+当前_节点);
返回false;}
//if(current_node!=null&¤t_node.getLeft()!=null&¤t_node.getRight()!=null&¤t_node.getLeft().isRed()&¤t_node.getRight().isRed())
//flipColors(节点);
}
}
if(node.getParent()!=null){
node=node.getParent();
System.out.println(“案例:节点有父节点,val=“+node.getValue());
}
if(node.getLeft()!=null&&node.getRight()!=null){
if((node.getRight().isRed())&&!node.getLeft().isRed())
节点=旋转英尺(节点);
如果((node.getLeft().isRed())&&(node.getParent()!=null)&(node.getParent().getLeft().getLeft()!=null)&&(node.getParent().getLeft().getLeft().isRed())
节点=旋转右(节点);
if((node.getLeft().isRed())&&(node.getRight().isRed())
flipColors(节点);
}
返回true;
}

我在网上找不到任何类似的实现,而且布尔值似乎是程序gui正常工作所必需的。如果有人对从哪里开始有好的建议,我将不胜感激

对于递归insertNode,我建议您执行以下操作:创建一个函数
insertNode(Node-Node,Node-current\u-Node)
,该函数返回一个
布尔值。其思想是始终从根节点开始为当前调查的节点调用函数insertNode。如果无法立即将该节点添加到当前_节点,则递归调用负责节点来处理该节点。我根据您的代码提供了一个简短的示例(带有一些注释—基本思想是什么,显然缺少了一些东西)。我希望,我正确地理解了你的问题,这有助于你的理解

public boolean insertNode(Node node) {
    if (root == null) {
        root = node;
        root.setColor(Node.BLACK);
        return true;
    } else {
        boolean result = insertNode(node, root);

        if (result) {
            //Some other important stuff to do...
        }

        return result;
    }
}

public boolean insertNode(Node node, Node current_node) {
    int value = current_node.getValue();

    if (node.getValue() < value) {
        if (current_node.getLeft() != null) {
            // Investigate left
            return insertNode(node, current_node.getLeft());
        } else {
            // Insert node left
            return true;
        }
    } else if (node.getValue() > value) {
        if (current_node.getRight() != null) {
            // Investigate right
            return insertNode(node, current_node.getRight());
        } else {
            // Insert node right
            return true;
        }
    } else {
        return false;
    }
}
公共布尔插入节点(节点){
if(root==null){
根=节点;
setColor(Node.BLACK);
返回true;
}否则{
布尔结果=插入节点(节点,根);
如果(结果){
//还有一些重要的事情要做。。。
}
返回结果;
}
}
公共布尔插入节点(节点节点、节点当前节点){
int value=current_node.getValue();
if(node.getValue()value){
if(当前_节点.getRight()!=null){
//调查权
返回insertNode(node,current_node.getRight());
}否则{
//右插入节点
返回true;
}
}否则{
返回false;
}
}

我现在有了工作功能,
public boolean insertNode(Node node) {
    if(root==null){
        root=node;
        root.setColor(Node.BLACK);
        return true;
    }
    else
        node.setColor(Node.RED);

    return insertNode(node, root);

}

public boolean insertNode(Node node, Node cur){

    if(node.getValue()<cur.getValue()){

        if(cur.getLeft()!=null) 
            return insertNode(node, cur.getLeft());

        else{
            cur.setLeft(node);
            node.setParent(cur);
            handleInsertion(node);
            return true;    }   }

    else if(node.getValue()>cur.getValue()){

        if(cur.getRight()!=null)
            return insertNode(node, cur.getRight());

        else{
            cur.setRight(node);
            node.setParent(cur);
            handleInsertion(node);
            return true;    }   }
    else
        return false;
}