Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.

Similar presentations


Presentation on theme: "Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root."— Presentation transcript:

1 Chapter 6 (cont’) 1 AVL Tree

2 Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root All items in right sub-tree are greater than or equal to the root Each sub-tree is a binary search tree AVL trees (balanced)

3 3 1- Binary Search Trees(review)

4 BST Traversals 4 A preorder traversal of the BST produces: 23 18 12 20 44 35 52 A postorder traversal of the BST produces: 12 20 18 35 52 44 23 An inorder traversal of the BST produces: 12 18 20 23 35 44 52 (a sorted list!)

5 BST Search Algorithms 5 Find Smallest Node: recursively follow the left branch until reaching a leaf Find Largest Node: recursively follow the right branch until reaching a leaf BST Search: recursively search for key node in a sub-tree (similar to the binary search)

6 Algorithm : Find Smallest 6 algorithm findSmallestBST (val root ) 1. if (root->left null) 1. return (root) 2. end if 3. return findSmallestBST (root->left) end findSmallestBST

7 Algorithm : Find Largest 7 algorithm findLargestBST(val root ) 1. if (root->right null) 1. return (root) 2. end if 3. return findLargestBST(root->right) end findLargestBST

8 Algorithm : BST Search 8 algorithm searchBST (ref root, val argument ) 1. if (root is null) 1. return null 2. end if 3. if (argument key) 1. return searchBST (root->left, argument) 4. elseif (argument > root->key) 1. return searchBST (root->right, argument) 5. else 1. return root 6. end if end searchBST

9 9 Algorithm : Recursively add node to BST algorithm addBST (ref root, val new ) 1.if (root is null) 1.root = new 2.else Locate null sub-tree for insertion 1.if (new->key key) 1.addBst (root->left, new) 2.else 1.addBst (root->right, new) 3.end if 4.return end addBST

10 10

11 11

12 12

13 13

14 14

15 15 2- AVL Trees Invented by Adelson-Velskii and Landis Height-balanced binary search tree where the heights of sub- trees differ by no more than 1: | H L – H R | <= 1 Search effort for an AVL tree is O(log 2 n) 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)

16 16 Figure 8-12

17 17 Figure 8-13 An AVL tree

18 Balancing Trees 18 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

19 19

20 20

21 Case 1: Left of Left 21 The sub-tree of a left high tree has become left high Simple right rotation: Rotate the out of balance node (the root) to the right Complex right rotation: 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

22 22

23 23 Case 2: Right of Right Mirror of Case 1: The sub-tree of a right high tree has become right high Simple left rotation: Rotate the out of balance node (the root) to the left 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

24 24

25 25 Case 3: Right of Left The sub-tree of a left high tree has become right high 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 Complex double rotation right: Rotate the right-high left sub-tree to the left; rotate the left-high left sub-tree to the right

26 26

27 27 Case 4: Left of Right Mirror of Case 3: The sub-tree of a right high tree has become left high Simple double rotation right: Rotate right sub-tree to the right; rotate root to the left, so the old root's right node is the new root Complex double rotation right: Rotate the left-high right sub-tree to the right; rotate the right-high right sub-tree to the left

28 28

29 AVL Node Structure 29 Node key data Left right bal // Balance factor End Node


Download ppt "Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root."

Similar presentations


Ads by Google