230 likes | 357 Views
Lab#9. AVL + Heap + Big O. AVL Tree. Height-balanced binary search tree where the heights of sub-trees differ by no more than 1: | H L – H R | <= 1 Each node has a balance factor Balance factors may be: Left High (LH) = +1 (left sub-tree higher than right sub-tree)
E N D
Lab#9 AVL + Heap + Big O
AVL Tree • Height-balanced binary search tree where the heights of sub-trees differ by no more than 1: | HL – HR | <= 1 • Each node has a balance factor • Balance factors may be: • Left High (LH) = +1 (left sub-tree higher than right sub-tree) • Even High (EH) = 0 (left and right sub-trees same height) • Right High (RH) = -1 (right sub-tree higher than left sub-tree)
HR=2 HR=3 HL=1 HL=1 AVL Tree HL-HR=1 Unbalanced BST HL-HR = 2
Balancing Trees • Insertions and deletions potentially cause a tree to be imbalanced • When a tree is detected as unbalanced, nodes are balanced by rotating nodes to the left or right • Four imbalance cases: • Left of left: the sub-tree of a left high tree has become left high • Right of right: the sub-tree of a right high tree has become right high • Right of left: the sub-tree of a left high tree has become right high • Left of right: the sub-tree of a right high tree has become left high • Each imbalance case has simple and complex rotation cases
Case1: Left of left Simple Right rotation Single Right rotation Rotate the out of balance node (the root) to the right
Case1: Left of left Insert 4 18 12 12 20 8 18 14 8 14 4 20 Complex Right rotation 4 • Rotate root to the right, so the old root is the right sub-tree of the new root; the new root's right sub-tree is connected to the old root's left sub-tree
Case2 : Right of right Simple left rotation Rotate the out of balance node (the root) to the left
Case2 : Right of right Complex left rotation Rotate root to the left, so the old root is the left sub-tree of the new root; the new root's left sub-tree is connected to the old root's right sub-tree
Case3 : Right of left Insert 7 11 11 7 5 7 left rotation Right rotation 11 5 7 5 • Simple double rotation right: Rotate left sub-tree to the left; rotate root to the right, so the old root's left node is the new root
Case4 : Left of right Rotate right
Case4 : Left of right Rotate Left Rotate right sub-tree to the right; rotate root to the left, so the old root's right node is the new root
Case4 : Left of right Rotate Right
Case4 : Left of right Rotate Left
HW: • The following keys are inserted • (in the order given) into an initially empty AVL tree. • Show the AVL tree after each insertion. 24,39,31,46,48,34,19,5,29
Heap • A particular kind of binary tree, called a heap • It has two properties: • The value of each node is greater(max heap)/less (min heap) than or equal to the value stored in each of its children. • The tree is perfectly balanced, and the leaves in the last level are all in the leftmost positions. • Heaps can be implemented by arrays. • Elements in a heap are not perfectly ordered. There is no relation between siblings .
Heap or Not ? Min Heap 10 10 20 80 20 20 80 40 60 85 99 30 15 50 700 not a heap heap
Heaps as Priority Queues • A heap is a way to implement priority queues. • Two procedures has to be implemented : • Enqueue an element : the element is added at the end of the heap as the last leaf . • 2. Dequeue an element : remove the root element from the heap.
Enqueue 60 40 50 20 25 30 35 10 12 15 45 60 45 50 20 40 30 35 10 12 15 25
Dequeue 60 40 50 20 25 30 35 10 12 15 19 19 40 50 20 25 30 35 10 12 15
50 40 19 20 25 30 35 10 12 15 50 40 35 20 25 30 19 10 12 15
Algorithm efficiency • A function of the number of elements to be processed • f(n)=efficiency • If a function is linear – that is, if it contains no loop – then its efficiency is a functions of the number of instructions it contains. • On the other hand, functions that loop vary widely in their efficiency. Algorithm Efficiency
What is the Big-O complexity of each of the following methods? O(n) O(n^2) O(n)