Presentation is loading. Please wait.

Presentation is loading. Please wait.

Self-Balancing Search Trees

Similar presentations


Presentation on theme: "Self-Balancing Search Trees"— Presentation transcript:

1 Self-Balancing Search Trees
Chapter 11 Self-Balancing Search Trees Chapter 11

2 Tip #38: Very Late? Self-Balancing Search Trees Is someone’s junior year in college very late to learn programming? Yes, give up… Yes, give up… Or, You can take the advice of this wise Chinese proverb: “The best time to plant a tree was 20 years ago. The second best time is now.” You are still young. You have most of your life ahead of you. Regardless of what field or trade you pick up, you WILL have to learn something new, whether you like it or not. So, why not learn something that interests you?

3 Self-Balancing Search Trees
Chapter 11 Self-Balancing Search Trees 11.1, pgs

4 Tree Terminology Summary
Self-Balancing Search Trees Branch (Edge) Node (Vertex) Depth Height: 4 Depth: 0 Level: 1 Height Height: 2 Depth: 1 Level: 2 Height: 3 Depth: 1 Level: 2 Height: 1 Depth: 2 Level: 3 Height: 1 Depth: 2 Level: 3 Height: 2 Depth: 2 Level: 3 Height: 1 Depth: 3 Level: 4 Root Node Internal Node Leaf Node Root node is depth 0 Leaf nodes are height 1

5 Self-Balancing Search Trees
The performance of a binary search tree is proportional to the height of the tree or the maximum number of nodes along a path from the root to a leaf. A full binary tree of height k can hold 2k - 1 items. If a binary search tree is full and contains n items, the expected performance is O(log n). However, if a binary tree is not full, the actual performance is worse than expected. To solve this problem, we introduce self-balancing trees to achieve a balance so that the heights of the right and left subtrees are equal (or nearly equal.) Self-balancing trees include the AVL binary search tree, and non-binary search trees such as the B-tree and its specializations, the 2-3 and trees, and the B+ tree.

6 11.1, pgs. 624-628 11.1 Tree Balance and Rotation
Why Balance Is Important Rotation Algorithm for Rotation Implementing Rotation 11.1, pgs

7 Why Balance is Important
Self-Balancing Search Trees Searches into this unbalanced search tree are O(n), not O(log n). A realistic example of an unbalanced tree.

8 Rotation Self-Balancing Search Trees A binary tree with n nodes (root, leaf, and internal nodes) and of height h is balanced if: 2(h − 1) ≤ n < 2h Otherwise, it is unbalanced. For example, a binary tree with height 4 can have between 8 and 15 nodes (between 1 and 8 leaf nodes) to be balanced. We need an operation on a binary tree that changes the relative heights of left and right subtrees, but preserves the binary search tree property.

9 Algorithm for Right Rotation
Self-Balancing Search Trees 20 10 40 5 15 7 root root = left right = data = 20 BTNode = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Right rotate around 20 = left right = data = 7 NULL BTNode

10 Algorithm for Right Rotation
Self-Balancing Search Trees 20 10 40 5 15 7 root root = left right = data = 20 BTNode temp temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) = left right = data = 7 NULL BTNode

11 Algorithm for Right Rotation
Self-Balancing Search Trees 20 10 40 5 15 7 root temp root = left right = data = 20 BTNode temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) Set root->left to value of temp->right = left right = data = 7 NULL BTNode

12 Algorithm for Right Rotation
Self-Balancing Search Trees 20 10 40 5 15 7 root temp root = left right = data = 20 BTNode temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) Set root->left to value of temp->right Set temp->right to root = left right = data = 7 NULL BTNode

13 Algorithm for Right Rotation
Self-Balancing Search Trees 20 10 40 5 15 7 root temp root = left right = data = 20 BTNode temp = left right = data = 10 BTNode = left right = data = 40 NULL BTNode = left right = data = 5 NULL BTNode = left right = data = 15 NULL BTNode Remember value of root->left (temp = root->left) Set root->left to value of temp->right Set temp->right to root Set root to temp = left right = data = 7 NULL BTNode

14 Algorithm for Right Rotation
Self-Balancing Search Trees = left right = data = 20 BTNode data = 10 data = 40 NULL data = 5 data = 15 data = 7 root Remember value of root->left (temp = root->left) Set root->left to value of temp->right Set temp->right to root Set root to temp 20 10 40 5 15 7 root 10 5 20 15 7 40 root

15 11.2 AVL Trees 11.2, pgs. 628-643 Balancing a Left-Left Tree
Balancing a Left-Right Tree Four Kinds of Critically Unbalanced Trees Implementing an AVL Tree Inserting into an AVL Tree Removal from an AVL Tree Performance of the AVL 11.2, pgs

16 AVL Trees Self-Balancing Search Trees In 1962 G.M. Adel'son-Vel'skiî and E.M. Landis developed a self-balancing tree. The tree is known by their initials: AVL. The AVL tree algorithm keeps track of the difference in height of each subtree. As items are added to or removed from a tree, the balance of each subtree from the insertion or removal point up to the root is updated. BalanceFactor = height(right-subtree) – height(left-subtree) The absolute difference between the left sub tree and right sub tree is never greater than 1. If the balance gets out of the range -1 to +1, the tree is rotated to bring it back into range.

