Presentation is loading. Please wait.

Presentation is loading. Please wait.

Friday, April 13, 2018 Announcements… For Today…

Similar presentations


Presentation on theme: "Friday, April 13, 2018 Announcements… For Today…"— Presentation transcript:

1 Friday, April 13, 2018 Announcements… For Today…
Self-Balancing Search Trees Announcements… Lab 09 – Pokemon Reviews Lab 10 – Quicksort due Monday, April 16 Direct s to (w/netid) Learning Suite Scores For Today… Self-Balancing Trees 11.1-2 11.3

2 Attendance Quiz #39 Self-Balancing Search Trees

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

4 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.

5 Tree Terminology Self-Balancing Search Trees A tree consists of a collection of elements or nodes, with each node linked to its successors Each node in a tree has exactly one parent except for the root node, which has no parent The node at the top of a tree is called its root The predecessor of a node is called its parent dog cat wolf canine The links from a node to its successors are called branches dog is the parent of cat in this tree canine is a descendant of cat in this tree A subtree of a node is a tree whose root is a child of that node The successors of a node are called its children Nodes that have the same parent are siblings A node that has no children is called a leaf node Leaf nodes also are known as external nodes, and non-leaf nodes are known as internal nodes A generalization of the parent-child relationship is the ancestor-descendant relationship

6 Tree Terminology Self-Balancing Search Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The height of this tree is 3 The level of a node is its distance from the root plus 1 Level 1 Level 2 Level 3 dog cat wolf canine The level of a node is defined recursively If node n is the root of tree T, its level is 1 If node n is not the root of tree T, its level is the level of its parent The height of a tree is the number of nodes in the longest path from the root node to a leaf node

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

8 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.

9 Rotation Self-Balancing Search Trees A binary tree with nodes (leaf nodes and internal nodes, including the root node) and height is balanced if the following is true: 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.

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

11 Algorithm for Right Rotation
Self-Balancing Search Trees 20 10 40 5 15 7 root = left right = data = 20 BTNode root 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

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 = 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 = left right = data = 7 NULL BTNode

14 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

15 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

16 Implementing Rotation
Self-Balancing Search Trees

17 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

18 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.

19 Which of the following binary trees are 1) balanced and 2) AVL trees
Which of the following binary trees are 1) balanced and 2) AVL trees? Why or why not? 20 10 40 5 15 7 Balanced: 2(h − 1) ≤ n < 2h AVL: |Rh – Lh| ≤ 1

20 BST, not AVL, not Balanced
Which of the following binary trees are 1) balanced and 2) AVL trees? Why or why not? (3,1) (2,1) (0,1) BST, not AVL, not Balanced 20 10 40 5 15 7 Not BST, not AVL, Balanced 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

21 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

22 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

23 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

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

25 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

26 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

27 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

28 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

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

30 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

31 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"

32 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

33 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

34 AVL Tree Example The +2 -1 quick brown Insert brown
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)

35 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)

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 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

46 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

47 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

48 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

49 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

50 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

51 Insert the following into an AVL tree: "The cure for boredom is curiosity."

52 Implementing an AVL Tree
Self-Balancing Search Trees

53 Inserting into an AVL Tree
Self-Balancing Search Trees The easiest way to keep a tree balanced is never to let it remain critically unbalanced. If any node becomes critical, rebalance immediately. Identify critical nodes by checking the balance at the root node as you return along the insertion path.

54 Inserting into an AVL Tree
Self-Balancing Search Trees Algorithm for Insertion into an AVL Tree 1. if the root is NULL 2. Create a new tree with the item at the root and return true else if the item is equal to root->data 3. The item is already in the tree; return false else if the item is less than root->data 4. Recursively insert the item in the left subtree. 5. if the height of the left subtree has increased (increase is true) Decrement balance if balance is zero, reset increase to false if balance is less than –1 9. Reset increase to false. 10. Perform a rebalance_left else if the item is greater than root->data 11. The processing is symmetric to Steps 4 through 10. Note that balance is incremented if increase is true.

55 Recursive insert Function
Self-Balancing Search Trees

56 Effect of Rotations on Balance
Self-Balancing Search Trees Initial Algorithm for rebalanceLeft 1. if the left subtree has positive balance (Left-Right case) 2. Rotate left around left subtree root. 3. Rotate right. This rebalance algorithm is incomplete as the balance of the nodes has not been adjusted. For a Left-Left tree the balances of the new root node and of its right child are 0 after a right rotation. Left-Right is more complicated: the balance of the root is 0.

57 Effect of Rotations on Balance
Self-Balancing Search Trees if the critically unbalanced situation was due to an insertion into subtree bL (Left-Right-Left case), the balance of the root's left child is 0 and the balance of the root's right child is +1

58 Effect of Rotations on Balance
Self-Balancing Search Trees if the critically unbalanced situation was due to an insertion into subtree bR (Left-Right-Right case), the balance of the root's left child is -1 and the balance of the root's right child is 0

59 Revised Algorithm for rebalance_left
Self-Balancing Search Trees Revised Algorithm for rebalance_left if the left subtree has a positive balance (Left-Right case) if the left-right subtree has a negative balance (Left-Right-Left case) Set the left subtree (new left subtree) balance to 0 Set the left-left subtree (new root) balance to 0 Set the local root (new right subtree) balance to +1 else if the left-right subtree has a positive balance (Left-Right-Right case) Set the left subtree (new left subtree) balance to –1 Set the local root (new right subtree) balance to 0 else (Left-Right Balanced case) Rotate the left subtree left else (Left-Left case) Set the left subtree balance to 0 Set the local root balance to 0 Rotate the local root right Lines 4, 8, and 12 should say, “set the left-right subtree” instead of “set the left-left subtree”

60 Removal from an AVL Tree
Self-Balancing Search Trees Removal from a left subtree, increases the balance of the local root. from a right subtree, decreases the balance of the local root. The binary search tree removal function can be adapted for removal from an AVL tree. A data field decrease tells the previous level in the recursion that there was a decrease in the height of the subtree from which the return occurred. The local root balance is incremented or decremented based on this field. If the balance is outside the threshold, a rebalance function is called to restore balance.

61 Removal from an AVL Tree
Self-Balancing Search Trees Functions rebalance_left, and rebalance_right need to be modified so that they set the balance value correctly if the left (or right) subtree is balanced. When a subtree changes from either left-heavy or right-heavy to balanced, then the height has decreased, and decrease should remain true. When the subtree changes from balanced to either left-heavy or right-heavy, then decrease should be reset to false. Each recursive return can result in a further need to rebalance.

62 Performance of the AVL Tree
Self-Balancing Search Trees Since each subtree is kept close to balanced, the AVL has expected O(log n). Each subtree is allowed to be out of balance ±1 so the tree may contain some holes. In the worst case (which is rare) an AVL tree can be 1.44 times the height of a full binary tree that contains the same number of items. Ignoring constants, this still yields O(log n) performance. Empirical tests show that on average log2.n comparisons are required to insert the nth item into an AVL tree – close to insertion into a corresponding complete binary search tree.

63 Pros and Cons of AVL Trees
Self-Balancing Search Trees Argument for AVL trees: Search is O(log n) since AVL trees are always balanced. Insertion and deletions are also O(log n). The height balancing adds no more than a constant factor to the speed of insertion. Arguments against using AVL trees: Difficult to program & debug; more space for balance factor. Asymptotically faster but rebalancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). May be OK to have O(n) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees).

64 Pros and Cons of AVL Trees
Self-Balancing Search Trees

65


Download ppt "Friday, April 13, 2018 Announcements… For Today…"

Similar presentations


Ads by Google