1 / 7

Lab 7 – AVL Trees

Lab 7 – AVL Trees. Some suggestions. To extend or not to extend. Modify BinaryTreeNode to include new fields & modify LinkedBinarySearchTree to be AVLTree

audi
Download Presentation

Lab 7 – AVL Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 7 – AVL Trees Some suggestions

  2. 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)

  3. 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

  4. 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

  5. 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); }

  6. 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!

  7. So, take an extra week.

More Related