0% found this document useful (0 votes)
5 views75 pages

MOD007357 Lecture 04 - Trees I

The lecture covers binary trees, including their terminology, traversal methods, and the operations of Binary Search Trees (BSTs) such as insertion, searching, and deletion. It discusses the performance of BSTs compared to linked lists, the advantages and disadvantages of different deletion methods, and the complexities involved in these operations. The lecture concludes with a preview of binary heaps and Huffman trees for data compression.

Uploaded by

SHORTFLIX
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views75 pages

MOD007357 Lecture 04 - Trees I

The lecture covers binary trees, including their terminology, traversal methods, and the operations of Binary Search Trees (BSTs) such as insertion, searching, and deletion. It discusses the performance of BSTs compared to linked lists, the advantages and disadvantages of different deletion methods, and the complexities involved in these operations. The lecture concludes with a preview of binary heaps and Huffman trees for data compression.

Uploaded by

SHORTFLIX
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Rev.

Date: 08-02-24

Lecture 04: Trees I


Ian van der Linde, PhD
• We will look at a very common data structure that has some
very nice properties: the binary tree.

• We will consider terminology related to binary trees, and


some fundamentals formulas that help us to understand
their structure.

• We will look at methods for traversing binary trees

• We will then look at the Binary Search Tree and its


operations: insert, search and delete.

• In particular, we will consider a number of alternative


approaches to deletion and compare their relative merits.
variable name
𝑒 edges
𝑛 nodes

𝑒 =𝑛−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

ℎ𝑚𝑖𝑛 (𝑛) = log 2 (𝑛 + 1)

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

visit nodes on each level, working left-to-right, top-to-


❹ level-by-level bottom, starting from the root.

1. visit left boundary


❺ boundary 2. visit leaves
3. visit right boundary

visit nodes on each diagonal, working down and to the


❻ diagonal right, starting from the root.
order: visit, left, right

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.

• BSTs have faster insert, delete and search performance than


linked lists on average, because the number of hops from
root to any node is related to tree height rather than linearly
related to the number of nodes as it was for a linked list. In
the average case, tree height is a logarithmically related to 𝑛.

• 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.

• However, it is not space efficient and also the performance of


the tree will deteriorate over time as it will be larger than
necessary.

• Re-attach orphans is not a good idea as it is likely to increase


the height of the tree, which will detrimentally affect its
performance.

• Promote a leaf is the best, overall as it minimises tree height.


• The performance of a BST with 𝑛 nodes in units of attempts, is:

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.

• We will look at min heaps, max heaps and the processes


required to build and maintain them.

• We will also look at Huffman trees; a type of tree that is


used to develop efficient data compression schemes,
rather than for data storage per se.
END

You might also like