Chapter 5
Chapter 5
Trees
Yi-Fen Liu
Department of IECS, FCU
References:
- E. Horowitz, S. Sahni and S. Anderson-Freed, Fundamentals of Data Structures (2nd Edition)
- Slides are credited from Prof. Chung, NTHU
Outline
• Introduction
• Binary Trees
• Binary Tree Traversals
• Additional Binary Tree Operations
• Threaded Binary Tree
• Heaps
• Binary Search Trees
• Selection Trees
• Forest
Yi-Fen Liu, FCU IECS Data Structures (Ch5) - 2
INTRODUCTION
Introduction (1)
• A tree structure means that the data are
organized so that items of information are
related by branches
• Examples
Data Structures
Introduction (3)
• Definition (recursively): A tree is a finite set of one
or more nodes such that
– There is a specially designated node called root
– The remaining nodes are partitioned into n≧0 disjoint
set T1,…,Tn, where each of these sets is a tree
– T1,…,Tn are called the subtrees of the root
• Every node in the tree is the root of some subtree
n2
L ptr
V
R
output: + * * / A B C D E
V
L
R
L
R
V
L
V
output: A / B*C * D + E
node
Binary Tree Traversals (7)
• Analysis of inorder (Non-recursive Inorder traversal)
– Let n be the number of nodes in the tree
– Time complexity: O(n)
• Every node of the tree is placed on and removed
from the stack exactly once
– Space complexity: O(n)
• Equal to the depth of the tree which
(skewed tree is the worst case)
FIFO
ptr
ADDITIONAL BINARY TREE
OPERATIONS
Additional Binary Tree Operations
– Copying Binary Trees
• Modify the postorder traversal algorithm only slightly to
copy the binary tree
Additional Binary Tree Operations
– Testing Equality
– Binary trees are equivalent if they have the same topology
and the information in corresponding nodes is identical
V
L
R
data
value X3
X1 X3
X2 X1
Satisfiability Problem (4)
• Node structure
– For the purpose of our evaluation algorithm, we assume each
node has four fields
– We define this node structure in C as:
TRUE
FALSE
FALSE
TRUE T
TRUE
FALSE F FALSE
T TRUE TRUE
FALSE F
Data Structures T TRUE 41
THREADED BINARY TREES
Threaded Binary Trees (1)
• Threads
– The drawback of the binary tree
• Too many null pointers in current representation of
binary trees
– n: number of nodes
• number of non-null links: n-1
• total links: 2n
• null links: 2n-(n-1) = n+1
– Solution: replace these null pointers with some
useful “threads”
D t E t F G
dangling
inorder traversal:
H I
H D I B E A F C G
Data Structures (Ch5) - 45
Threaded Binary Trees (4)
• Two additional fields of the node structure,
left_thread and right_thread
– If ptr->left_thread = TRUE, then ptr->left_child contains a thread
– Otherwise it contains a pointer to the left child
– Similarly for the right_thread
Threaded Binary Trees (5)
• If we don’t want the left pointer of H and the right pointer of
G to be dangling pointers, we may create root node and
assign them pointing to the root node
Inorder
A parent
root child
B
X
A parent C
B
D
C X child E F
14 10 2 5 58
Heaps (6)
• Deletion from a max heap
– After deletion, the heap
is still a complete binary
tree
– Analysis of
delete_max_heap
• The complexity of the
insertion function
is O(log2 n)
parent = 41
2
*n= 5
4
child = 8
2 [1]
4
<
15
20
[2] [3]
15
14 2
[4] [5]
item.key = 20
10 10
14 temp.key = 10
BINARY SEARCH TREES
Binary Search Trees (1)
• Why do binary search trees need?
– Heap is not suited for applications in which arbitrary
elements are to be deleted from the element list
• A min (max) element is deleted O(log2n)
• Deletion of an arbitrary element O(n)
• Search for an arbitrary element O(n)
• Definition of binary search tree
– Every element has a unique key
– The keys in a nonempty left subtree (right subtree) are
smaller (larger) than the key in the root of subtree
– The left and right subtrees are also binary search trees
medium
smaller larger
17 88
32 65 97
28 54 82
29 76
80
O(h)
An empty tree
Binary Search Trees (7)
• Deletion from a binary search tree
– Three cases should be considered
– case 1. leaf delete
– case 2.
one child delete and change the pointer to this child
– case 3. two child
either the smallest
element in the right
subtree or the
largest element in
the left subtree
Binary Search Trees (8)
• Height of a binary search tree
– The height of a binary search tree with n elements can
become as large as n
– It can be shown that when insertions and deletions are
made at random, the height of the binary search tree is
O(log2n) on the average
– Search trees with a worst-case height of O(log2n) are called
balance search trees
Winner Tree
ordered sequence
Selection Trees (4)
• Analysis of merging runs using winner trees (k is the
number of sequence)
– # of levels: log2K +1 restructure time: O(log2K)
– Merge time: O(nlog2K)
– Setup time: O(K)
• Slight modification: tree of loser
– Consider the parent node only (v.s. sibling nodes)
15
Selection Trees (6)
0
6 8
Tree of losers
can be conducted
by Winner tree 9 17
10 20 9 90
Selection Trees (7)
• The loser tree after output 6
0
8 Overall
winner
1
9
2 3
15 17
4 5 7
6
10 20 9 90
9 10 11 12 13 14 15
10 9 20 15 8 9 90 17
run 1 2 3 4 5 6 7 8
FORESTS
Forests
• Definition
– A forest is a set of n ≥ 0 disjoint trees
• When we remove a root from a tree, we’ll get a
forest
– Removing the root of a binary tree will get a forest of two
trees
A E G
B C D F H I
Three-tree forest
Data Structures (Ch5) - 79
Transforming A Forest Into
A Binary Tree (1)
• T1, …, Tn
– Forest of trees
• B(T1, …, Tn)
– Binary tree
• Algorithm
– Is empty if n = 0
– Has root equal to root(T1)
– Has left subtree equal to B(T11, …, T1m)
• T11, …, T1m are the subtree of root(T1)
– Has right subtree equal to B(T2, …, Tn)
B C D
B E
E C F G
G
D H
F H I
6 7 8 1 9 3 5
Set I = {0 , 6 ,7 ,8 } Set 3 = {2 , 3 , 5}
S1 S2
0 4
S2
6 7 8 4 1 9
1 9
S1 S2
0 4
S1
6 7 8 0 1 9
6 7 8
Set
Pointer 0 4 2
Name
S1
S2 6 7 8 1 9 3 5
S3
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 -1 2 0 0 0 4
Root
EX: Find1( 5 ) ;
int Find1( int i )
i =2
{
for(;parent[i] >= 0 ; i = parent[i]) ;
return i ;
} Data Structures (Ch5) - 90
Analysis Union/Find Operations
• For a set of n elements each in a set of its own, then
the result of the union function is a degenerate tree
• The time complexity of the following union-find
operation is O(n2) n-1
• The complexity can be improved by using
weighting rule for union n-2
union(0, 1), find(0) Union operation
union(1, 2), find(0) O(n)
Find operation
i j
11 1
2 23
0 2 4 6
1 3 5 7
0 4 0
1 2 5 6 1 2 4
3 7 3 5 6
[-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1]
0 1 2 3 4 5 6 7 8 9 10 11
(a) Initial trees
4 1 10 9
(b) Height-2 trees following 0≡4, 3≡1, 6≡10, and 8≡9
4 1 10 9
(b) Height-2 trees following 0≡4, 3≡1, 6≡10, and 8≡9
[-3] [-4] [-3] [-2]
0 6 3 2
4 7 10 8 1 5 11
9
(c) Tree following 7≡4, 6≡8, 3≡5, and 2≡11
Example (3)
0 6 3
4 7 2 10 8 1 5
11 9
(d) Tree following 11≡0
B, C E, D, G, H, F, I
preorder: A B C (D E F G H I)
inorder: B C A (E D G H F I)
A
B D
C E F, G, H, I
Stack permutation (3)
• The number of distinct permutations obtainable by
passing the numbers 1 through n through a stack is
equal to the number of distinct binary trees with n
nodes
• Example
– If we start with the numbers 1 , 2 , and 3 , then the
possible permutations obtainable by a stack are
(1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,2,1)
1 1 1 1 1
2 2 2 3 2 2
3 3 3 3
bn
bi bn-i-1