90 likes | 311 Views
Mark Allen Weiss: Data Structures and Algorithm Analysis in Java. Chapter 4: Trees. AVL Trees. Lydia Sinapova, Simpson College. AVL Trees. Keep the tree balanced Use node rotation Balanced condition: The left and the right subtrees of each node should differ by at most one level.
E N D
Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 4: Trees AVL Trees Lydia Sinapova, Simpson College
AVL Trees • Keep the tree balanced • Use node rotation • Balanced condition: The left and the right subtrees of each node should differ by at most one level. The method was proposed by two Russian scientists Adelson-Velskii and Landis in 1962
5 3 7 6 8 9 Single Rotation The sub-tree containing the inserted node (rooted at 8) is at the same side as the ‘heavier’ sub-tree of node 5 (rooted at 7) New node Links to be changed
7 5 8 3 6 9 After the Rotation The middle node 6 is switched to the other subtree
5 5 8 7 8 7 7 8 9 6 8 7 9 6 Double Rotation New node The new node 6 is in theleft subtree of node 8, while node 8 is therightsubtree of 5 -> rotate 7 and 8 to obtain same-side trees. Animation
Splay Trees Move to the top each accessed node with rotations, decreasing the depth of the tree.
E I R S A C G H N Multi-Way Search Nodes contain more than one key nodes are used only to contain the keys, the actual records are stored at the terminal nodes at the bottom of the tree
Complexity Issues • AVL trees: Search,Insertion, and Deletion:O(logN) Creating the tree – O(N) • Trees withmultiple keys: O(LogmN) m – branching factor