简体   繁体   中英

Printing an AVL Tree by level in Java

I am attempting to take an AVL tree and display it, level by level, but I'm failing somehow and don't know where. Attached is an image showing my current output. What I actually should be getting is a full binary tree, so obviously something is going wrong. In the attached image, there is a photo of my "byLevel" print function, so you can see how I'm attempting to print them, and I will attach my insert function, as those are the only two functions that matter for this part. I'd appreciate any help, I don't understand what I'm doing wrong, as this is a commonly used algorithm.

private Node insert(Node newNode, String newWord){
    if(newNode == null){
        newNode = new Node(newWord);
    }
    else if (newWord.compareToIgnoreCase(newNode.getReservedWord()) < 0){
        newNode.setlChild(insert(newNode.getlChild(), newWord));

        if(depth(newNode.getlChild()) - depth(newNode.getrChild()) == 2){
            if(newWord.compareToIgnoreCase(newNode.getlChild().getReservedWord()) < 0){
                newNode = rotateWithLeftChild(newNode);
            }
            else{
                newNode = doubleWithLeftChild(newNode);
            }
        }
    }
    else if (newWord.compareToIgnoreCase(newNode.getReservedWord()) > 0){
        newNode.setrChild(insert(newNode.getrChild(), newWord));

        if(depth(newNode.getrChild()) - depth(newNode.getlChild()) == 2){
            if(newWord.compareToIgnoreCase(newNode.getrChild().getReservedWord()) > 0){
                newNode = rotateWithRightChild(newNode);
            }
            else{
                newNode = doubleWithRightChild(newNode);
            }
        }
    }
    else;

    newNode.setDepth(max(depth(newNode.getlChild()), depth(newNode.getrChild()) + 1));
    /*if(!this.getAllNodes().contains(newNode)){
        this.getAllNodes().add(newNode);
    }*/
    return newNode;
}

private Node rotateWithLeftChild(Node nodeToRotate){
    Node newNode = nodeToRotate.getlChild();
    nodeToRotate.setlChild(newNode.getrChild());
    newNode.setrChild(nodeToRotate);
    nodeToRotate.setDepth(max(depth(nodeToRotate.getlChild()), depth(nodeToRotate.getrChild()) + 1));
    newNode.setDepth(max(depth(newNode.getlChild()), nodeToRotate.getDepth() + 1));
    return newNode;
}

private Node rotateWithRightChild(Node nodeToRotate){
    Node newNode = nodeToRotate.getrChild();
    nodeToRotate.setrChild(newNode.getlChild());
    newNode.setlChild(nodeToRotate);
    nodeToRotate.setDepth(max(depth(nodeToRotate.getlChild()), depth(nodeToRotate.getrChild()) + 1));
    newNode.setDepth(max(depth(newNode.getrChild()), nodeToRotate.getDepth() + 1));
    return newNode;
}

private Node doubleWithLeftChild(Node nodeToRotate){
    nodeToRotate.setlChild(rotateWithRightChild(nodeToRotate.getlChild()));
    return rotateWithLeftChild(nodeToRotate);
}

private Node doubleWithRightChild(Node nodeToRotate){
    nodeToRotate.setrChild(rotateWithLeftChild(nodeToRotate.getrChild()));
    return rotateWithRightChild(nodeToRotate);
}

TestOutput

Once again, fixed my own problem, and yes, the code provided was enough. The problem was simply that I was miscalculating my depth by adding +1 to the second of the max function, instead of adding it after the max function. I think the question was well-stated enough on it's own since AVL trees are common coding. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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