Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Tree Chapter 6 (cont’).

Similar presentations


Presentation on theme: "AVL Tree Chapter 6 (cont’)."— Presentation transcript:

1 AVL Tree Chapter 6 (cont’)

2 Search Trees 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 1- Binary Search Trees(review)

4 BST Traversals A preorder traversal of the BST produces:
A postorder traversal of the BST produces: An inorder traversal of the BST produces: (a sorted list!)

5 BST Search Algorithms 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
algorithm findSmallestBST (val root <pointer>) if (root->left null) return (root) end if return findSmallestBST (root->left) end findSmallestBST

7 Algorithm : Find Largest
algorithm findLargestBST(val root <pointer>) if (root->right null) return (root) end if return findLargestBST(root->right) end findLargestBST

8 Algorithm : BST Search algorithm searchBST (ref root <pointer>, val argument <key>) if (root is null) return null end if if (argument < root->key) return searchBST (root->left, argument) elseif (argument > root->key) return searchBST (root->right, argument) else return root end searchBST

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

10 Insert

11 Insert

12

13 Delete

14 Delete

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: | HL – HR | <= 1 Search effort for an AVL tree is O(log2n) 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 Figure 8-12

17 Figure 8-13 An AVL tree

18 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

19

20

21 Case 1: Left of Left 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

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

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

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

29 AVL Node Structure Node key <keyType> data <dataType> Left <pointer to Node> right <pointer to Node> bal <LH, EH, RH> // Balance factor End Node


Download ppt "AVL Tree Chapter 6 (cont’)."

Similar presentations


Ads by Google