cs 261 –data structures - college of...
TRANSCRIPT
![Page 1: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/1.jpg)
CS 261 – Data Structures
AVL Trees
1
![Page 2: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/2.jpg)
Binary Search Tree
• Complexity of BST operations:
–proportional to the length of the path from a node to the root
• Unbalanced tree: operations may be O(n)
–E.g.: adding elements in a sorted order
2
![Page 3: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/3.jpg)
Balanced Binary Search Tree
• Balanced tree: the length of the longest path
is roughly log n
• BALANCE IS IMPORTANT!
3
![Page 4: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/4.jpg)
Complete Binary Tree is Balanced
• Has the smallest height for any binary tree with the same number of nodes
• The longest path guaranteed to be ≤ log n
• => Keep the tree complete
4
![Page 5: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/5.jpg)
Requiring Complete Trees
• However, it is very costly to maintain a
complete binary treeAlex
Abner Angela
Adela Alice
Adam
Abigail
Add to tree5
![Page 6: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/6.jpg)
Requiring Complete Trees
• However, it is very costly to maintain a
complete binary treeAlex
Abner Angela
Adela Alice
Adam
Abigail
6
![Page 7: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/7.jpg)
Height-Balanced Trees
•For each node, the height difference between the left and right subtrees is ≤ 1
3(3)
9(0)1(0)
8(2)2(1)
4(0)
5(1)
6(0)
indicates maximum
height
7
![Page 8: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/8.jpg)
Height-Balanced Trees
• Are locally balanced, but globally (slightly)
unbalanced
3(3)
9(0)1(0)
8(2)2(1)
4(0)
5(1)
6(0)
8
![Page 9: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/9.jpg)
Height-Balanced Trees
• Mathematically, the longest path has been shown to be, at worst, 44% longer than log n
• Algorithms that run in time proportional to the path length are still O(log n)
–Why?9
![Page 10: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/10.jpg)
AVL Trees
• Named after the inventors’ initials:
–Adelson-Velskii and Landis
• Maintain the height balanced property of Binary Search Trees
10
![Page 11: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/11.jpg)
AVL Trees
• Add an integer height field to each node:–Null child has a height of –1–A node is unbalanced when the absolute height difference between the left and right subtrees is greater than one
1 (2)
Node data Height field11
![Page 12: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/12.jpg)
AVL Implementation
struct AVLNode {
TYPE val;
struct AVLNode *left;
struct AVLNode *rght;
int hght; /* Height of node*/
};
12
![Page 13: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/13.jpg)
Get Height
int _height(struct AVLNode *cur)
{
if(cur == 0)
return -1
else return cur->hght;
}
13
![Page 14: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/14.jpg)
Compute Height
void _setHeight(struct AVLNode *cur) {
int lh = _height(cur->left);
int rh = _height(cur->rght);
if(lh < rh)
cur->hght = 1 + rh;
else
cur->hght = 1 + lh;
}
14
![Page 15: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/15.jpg)
Maintaining the Height Balanced Property
• When unbalanced, perform a “rotation” to balance the tree
3(0)
2(1)
1(2)
3(0)1(0)
2(1)
Rotateleft
Unbalancednode
15
![Page 16: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/16.jpg)
Left Rotation1.Input: current2.New top = current's right child
1(0)
2(3)
6(0)
3(0)
4(2)
5(1)
4(?)
Current
New top
16
Rotateleft
![Page 17: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/17.jpg)
Left Rotation1.Input: current2.New top = current's right child3.New top’s new left child = current
1(0)
2(3)
6(0)
3(0)
4(2)
5(1)
2(?)
6(0)
4(?)
5(1)
New top
Current
17
Rotateleft
![Page 18: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/18.jpg)
Left Rotation1.Input: current2.New top = current's right child3.New top’s new left child = current4.Current’s new right child = new top's left child
1(0)
2(3)
6(0)
3(0)
4(2)
5(1) 1(?)
2(?)
6(0)3(?)
4(?)
5(1)
New topCurrent
18
Rotateleft
![Page 19: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/19.jpg)
Left Rotation1.Input: current2.New top = current's right child3.New top’s new left child = current4.Current’s new right child = new top's left child5.Set height of current6.Set height of new top node
1(0)
2(3)
6(0)
3(0)
4(2)
5(1) 1(0)
2(1)
6(0)3(0)
4(2)
5(1)
19
Rotateleft
![Page 20: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/20.jpg)
Left Rotation
1(0)
2(1)
6(0)3(0)
4(2)
5(1)
20
1(0)
2(3)
6(0)
3(0)
4(2)
5(1)
Rotateleft
![Page 21: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/21.jpg)
Right Rotation
2(0)
3(1)
7(0)5(0)
4(2)
6(1)
21
7(0)
6(3)
2(0)
3(1)
4(2)
5(0)
![Page 22: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/22.jpg)
Right Rotation1.Input: current2.New top = current's left child3.New top’s right child = current4.Current’s new left child = new top's right child5.Set height of current6.Set height of new top node
7(0)
6(3)
2(0)
3(1)
4(2)
5(0) 2(0)
3(1)
7(0)5(0)
4(2)
6(1)
22
![Page 23: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/23.jpg)
Double Rotation Left
• A single rotation may not fix the problem:
– When the right child is heavy, i.e.,• its parent is unbalanced• has only a right subtree
2(0)
1(2)
Unbalanced“top” node
3(1)“Heavy”
right child 2(0)
3(2)
1(1)Rotate
left
Doesn’twork!!!
23
![Page 24: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/24.jpg)
Double Rotation Left
• Rotate the child before the regular rotation:1.Rotate the heavy right child to the right 2.Rotate the “top” node to the left
2(0)
1(2)Unbalanced“top” node
3(1)“Heavy”
right childRotate heavy
child right3(0)
2(1)
1(2)
3(0)1(0)
2(1)
Rotate topnode left
24
![Page 25: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/25.jpg)
Double Rotation
• A single rotation may not fix the problem:
– When the left child is heavy, i.e.,• its parent in unbalanced from the left• has only a left subtree
2(0)
1(1)
Unbalanced“top” node 3(2)
“Heavy”left child 2(0)
1(2)
3(1)Rotateright
Doesn’twork!!!
25
![Page 26: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/26.jpg)
Double Rotation Right
• This case requires rotating the child before the regular rotation:1.Rotate the heavy left child to the left 2.Rotate the “top” node to the right
Unbalanced“top” node
“Heavy”left child
Rotate left the heavy
child 1(0)
2(1)
3(2)
3(0)1(0)
2(1)
Rotate topnode right
2(0)
1(1)
3(2)
26
![Page 27: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/27.jpg)
Balancing an Unbalanced NodeIf left child is taller than right child{/* Rotation right */
If left child is heavy{/* Double rotation right*/Rotate left the heavy left child
} Rotate right the node
}else{ /* Rotation left */If right child is heavy {/* Double rotation left */
Rotate right the heavy right child}Rotate left the node
}Return node
27
![Page 28: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/28.jpg)
3(3)
9(0)1(0)
8(2)2(1)
4(0)
5(1)
6(0)
Example: Add 7 to the tree
Add data: 7
Height-Balanced Tree Unbalanced Tree
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(2)
7(0)
6(1)
Added to right sideof heavy left child
“Heavy” leftchild
Unbalanced“top” node
28
![Page 29: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/29.jpg)
Tree Still Unbalanced
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(2)
7(0)
6(1)
Single rightrotation
Example – Suppose We Used Single Rotation
Unbalanced Tree
3(4)
1(0)
2(1)
7(0)
6(1) 9(0)
8(2)
5(3)
4(0)
Unbalanced“top” node(still)
29
![Page 30: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/30.jpg)
Tree Still Unbalanced, but …
Rotate left theheavy left child
Example – Double Rotation Right
“Heavy” leftchild
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(1) 7(0)
6(2)
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(2)
7(0)
6(1)
Unbalanced Tree
30
![Page 31: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/31.jpg)
Tree Now Balanced
Rotate right top node
Example – Double Rotation RightUnbalanced Tree(after 1st rotation)
3(3)
1(0)
2(1)
7(0)
3(4)
9(0)1(0)
8(3)2(1)
4(0)
5(1) 7(0)
6(2) Unbalanced“top” node
4(0)
5(1)
6(2)
9(0)
8(1)
31
![Page 32: CS 261 –Data Structures - College of Engineeringweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261... · 2017-05-17 · CS 261 –Data Structures AVL Trees 1. Binary Search Tree](https://reader034.vdocuments.us/reader034/viewer/2022050122/5f528884918efd0b9a7fa7a1/html5/thumbnails/32.jpg)
Your Turn
• Any questions
• Worksheet:– Start by inserting values 1-7 into an empty AVL tree– Then write code for left and right rotations
32