MOD007357 Lecture 04 - Trees I
MOD007357 Lecture 04 - Trees I
Date: 08-02-24
𝑒 =𝑛−1
𝑛 =𝑒+1
= 1 if a rooted tree
includes root if
root not alone
variable name
𝑖 internal nodes
𝑛 nodes
𝑣 leaf nodes
𝑛 =𝑖+𝑣
𝑖 =𝑛−𝑣
𝑣 =𝑛−𝑖
1
2 3
4 5 6
7 8 9
variable name
𝑛 nodes
deg + (𝑛) ∈ {0,1,2} 2
2 1
0 2 1
0 0 0
variable name
𝑛 nodes
deg − (𝑛) ∈ {0,1} 0
1 1
1 1 1
1 1 1
∈ ℕ0 0
1 1
2 2 2
3 3 3
variable name
𝑛 nodes
𝑛𝑚𝑎𝑥 (𝑙) = 2𝑙 1 𝑙 level
2 2
4 4 4 4
8 8 8 8 8 8 8 8
variable name
𝑛 nodes
𝑛𝑚𝑖𝑛 𝑙 = 1 (degenerate) 1 𝑙 level
1
variable name
𝑛 nodes
ℎ𝑚𝑎𝑥 (𝑛) = 𝑛 1 ℎ height
2 2
3 3 3
4 4 4
variable name
𝑛 nodes
𝑛𝑚𝑖𝑛 (ℎ) = 2ℎ−1 ℎ height
variable name
𝑛 nodes
𝑛𝑚𝑎𝑥 (ℎ) = 2ℎ − 1 1 ℎ height
2 3
4 5 6 7
8 9 10 11 12 13 14 15
variable name
𝑛 nodes
ℎ height
𝑖 interior modes
𝑛(ℎ) = 2ℎ − 1 1
𝑣 leaves
𝑛 𝑖 = 2𝑖 + 1
𝑛 𝑣 = 2𝑣 − 1 2 3
4 5 6 7
8 9 10 11 12 13 14 15
variable name
𝑛 nodes
𝑖 interior modes
𝑣 𝑖 = 𝑖+1 𝑣 leaves
𝑛+1
𝑣 𝑛 =
2
1 2 3 4 5 6 7 8
variable name
𝑛 nodes
𝑖 interior modes
𝑖 𝑣 = 𝑣−1 1 𝑣 leaves
𝑛−1
𝑖 𝑛 =
2
2 3
4 5 6 7
1. visit root
❶ pre-order 2. traverse left sub-tree
3. traverse right sub-tree
1. traverse left sub-tree
❷ in-order 2. visit root
3. traverse right sub-tree
1. traverse left sub-tree
❸ post-order 2. traverse right sub-tree
3. visit root
dot on left of each node; draw tight fence from left of root.
order: visit, left, right
(F, B, A, D, C, E, G, I, H)
order: left, visit, right
dot on bottom of each node; draw tight fence from left of root.
order: left, visit, right
(A, B, C, D, E, F, G, H, I)
order: left, right, visit
dot on right of each node; draw tight fence from left of root.
order: left, right, visit
(A, C, E, D, B, H, I, G, F)
visit nodes from top to bottom, left to right.
(F, B, G, A, D, I, C, E, H)
order: left boundary,
leaves, right boundary
(F, B, A, C, E, H, I, G)
order: diagonal shells level 0
A
level 1
level 2 B C
D E F
(A, C, F, B, E, G, D)
• In a BST nodes to the left of the current node contain keys
smaller than the current node; nodes to the right of the
current node contain keys larger than the current node.
• One drawback is the need to store two pointers per node (left
and right), rather than the one we needed for a singly linked
list (although the same number as for a doubly linked list!).
• In-order traversal of a BST visits nodes in ascending order:
D P
B N Z
A C O
pre-order (M, D, B, A, C, P, N, O, Z)
in-order (A, B, C, D, M, N, O, P, Z)
post-order (A, C, B, D, O, N, Z, P, M)
root
C < F,
go left
C < F,
go left
C > B,
go right
C < F,
go left
C > B,
go right
C < D,
go left
C < F,
go left
C > B,
go right
C < D,
go left
attach!
root
C < F,
go left
C < F,
go left
C > B,
go right
C < F,
go left
C > B,
go right
C < D,
go left
C < F,
go left
C > B,
go right
C < D,
go left
found!
M
F R
B H P U
A C G I
M
F R
B H P U
A C G I
M
F R
B H P U
A C G I
M
B H P U
A C G I
orphans
M
B R
C P U
A
G I
M
B H P U
A C G I
orphans
M
H R
I P U
G
A C
M
F R
B H P U
A C G I
M
F R
B H P U
A C G I
M
C R
B H P U
A G I
M
F R
B H P U
A C G I
M
G R
B H P U
A C I
• Mark as deleted is fastest; we need to traverse the tree to the
item to be deleted and then set a flag to signify that it is an
inactive node. It also supports undelete.
Successful:
best average worst
insert 1 log2(𝑛) 𝑛 (degenerate)
delete 1 log2(𝑛) 𝑛 (degenerate)
search 1 log2(𝑛) 𝑛 (degenerate)
Unsuccessful:
best average worst
insert N/A N/A N/A
delete 1 log2(𝑛) 𝑛 (degenerate)
search 1 log2(𝑛) 𝑛 (degenerate)
• The performance of a BST with 𝑛 nodes in units of attempts, is:
Successful:
best average worst
insert 𝑂(1) 𝑂(log 𝑛 ) 𝑂(𝑛)
delete 𝑂(1) 𝑂(log 𝑛 ) 𝑂(𝑛)
search 𝑂(1) 𝑂(log 𝑛 ) 𝑂(𝑛)
Unsuccessful:
best average worst
insert N/A N/A N/A
delete 𝑂(1) 𝑂(log 𝑛 ) 𝑂(𝑛)
search 𝑂(1) 𝑂(log 𝑛 ) 𝑂(𝑛)
• We will continue of consideration of trees and a closely
related data structure, the binary heap.