17 Balanced: 2(h − 1) ≤ n < 2h AVL: |Rh – Lh| ≤ 1
Q38.1: Which of these trees are 1) binary, 2) balanced, and 3) AVL trees? Why or why not? 20 10 40 5 15 7 Balanced: 2(h − 1) ≤ n < 2h AVL: |Rh – Lh| ≤ 1

18 Not BST, not AVL, Balanced
Q38.1: Which of these trees are 1) binary, 2) balanced, and 3) AVL trees? Why or why not? Not BST, not AVL, Balanced (3,1) (2,1) (0,1) BST, not AVL, not Balanced 20 10 40 5 15 7 Balanced: 2(h − 1) ≤ n < 2h AVL: |Rh – Lh| ≤ 1 (3,2) (0,1) (1,2) (1,1) BST, AVL, Balanced (3,3) (0,2) (1,2) (1,1) (0,1) BST, not AVL, Balanced

19 Balancing a Left-Left Tree
Self-Balancing Search Trees The dark purple trapezoid represents an insertion into this tree, making its height k + 1 50 25 c a Each light purple triangle represents a tree of height k b

20 Balancing a Left-Left Tree
Self-Balancing Search Trees The heights of the left and right subtrees are unimportant; only the relative difference matters when balancing 50 k - (k + 2) -2 25 c -1 k - (k + 1) a b The formula hR – hL is used to calculate the balance of each node

21 Balancing a Left-Left Tree
Self-Balancing Search Trees When the root and left subtree are both left-heavy, the tree is called a Left-Left tree 50 -2 25 c -1 a b A Left-Left tree can be balanced by a rotation right

22 Balancing a Left-Left Tree
Self-Balancing Search Trees 25 a 50 b c Even after insertion, the overall height has not increased

23 Balancing a Left-Right Tree
Self-Balancing Search Trees k - (k + 2) 50 -2 25 c +1 (k + 1) - k a b A Left-Right tree cannot be balanced by a simple rotation right

24 Balancing a Left-Right Tree
Self-Balancing Search Trees 50 -2 25 c +1 a b Subtree b needs to be expanded into its subtrees bL and bR

25 Balancing a Left-Right Tree
Self-Balancing Search Trees 50 -2 25 c +1 40 a -1 bL bR 40 is left-heavy. The left subtree now can be rotated left

26 Balancing a Left-Right Tree
Self-Balancing Search Trees 50 -2 40 c -2 25 bR The overall tree is now Left-Left and a rotation right will balance it. a bL

27 Balancing a Left-Right Tree
Self-Balancing Search Trees 40 50 25 +1 a bL bR c

28 4 Critically Unbalanced Trees
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child

29 AVL Tree Example Build an AVL tree from the words:
Self-Balancing Search Trees Build an AVL tree from the words: "The quick brown fox jumps over the lazy dog"

30 AVL Tree Example The Insert The Self-Balancing Search Trees
Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The Insert The

31 AVL Tree Example The +1 quick Insert quick Self-Balancing Search Trees
Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The +1 Insert quick quick

32 AVL Tree Example The +2 -1 quick brown Insert brown
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The +2 -1 Insert brown quick brown The overall tree is right-heavy (Right-Left) 1. Rotate right around the child (quick)

33 AVL Tree Example The brown +2 +1 quick
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The brown +2 +1 quick Rotate right around the child (quick) Rotate left around the parent (The)

34 AVL Tree Example brown quick The Insert fox
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The Insert fox

35 AVL Tree Example +1 brown The quick fox Insert fox
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +1 brown Insert fox The quick fox

36 AVL Tree Example brown quick The +1 fox Insert jumps
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +1 Insert jumps fox

37 The tree is now left-heavy about quick (Left-Right case)
AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +2 -2 +1 brown Insert jumps The quick fox The tree is now left-heavy about quick (Left-Right case) jumps

38 AVL Tree Example brown quick The +2 -2 fox +1 jumps
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +2 -2 fox +1 Rotate left around the child (fox) jumps

39 AVL Tree Example brown quick The +2 -2 jumps -1 fox
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +2 -2 jumps -1 Rotate left around the child fox Rotate right around the parent

40 AVL Tree Example brown jumps The +1 fox quick
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown jumps The +1 fox quick

41 We now have a Right-Right imbalance
AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +2 +1 -1 brown Insert over The jumps fox quick We now have a Right-Right imbalance over

42 1. Rotate left around the parent
AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown jumps The +2 +1 fox quick -1 1. Rotate left around the parent over

43 AVL Tree Example jumps quick brown -1 The fox over Insert the
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown -1 The fox over Insert the

44 AVL Tree Example jumps brown quick The fox over the Insert the
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps Insert the brown quick The fox over the

45 AVL Tree Example jumps quick brown The fox over the Insert lazy
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown The fox over the Insert lazy

46 AVL Tree Example +1 -1 jumps brown quick The fox over the lazy
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +1 -1 jumps Insert lazy brown quick The fox over the lazy

47 AVL Tree Example jumps quick brown +1 -1 The fox over the lazy
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown +1 -1 The fox over the lazy Insert dog

48 AVL Tree Example -1 +1 jumps brown quick The fox over the dog lazy
Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child -1 +1 jumps Insert dog brown quick The fox over the dog lazy

49


Download ppt "Self-Balancing Search Trees"

Similar presentations


Ads by Google