{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

B-trr and AVL lecture

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

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

Self-Balancing Search Trees

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

View Full Document
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
Chapter 11: Self-Balancing Search Trees 3 Why Balance is Important Searches in unbalanced tree can be O(n)

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

View Full Document
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
Chapter 11: Self-Balancing Search Trees 5 Rotation (2) Hint: Watch what happens to 10, 15, and 20, below:

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

View Full Document
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
Chapter 11: Self-Balancing Search Trees 7 Adding Rotation To BST Just add rotation methods in this subclass

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

View Full Document
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 };
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)

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

View Full Document
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
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)

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

View Full Document
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:
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
Ask a homework question - tutors are online