420 likes | 445 Views
AVL Trees. Binary Tree Issue. One major problem with the binary trees we have discussed thus far: they can become extremely unbalanced this will lead to long search times in the worst case scenario, inserts are done in order this leads to a linked list structure possible O(n) performance
E N D
Binary Tree Issue • One major problem with the binary trees we have discussed thus far: • they can become extremely unbalanced • this will lead to long search times • in the worst case scenario, inserts are done in order • this leads to a linked list structure • possible O(n) performance • this is not likely but it’s performance will tend to be worse than O(log n)
Binary Tree Issue BinaryTree tree = new BinaryTree(); tree.insert(A); tree.insert(C); tree.insert(F); tree.insert(M); tree.insert(Z); root A C F M Z
Balanced Tree • In a perfectly balanced tree • all leaves are at one level • each non-leaf node has two children root M E S C J O W • Worst case search time is log n
AVL Tree • AVL tree definition • a binary tree in which the maximum difference in the height of any node’s right and left sub-trees is 1 (called the balance factor) • balance factor = height(right) – height(left) • AVL trees are usually not perfectly balanced • however, the biggest difference in any two branch lengths will be no more than one level
AVL Tree -1 0 0 0 -1 1 0 0 0 0 AVL Tree AVL Tree
AVL Tree Node • Very similar to regular binary tree node • must add a balance factor field • For this discussion, we will consider the key field to also be the data • this will make things look slightly simpler • they will be confusing enough as it is
Searching AVL Trees • Searching an AVL tree is exactly the same as searching a regular binary tree • all descendants to the right of a node are greater than the node • all descendants to the left of a node are less than the node
Inserting in AVL Tree • Insertion is similar to regular binary tree • keep going left (or right) in the tree until a null child is reached • insert a new node in this position • an inserted node is always a leaf to start with • Major difference from binary tree • must check if any of the sub-trees in the tree have become too unbalanced • search from inserted node to root looking for any node with a balance factor of ±2
Inserting in AVL Tree • A few points about tree inserts • the insert will be done recursively • the insert call will return true if the height of the sub-tree has changed • since we are doing an insert, the height of the sub-tree can only increase • if insert() returns true, balance factor of current node needs to be adjusted • balance factor = height(right) – height(left) • left sub-tree increases, balance factor decreases by 1 • right sub-tree increases, balance factor increases by 1 • if balance factor equals ±2 for any node, the sub-tree must be rebalanced
Inserting in AVL Tree M(-1) M(0) insert(V) E(1) P(0) E(1) P(1) J(0) J(0) V(0) M(-1) M(-2) insert(L) E(1) P(0) E(-2) P(0) J(0) J(1) L(0) This tree needs to be fixed!
Re-Balancing a Tree • To check if a tree needs to be rebalanced • start at the parent of the inserted node and journey up the tree to the root • if a node’s balance factor becomes ±2 need to do a rotation in the sub-tree rooted at the node • once sub-tree has been re-balanced, guaranteed that the rest of the tree is balanced as well • can just return false from the insert() method • 4 possible cases for re-balancing • only 2 of them need to be considered • other 2 are identical but in the opposite direction
Re-Balancing a Tree • Case 1 • a node, N, has a balance factor of 2 • this means it’s right sub-tree is too long • inserted node was placed in the right sub-tree of N’s right child, Nright • N’s right child have a balance factor of 1 • to balance this tree, need to replace N with it’s right child and make N the left child of Nright
Case 1 M(1) M(2) insert(Z) E(0) R(1) E(0) R(2) V(0) V(1) rotate(R, V) Z(0) M(1) E(0) V(0) R(0) Z(0)
Re-Balancing a Tree • Case 2 • a node, N, has a balance factor of 2 • this means it’s right sub-tree is too long • inserted node was placed in the left sub-tree of N’s right child, Nright • N’s right child have a balance factor of -1 • to balance this tree takes two steps • replace Nright with its left child, Ngrandchild • replace N with it’s grandchild, Ngrandchild
Case 2 M(1) M(2) insert(T) E(0) R(1) E(0) R(2) V(0) V(-1) rotate(V, T) M(2) T(0) E(0) R(2) M(1) rotate(T, R) T(1) E(0) T(0) V(0) R(0) V(0)
Rotate Left V(0) R(2) X(1) V(1) R(0) N(0) rotateLeft(R) X(1) N(0) T(0) Z(0) T(0) Z(0)
AVL Tree Example: • Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 11 17 7 53 4
AVL Tree Example: • Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 7 17 4 11 53 13
AVL Tree Example: • Now insert 12 14 7 17 4 11 53 13 12
AVL Tree Example: • Now insert 12 14 7 17 4 11 53 12 13
AVL Tree Example: • Now the AVL tree is balanced. 14 7 17 4 12 53 11 13
AVL Tree Example: • Now insert 8 14 7 17 4 12 53 11 13 8
AVL Tree Example: • Now insert 8 14 7 17 4 11 53 8 12 13
AVL Tree Example: • Now the AVL tree is balanced. 14 11 17 7 12 53 4 8 13
AVL Tree Example: • Now remove 53 14 11 17 7 12 53 4 8 13
AVL Tree Example: • Now remove 53, unbalanced 14 11 17 7 12 4 8 13
AVL Tree Example: • Balanced! Remove 11 11 7 14 4 8 12 17 13
AVL Tree Example: • Remove 11, replace it with the largest in its left branch 8 7 14 4 12 17 13
AVL Tree Example: • Remove 8, unbalanced 7 4 14 12 17 13
AVL Tree Example: • Remove 8, unbalanced 7 4 12 14 13 17
AVL Tree Example: • Balanced!! 12 7 14 4 13 17
In Class Exercises • Build an AVL tree with the following values: 15, 20, 24, 10, 13, 7, 30, 36, 25
15, 20, 24, 10, 13, 7, 30, 36, 25 20 15 15 24 20 10 24 13 20 20 13 24 15 24 10 15 13 10
15, 20, 24, 10, 13, 7, 30, 36, 25 20 13 13 24 10 20 10 15 7 15 24 30 7 13 36 10 20 7 15 30 24 36
15, 20, 24, 10, 13, 7, 30, 36, 25 13 13 10 20 10 20 7 15 30 7 15 24 24 36 30 25 25 36 13 10 24 7 20 30 15 25 36
Remove 24 and 20 from the AVL tree. 13 13 10 24 10 20 7 20 30 7 15 30 15 25 36 25 36 13 13 10 30 10 15 7 15 36 7 30 25 25 36
Example of Insertions in an AVL Tree 2 3 20 20 1 1 1 2 10 30 10 30 0 0 0 1 0 0 5 35 5 35 25 25 0 40 Now Insert 45 AVL Trees - Lecture 8
Single rotation (outside case) 3 3 20 20 1 2 1 2 10 30 10 30 0 2 0 0 0 1 5 35 5 40 25 25 0 0 35 45 40 1 Imbalance 0 45 Now Insert 34 AVL Trees - Lecture 8
Double rotation (inside case) 3 3 20 20 1 3 1 2 10 30 10 35 0 2 0 0 1 1 5 Imbalance 40 5 40 25 30 0 25 34 1 0 45 35 45 0 Insertion of 34 0 34 AVL Trees - Lecture 8
AVL Tree Deletion • Similar but more complex than insertion • Rotations and double rotations needed to rebalance • Imbalance may propagate upward so that many rotations may be needed. AVL Trees - Lecture 8