简体   繁体   中英

AVL Tree Insert NullPointerException?

I have a Binary Search Tree insert method that works. I am trying to add a triNodeRestructuring method that balances it if it is not height balanced. In my insertion method, after I insert the item, at the end of insertion method, I have an if statement that checks to see if it is height balanced. If it is , it prints out "Height Balanced". Else, it prints out "Not height balanced" followed by a call to my triNodeRestructure method on the item I just inserted. When I run my code, it gives me a null pointer exception at my call to triNodeRestructure(item). It traces it back to my triNodeRestructure method to a line that says:

AVLNode<E> z = x.getParent().getParent();

I have a setter for setPArent and a getter for getParent. Those are the only parent methods I have.

I inserted 50, 49, 48 in that respective order. So based on this, z should be 50 since x is the last node I inserted which is 48. Why do I get a null pointer exception? Z is not null so this shouldn't be happening. Any help would be appreciated.

I figured out what was wrong shortly after posting this question. I forgot to write a setParent() line in my insert method that never set a parent for an inserted node. Silly me.

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