L4 - Balanced Binary Trees (Moodle Version)
L4 - Balanced Binary Trees (Moodle Version)
5 March 2025
Admin
Quiz 3?
Assignment 2 – include code (4 methods) as appendix
WHAV401 students – challenge coming soon…
Admin: Assignment 1 feedback
What is an academic reflection?
• Opportunity to evaluate yourself, in the context of the module
content/assignment
• Show evidence of learning
• Requires critical thinking – ask yourself:
- Do I understand the content?
- Was my solution a good solution?
- Is there a better solution?
It is not…
• A description of the solution (or problem)
• Module feedback
3. Binary Search Trees: Recap
For each node, n
• childleft < n and
• childright > n
Figure 3.4
3. Binary Search Trees: Recap
h = height of tree
• Best case is h = log n
• Worst case is h = n
find is O(h)
insert is O(h)
remove is O(h)
find is O(h)
insert is O(h)
Figure 3.8
remove is O(h)
Figure 3.9
4. Balanced Binary Search
Trees: Introduction
Need improvement on BST
• Limit height
Figure 3.10
4.1 BBST: Ranks and Rotations
Want the heights of sub-trees to be more or less the same
• i.e. balanced
Rank, r(v) for a node v in binary tree T
• Where r(v) is either the height of T or a value related to the height of
T
• (a, b, c) = in-order
listing of (x, y, z)
• (T0, T1, T2, T3) = in-order a c
listing of subtrees of x, y
&z
• Replace subtree at z T0 T1 T2 T3
with:
4.1 BBST: Ranks and Rotations
Figure 4.3
4.1 BBST: Ranks and Rotations
Figure 4.3
4.1 BBST: Restructure example
relabel:
in-order:
(z, zy,x)y x b, c)
(a,
a b
z label sub-trees rotate y
b a c
y z x
b
T
0
c
x T T T T
a c
T 0 1 2 3
1
T T
T T T T
2 3
0 1 2 3
4.1 BBST: Restructure example
also known as a double rotation
can be thought as 1st rotating x over y then z
relabel:
in-order:
(z, z
x, y) (a,xb, y
c)
a
z label sub-trees rotate b
x
c
y a c
b
T z y
0
b
x
a c
T
3
T T T T
0 1 2 3
T T
1 2
T T T T
0 1 2 3
4.2 BBST: AVL Trees
Rank-based
z= 8 y=
7 8 7 7 0
1 3 1 1 2 2
3 1
5 2
8
1
3 5 8 3 4 7
y= 2 x = 8 z= 8
2 0 8 2 0 8
1 1 1 1
4 1
6 1
insert 49
4
2
6 restructure 4 6 8
x= (x)
9 2 8
8 2 8 1
2
4
sub-tree heights 9
label
from
insertz,
newusing
largest
node
usual
child
to root,
method
y, largest
find 1stchild sub-tree heights
unbalanced
x
4.2 BBST: AVL Trees - Removal
Remove node w, do same for regular binary search tree
• May cause tree to become unbalanced
To repair
• Locate first subtree, starting from parent of removed node w that is
unbalanced
• Label it z
• Choose y as child of z with highest height
• Label a child of y as x as
- The one with the largest height (if not same height); else
- If same height, choose child on same side of y as y was to z
• Restructure by calling restructure(x)
A single restructure may NOT restore the AVL Tree to balanced state
We need to continue to traverse from z to the root and search for additional
unbalanced nodes
4.2 BBST: AVL Trees - Removal
Figure 4.6
4.3/4 BBST: Other tree types
Red-Black
• Colours nodes in the tree as red or black
• Restructures trees based on relationships between nodes’ colours
Weak AVL
• Similar to AVL, but does not use tree-height
• Uses ranks of a node relative to another
• Moves nodes up/down ranks to balance
• Every AVL Tree is a Weak AVL Tree
• Every Weak AVL Tree can be coloured as a red-black tree
4.5 BBST: Splay Trees
Does not keep track of height
Instead performs a splaying operation after each access
• Every time insert, delete or search, perform splaying
• Over time, the tree tends to a balanced state
4.2 BBST: Splay Trees – zig-zig
Node x and parent y both are
left/right children of
grandparent z
Restructure:
• Replace z by x;
• Make y child of x;
• Make z child of y; and
• Preserve in-order
relationships
Figure 4.19
4.2 BBST: Splay Trees – zig-zag
Child x, parent y of
grandparent z. One left child
other right child.
Restructure:
• Replace z by x;
• Make z and y children of x;
and
• Preserve in-order
relationships
Figure 4.20
4.2 BBST: Splay Trees – zig
Child x, parent y, no
grandparent.
• Typically when near root.
Restructure:
• Rotate x over y;
• Make x’s children (one side)
y’s children; and
• Preserve in-order
relationships
Figure 4.21
4.2 BBST: Splay Trees –
operations
After zig-zig and zig-zag
• Decrease depth by 2
After zig
• Decrease depth by 1
Splay operation
• O(d)
• d = depth of tree
in-order (z, y, x) Restructure:
Example z = 14
8
Replace z by x;
Make y child of x;
zig-zig Make z child of y; and
Preserve in-order
3 10 15 10 relationships
y=
8 4 12 17 xx
z== 14
11
T2 zig-zig
3 11 6 y = 12 T
13 y = 15 12
travel to root,
1
4 T1 restructuring
5 7Restructure: zas = move
11 13 T T4xz =17
15
14
T3 T4
6 Replace z by x; 2
zig-zag
T3
Make y child Tof x; in-order
T (y, x, z)
Restructure: y = 13 z = 17
15
5 7 Make z child
Restructure: of y; and T
Restructure: Replace
1
z by2
x;
Preserve in-order
Replace z by relationships
x; T3
ReplaceMakez byz x;and y T
4
MakeMake
z and z and
y y insert 14 T x = 14 17
T
children
children of of
x; x;
and and 1 T
children of x; and T4
Preserve in-order 1
2 T 4
Preserve in-order
Preserve in-order
relationships T 3 T
relationships
relationships
4.2 BBST: Splay Trees – when to
splay
Search(k)
• Find k at x, then Splay(x)
• Else Splay(parent(x))
Insert(k)
• If k inserted at node, x, Splay(x)
Delete(k)
• If k at node x, after deletion
• Splay(parent(x))
Over time, operations are O(log n) – see proof if
interested
Tutorial Questions
Textbook Q’s
• R-4.(1, 3, 4, 6, 11)