Lecture 09 Tree II 20230309
Lecture 09 Tree II 20230309
Data Structures
Lecture 9
Tree II
Learning Objectives:
3
Binary Search Tree
4
Key-value pair and Search Tree
5
Definition Of Binary Search Tree
• A binary tree.
• Each node has a (key, value) pair.
• Nodes are insert to the tree according to the key.
• For every node x, all keys in the left subtree of x
are smaller than the key in x.
• For every node x, all keys in the right subtree of
x are greater than the key in x.
• No duplicate nodes.
Binary Search Trees
Key property
Key at node
Smaller values in left subtree
Larger values in right subtree
Example
X>Y
X
X<Z
Y Z
https://www.youtube.com/watch?v=mtvbVLK5xDQ
(00:00 – 02:00) 7
Binary Search Trees
Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary Non-binary
search trees search tree
8
Example Binary Search Tree
20
10 40
6 15 30
2 8 25
11
12
Key operations of BST
• get(k)
– get the value v for a given key k
• put(k, v)
– add a new (k, v) to a BST
• remove(k)
– delete a node with key k from BST
13
The Operation get()
20
10 40
6 15 30
2 8 25
14
class Node:
The Operation get()
def __init__(self, key, data): left key, data right
self.left = None
self.right = None
self.key = key
self.data = data
10 40
6 15 30
2 8 25 35
10 40
6 15 30
2 8 25 35
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
19
The Operation put()
def put(self, key, data):
# Compare the new value with the parent node
if self.key:
if key < self.key:
if self.left is None:
self.left = Node(key, data)
else:
self.left.put(key, data)
elif key > self.key:
if self.right is None:
self.right = Node(key, data)
else:
self.right.put(key, data)
else:
self.key = key
self.data = data
20
Exercise
21
The Operation remove()
Three cases:
▪ Element is in a leaf.
▪ Element is in a degree 1 node.
▪ Element is in a degree 2 node.
22
Remove From A Leaf
20
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
7
Replace with predecessor (largest key in left subtree of the deleted
node) or successor (smallest in right subtree of the deleted node).
28
Remove From A Degree 2 Node
20
10 40
6 15 30
18 25 35
2 8
8 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
18 25 35
2 8
10 40
6 15 30
2 8 25 35
7
Remove from a degree 2 node. key = 10
Replace with successor (smallest in right subtree of the deleted node).
35
Remove From A Degree 2 Node
18
10 40
6 15 30
2 8 25 35
15 40
6 15 30
2 8 25 35
38
Remove()
def remove(self, root, key):
# Base Case
if root is None:
return root
39
Remove()
return root
41
4
2
42
Balanced Binary Search Trees
43
AVL Tree
• binary tree
• for every node x, define its balance factor
balance factor of x = height of right subtree of x
- height of left subtree of x
• balance factor of every node x is -1, 0, or 1
Note: In some texts (e.g. the textbook by Sahni), the balance factor is computed
as follows:
44
Balance Factors
1
-1 -1
0 -1 1
0
0
0 0 1 0
-1 -1
7 40
0 -1 1
0 45
3 8 30
0
0 0 1 0 60
35
1 5 20
0
25
46
put(9)
1
10
0 -1 -1
7 40
0 -1 1
0 1 45
3 8 30
0
0 0 0 1 0 60
35
1 5 9 20
0
25
47
put(29)
1
10
-1 -1
7 40
0 -1 1
0 45
3 8 30
0
0 0 2 1 35
0 60
1 5 20
0 1
RR imbalance => new node is in 25
right subtree of right subtree of 0
-1 -1
7 40
0 -1 1
0 45
3 8 30
0
0 0 0 0 60
35
1 5 25
0 0
20 29
RR rotation. 49
AVL Rotations
50
AVL Rotations
6 0
51
AVL Rotations
52
AVL Rotations
-2 -2
0
20 20 18
Left Rotate Right Rotate 0
1 -1 0
16 18 16 20
0 0
18 16
https://www.youtube.com/watch?v=yAFLlCZFJy0
53
Exercise
54
Basic AVL Rotations
(in presence of subtrees underneath)
z z
Rotate at x right Rotate at y
y x
left
x y
C A
A B B C
56
Exercise
57
RL imbalance
58
LR imbalance
Left rotation
26 15
Right rotation
15 06 26
06
59
Summary
60