{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

B-trr and AVL lecture

B-trr and AVL lecture - Self-Balancing Search Trees Chapter...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Self-Balancing Search Trees
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 11: Self-Balancing Search Trees 2 Chapter Outline The impact of balance on search tree performance Balanced binary search trees: AVL trees Other balanced search trees: B-trees
Background image of page 2
Chapter 11: Self-Balancing Search Trees 3 Why Balance is Important Searches in unbalanced tree can be O(n)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 11: Self-Balancing Search Trees 4 Rotation For self-adjusting, need a binary tree operation that: Changes the relative height While preserving the binary search tree property Algorithm for rotation (toward the right): 1. Save value of root->left ( temp = root->left ) 2. Set root->left to value of root->left->right 3. Set temp->right to root 4. Set root to temp
Background image of page 4
Chapter 11: Self-Balancing Search Trees 5 Rotation (2) Hint: Watch what happens to 10, 15, and 20, below:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 11: Self-Balancing Search Trees 6 Rotation (3) Nodes v and w decrease in height Nodes y and z increase in height Node x remains at same height y v w x z w v y x z
Background image of page 6
Chapter 11: Self-Balancing Search Trees 7 Adding Rotation To BST Just add rotation methods in this subclass
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 11: Self-Balancing Search Trees 8 Coding Rotation template<typename Item_Type>   class BST_With_Rotate : public  Binary_Search_Tree<Item_Type> {   protected:        BTNode<Item_Type>* temp = local_root->left;     local_root->left = temp->right;     temp->right = local_root;     local_root = temp;   }   // Insert rotate_left here };
Background image of page 8
Chapter 11: Self-Balancing Search Trees 9 AVL Tree Insert/erase: update balance of each subtree from point of change to the root Rotation brings unbalanced tree back into balance The height of a tree is the number of nodes in the longest path from the root to a leaf node Height of empty tree is 0: ht(empty) = 0 Height of others: ht(n) = 1+max(ht(n.left), ht(n.right)) Balance (n) = ht(n.right) – ht(n.left)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 11: Self-Balancing Search Trees 10 AVL Tree (2) The balance of node n = ht(n.right) – ht(n.left) In an AVL tree, restrict balance to -1, 0, or +1 That is, keep nearly balanced at each node
Background image of page 10
Chapter 11: Self-Balancing Search Trees 11 AVL Tree Insertion We consider cases where new node is inserted into the left subtree of a node n Insertion into right subtree is symmetrical Case 1: The left subtree height does not increase No action necessary at n Case 2: Subtree height increases, balance( n ) = +1, 0 Decrement balance( n ) to 0, -1 Case 3: Subtree height increases, balance( n ) = -1 Need more work to obtain balance (would be -2)
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 11: Self-Balancing Search Trees 12 AVL Tree Insertion: Rebalancing These are the cases: Case 3a: Left subtree of left child grew: Left-left heavy tree Case 3b:
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 34

B-trr and AVL lecture - Self-Balancing Search Trees Chapter...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online