Lecture 6 - Algorithm trees II
Lecture 6 - Algorithm trees II
Computer Science
COMP-370
Algorithms
Lecture 6
Trees II
Spring Semester
2022
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 1 1
[email protected]
Department of
Computer Science
Lecture Overview
• Balanced Trees
• AVL Trees
• Properties
• Maintaining Balance
• Data on Disk
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 2 2
[email protected]
Department of
Computer Science
Balanced Tree
• A tree where its leafs are ALL at the same height.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 6 6
[email protected]
Department of
Computer Science
Exercise
• Which one is an AVL tree an why?
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 7 7
[email protected]
Department of
Computer Science
Exercise
• Is the following an AVL tree?
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 8 8
[email protected]
Department of
Computer Science
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 9 9
[email protected]
Department of
Computer Science
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 10 10
[email protected]
Department of
Computer Science
AVL Trees
• All operations are now guaranteed to be of O(logn).
• The AVLNode:
• key
• height
• Left subtree pointer
• Right subtree pointer
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 12 12
[email protected]
Department of
Computer Science
Node Insertion
• The insertion of a node happens as in BSTs but
we keep trace of the path followed.
Node Insertion
• Insert the values 72, 26, 9 to an empty AVL tree:
To avoid violation,
ideally, we would like
to have this.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 14 14
[email protected]
Department of
Computer Science
Left Rotation
• Insertion into the left subtree of the left child.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 15 15
[email protected]
Department of
Computer Science
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 16 16
[email protected]
Department of
Computer Science
Exercise
• Insert 6 to the following AVL tree and make sure
balance condition is preserved:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 17 17
[email protected]
Department of
Computer Science
Exercise
• The following tree is imbalanced. Find where the misbalancing
occurs and fix the problem.
A
Imbalanced
B ℎ𝑙𝑒𝑓𝑡 = 3
C
ℎ𝑟𝑖𝑔ℎ𝑡 = 1
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 18 18
[email protected]
Department of
Computer Science
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 19 19
[email protected]
Department of
Computer Science
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 20 20
[email protected]
Department of
Computer Science
Exercise
• Starting from an initial empty AVL tree, insert the items
3, 2, 1, 4, 5, 6, 7:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 21 21
[email protected]
Department of
Computer Science
Exercise
Next issue comes when we insert 5 with node 3 right height being 2 and left 0.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 22 22
[email protected]
Department of
Computer Science
Exercise
Next issue comes at the root when we insert 6.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 23 23
[email protected]
Department of
Computer Science
Exercise
Finally the insertion of 7 causes another issue:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 24 24
[email protected]
Department of
Computer Science
Double Rotations
• An insertion to right subtree of left child and an insertion
to left subtree of the right child require two rotations.
• These are the two cases where the insertion occurs on the
“inside” (i.e., left–right or right–left).
Still… imbalanced
Adding to Y cannot be
fixed with single rotation.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 25 25
[email protected]
Department of
Computer Science
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 26 26
[email protected]
Department of
Computer Science
2. k3.left = k2.right
3. k2.left = k1
4. k2.right = k3
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 27 27
[email protected]
Department of
Computer Science
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 28 28
[email protected]
Department of
Computer Science
2. k1.right = k2.left
3. k2.left = k1
4. k2.right = k3
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 29 29
[email protected]
Department of
Computer Science
Exercise
• Insert in the following AVL tree the values 16, 15, 14,
13, 12, 11, 10 , 8 and 9:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 30 30
[email protected]
Department of
Computer Science
Exercise
• Inserting 15 causes a height imbalance at node 7:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 31 31
[email protected]
Department of
Computer Science
Exercise
• Inserting 14 causes a double rotation:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 32 32
[email protected]
Department of
Computer Science
Exercise
• Inserting 13 causes a imbalance at the root but single
rotation is enough:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 33 33
[email protected]
Department of
Computer Science
Exercise
• Inserting 12 causes imbalance but single rotation is
enough:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 34 34
[email protected]
Department of
Computer Science
Exercise
• To insert 11, a single rotation needs to be performed, and the
same is true for insertion of 10. We insert 8 without a rotation
creating an almost perfectly balanced tree:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 35 35
[email protected]
Department of
Computer Science
Data on Disk
• Up to now, we have assumed entire data (structure) can fit
in memory -> unrealistic.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 36 36
[email protected]
Department of
Computer Science
Data on Disk
• Since disk I/Os are slow… we must reduce disk I/Os.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 37 37
[email protected]
Department of
Computer Science
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 38 38
[email protected]
Department of
Computer Science
M-ary Trees
• For a binary tree we need 1 key to decide which of two
branches to follow.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 39 39
[email protected]
Department of
Computer Science
B-trees
• A B-tree of order M is an M-ary tree with these properties:
• The data items are stored at leaves.
• The non-leaf nodes store up to 𝑀 − 1 keys to guide the searching
and key 𝑖 represents the smallest key in subtree 𝑖 + 1.
• The root is either a leaf or has between two and M children.
• All non-leaf nodes (except the root) have between ⌈𝑀/2⌉ and 𝑀
children.
• All leaves are at the same depth and have between ⌈𝐿/2⌉ and 𝐿
data items.
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 40 40
[email protected]
Department of
Computer Science
B-tree of order 5
B-tree
• Usually, each node represents one disk block that needs 1
disk I/O to be accessed.
• M and L are chosen based on items that can fit disk block.
B-tree Insertion
• Insert 57 to B-tree:
57
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 43 43
[email protected]
Department of
Computer Science
B-tree Insertion
• Insert 55 to B-tree
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 44 44
[email protected]
Department of
Computer Science
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 45 45
[email protected]
Department of
Computer Science
B-tree Insertion
• Insert 55 to B-tree:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 46 46
[email protected]
Department of
Computer Science
B-tree Insertion
• Insert 40 to B-tree:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 47 47
[email protected]
Department of
Computer Science
B-tree Insertion
• Insert 40 to B-tree:
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 49 49
[email protected]
Department of
Computer Science
COMP-370
Algorithms
Questions?
02/03/2022
Demetris Trihinas
Lecture 6 | Trees II 50 50
[email protected]