Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim...
-
Upload
franklin-shelton -
Category
Documents
-
view
216 -
download
0
Transcript of Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim...
![Page 1: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/1.jpg)
Chapter 19: Binary Search Trees
or How I Learned to Love AVL Trees
andBalance The Tree
Group 6: Tim Munn
![Page 2: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/2.jpg)
Basics
What Is It? ‘A simple data structure that extends
the binary search algorithm to allow for insertions and deletions’
Average time for operations is O(log N) but a worst case scenario of O(N)
![Page 3: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/3.jpg)
Basics continued…
How Do We Avoid Worst Case? AVL Trees Red-Black Trees AA-Trees
Focus Will Be: AVL Trees
![Page 4: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/4.jpg)
Some Definitions
Order Property For every node x, values in left
subtree are less than x and values in right subtree are greater than x
Duplicate nodes or key values can be handled by a counter
![Page 5: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/5.jpg)
Basic Operations or Methods find()
Start at root and go left or right based on comparison
findMin() Start at root and keep going left
findMax() Start at root and keep going right
insert() remove()
![Page 6: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/6.jpg)
A Search Tree
![Page 7: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/7.jpg)
Not A Search Tree
![Page 8: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/8.jpg)
Problems
Insertions and Deletions Can Lead To Unbalanced Tree
If Input Is Sorted…
If Input Contains Long Sequence of Nonrandomness
![Page 9: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/9.jpg)
Insertion Sequence: 7,2,9,5,3,1
![Page 10: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/10.jpg)
Insertion Sequence: 3 2 1 9 7 5
![Page 11: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/11.jpg)
Deleting A Node
Most complicated operation If node is a leaf = delete If node has one child, connect child
to node to be deleted’s parent, then delete
If node has two children = most complicated!
![Page 12: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/12.jpg)
Deleting a Node with 2 Children
Idea: Replace node to be deleted with the smallest node in the right subtree then remove the node Finding smallest node is easy -> keep
going left in right subtree
![Page 13: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/13.jpg)
Deletion Example: Single Child
Remove 5
![Page 14: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/14.jpg)
Deletion Example: Single Child with a Subtree
Remove 5
![Page 15: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/15.jpg)
Deletion Example: 2 Children
Remove 2
Remove 2
![Page 16: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/16.jpg)
Balanced Tree
![Page 17: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/17.jpg)
An Unbalanced Tree
![Page 18: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/18.jpg)
AVL Trees
Balanced Search Tree Adelson-Velskii and Landis for those
historically inclined
Satisfies O(log N)
![Page 19: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/19.jpg)
Basic Idea
Require left and right tree are same height
Using recursion, this implies that all nodes must adhere to this as each node is a root of a subtree
Insertion become difficult due to balancing
![Page 20: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/20.jpg)
AVL Definition
A Binary Search Tree with balance property that for any node, the left and right subtree heights differ by no more than 1.
![Page 21: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/21.jpg)
AVL Tree…Not
![Page 22: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/22.jpg)
AVL Tree…Not – The Problem
![Page 23: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/23.jpg)
Balancing Approaches ‘Random’ insertions and deletions will
eventually lead to an unbalanced tree To simplify task, information regarding
the height of a node's subtrees is stored in the node structure.
If we know the relative height, we can arrange to simply store a value that indicates The subtree heights are the same Left subtree is higher Right subtree is higher
![Page 24: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/24.jpg)
It All Boils Down To...
Only nodes on along the insertion to the root may have their balances alterered...because...only their subtrees are being changed
A height imbalance occurs if for a node X abs(height(left subtree of X) –
height(right subtree of X)) = 2
![Page 25: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/25.jpg)
The 4 Cases for Rotations
1. Insertion in left subtree of left child of X
2. Insertion in right subtree of left child of X
3. Insertion in left subtree of right child of X
4. Insertion in right subtree of right child of X
![Page 26: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/26.jpg)
Become 2 Cases for Rotations
1 & 4 are symmetrically the same -> left–left & right–right Handled by Single Rotation
2 & 3 are symmetrically the same -> right–left & left-right Handled by more complicated Double
Rotation
![Page 27: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/27.jpg)
The Single Rotation
Switches Role of Parent and Childwhile maintaining search order
Before Inserting 1
During
After
![Page 28: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/28.jpg)
Double Rotation
Start at page 627
![Page 29: Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.](https://reader035.fdocuments.us/reader035/viewer/2022062520/5697bfd51a28abf838cad8a3/html5/thumbnails/29.jpg)
A ‘Simple’ Project????public class AVLNode extends BinaryNode {
public static final int LEFT_HEAVY = -1;public static final int RIGHT_HEAVY = +1;public static final int EQUAL = 0;
private int balance;
AVLNode(Comparable theElement){
super(theElement);this.balance = EQUAL;
}public int getBalance({ return balance;}public void setBalance(int balance){ this.balance = balance;}
}