70 likes | 158 Views
Lab 7 – AVL Trees. Some suggestions. To extend or not to extend. Modify BinaryTreeNode to include new fields & modify LinkedBinarySearchTree to be AVLTree
E N D
Lab 7 – AVL Trees Some suggestions
To extend or not to extend • Modify BinaryTreeNode to include new fields & modify LinkedBinarySearchTree to be AVLTree • Modify BinaryTreeNode to include new fields & extend LinkedBinarySearchTree to AVLTree(rewrite add and remove methods in AVLTree) • Extend BinaryTreeNode to AVLTreeNode with new fields & modify LinkedBinaryTree and LinkedBinarySearchTree to use AVLTreeNode • Extend BinaryTreeNode to AVLTreeNode with new fields & extend LinkedBinarySearchTree to AVLTreeNode (uses AVLTreeNode)
Modification(?) by extension only • Inheritance (extends) is meant to modify only by extension, not by replacement. • I can extend a class without even having the source code for it
Rebalancing after add/remove • For every node on the path from new node (or removed node) to the root, must check balance factor and rebalance if needed. • Can be done with recursion (without parent link), but is probably easier to do (and think about) with an iterative approach
From Wikipedia (only 2 basic rotations) if (balance_factor(L) == 2) { //The left column let P=left_child(L) if (balance_factor(P) == -1) { //The "Left Right Case" rotate_left(P) //reduce to "Left Left Case" } //Left Left Case rotate_right(L); } else { // balance_factor(L) == -2, the right column let P=right_child(L) if (balance_factor(P) == 1) { //The "Right Left Case" rotate_right(P) //reduce to "Right Right Case" } //Right Right Case rotate_left(L); }
General suggestions • Add comments to each section of the code you need to modify to clarify (for you) what that code is supposed to accomplish. • Develop incrementally with some output capabilities (pretty print with balance) to get better pictures of where you stand and to debug. • Save versions under different names (dated) so that you can go back to a version if you go down a wrong trail. • PLAN BEFORE YOU CODE!