简体   繁体   中英

Binary Search Trees: Height of a node in an AVL tree

Here is my AVL tree that I had to make (hopefully large enough to see clearly)

http://oi46.tinypic.com/2426fer.jpg

I know my tree is correct for what I had to do, but I am unsure about the height of the AVL tree and how that works, as you can probably see within my drawing. Really hope you can help, I understand the concept of everything else that has to do with AVL trees. Thanks


You deleted a previous question in which you were having a problem with your binarySearchTree. Here was the solution I wrote before you deleted the question. Maybe it'll help.

            if (tree.left == null && tree.right == null) {
                    if (compResult < 0) {
                            tree.left.data = item;
                            return true;
                    }
                    if (compResult > 0) {
                            tree.right.data = item;
                            return true;
                    }
            }
            if (compResult < 0) {
                    if (tree.left == null) {        //RED FLAG 
                            tree.left.data = item;  //BUT tree.left IS NULL!!
                            return true;
                    } else
                            add(tree = tree.left, item, count);
            }
            if (compResult > 0) {
                    if (tree.right == null) {       //RED FLAG
                            tree.right.data = item; //BUT tree.right IS NULL!!
                            return true;
                    } else
                            add(tree = tree.right, item, count);
            }

To fix these, create a new node and then assign it to tree.left or tree.right.

You should do an iterative add rather than a recursive one, it's faster.

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