繁体   English   中英

红/黑树节点插入的异常Java实现

[英]Unusual Java implementation of red/black tree node insertion

我正在为Java类编写有关红/黑树的程序。 我对它们通常如何工作有很好的了解,应该使用递归插入方法。 下面是我通常使用的,以匹配教授的Node类。 关于颜色,0是黑色,1是红色。 给我们的Node类根本不处理键。

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;
}

但是,要注意的是我们应该返回一个布尔值-如果用户像在树上那样插入重复值,则返回false并且不附加该节点。 否则,我们将它们附加并返回true;否则,我们将返回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;
}

我无法在网上找到任何可比较的实现,并且似乎布尔值对于程序的gui正常工作是必需的。 如果有人对从哪里开始有很好的建议,我将不胜感激!

对于递归insertNode,我建议您执行以下操作:创建一个函数insertNode(Node node, Node current_node) ,该函数返回boolean值。 这个想法是始终从根节点开始为当前调查的节点调用函数insertNode。 如果不能立即将节点添加到current_node,则递归调用负责节点以处理该节点。 我已经根据您的代码向您提供了一个简短的示例(带有一些注释的基本思想是,显然缺少一些东西)。 希望我能正确回答您的问题,这有助于您的理解。

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;
    }
}

我现在具有工作功能,如下所示:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM