ch05 Trees Nosolution
ch05 Trees Nosolution
Chapter 5: Trees
5-1
Pedigree Genealogical Chart
血統、家譜 宗譜的、家系的
Chery
l
Kevin Rosemar
y
Proto Indo-European
Osco Umbrian Spanis French Italian Icelandic Norwegia Swedish Low High Yiddish
h n
5-5
Tree Terminologies (1)
• degree ( 分支度 ) of a node:
number of subtrees of the node.
• degree of a tree:
maximum degree of the nodes in the tree.
• leaf (terminal) node:
a node with degree zero A
• Siblings (brothers): B C D
K L M
5-6
Tree Terminologies (2)
• ancestors of a node:
all the nodes along the path from
the root to the node. A
• descendants of a node: B C D
all the nodes of its subtrees. E F G H I J
• level of a node:
K L M
the level of the node’s parent plus one.
Here, the level of the root is 1.
• height (depth) of a tree:
the maximum level of the nodes in the tree.
5-7
List Representation of a Tree
The tree is represented as a list:
A
(A (B (E (K, L), F), C(G), D(H (M), I, J)))
B C D
E F G H I J
K L M
A 0
B F 0 C G 0 D I J 0
E K L 0 H M 0
5-8
Representation of Trees
• Left child-right sibling tree
– two links (or pointers): left child and right
sibling
data
A
left child right sibling
B C D
A
B C D
E F G H I J
E F G H I J
K L M
K L M
5-9
Binary Trees
A A A
B C B
B C
D E F
C
D E F G
G H I
D
Binary tree H I
E
• A binary tree:
– a finite set of nodes that is either empty, or
consists of a root and two disjoint binary trees
called the left subtree and the right subtree.
• In a binary tree, we distinguish between the
order of the children; in a tree we do not.
A A
B B
2 3
4 5 6 7
5-12
8 9 10 11 12 13 14 15
Complete Binary Tree
• A complete binary tree with n nodes and
depth k is that its nodes correspond to the
nodes numbered from 1 to n in the full
binary tree of depth k.
• It can be represented by an array.
1
• Root is at a[1]. a[0] is not used.
2 3
i
parent(i)= 2
4 5 6 7
** left_child(i)=
right_child(i)= 8 9
5-13
Linked Representation of Binary Trees
data
leftChild rightChild
5-14
Linked Representation of Binary Trees
5-15
Linked Representation of a Binary Tree
A
root
B C
A
D E F G
B C
H I
D 0 E 0 0 F 0 0 G 0
0 H 0 0 I 0
5-16
Binary Tree Traversal
• Preorder traversal:
1. root
2. left subtree
3. right subtree
• Inorder traversal:
1. left subtree
2. root
3. right subtree
• Postorder traversal:
1. left subtree
2. right subtree
3. root
5-17
Arithmetic Expression Trees
• Preorder Traversal: **
=> Prefix expression
• Inorder Traversal:
+
=> Infix expression
• Postorder Traversal: * E
=> Postfix expression
* D
/ C
A B
5-18
Preorder Traversal
template <class T>
void Tree <T>::Preorder()
{//Driver calls workhorse for traversal of entire tree
Preorder(root);
}
+
* E
* D
/ C
A B
+*E*D/CAB 5-23
Threaded Binary Tree 引線二元樹
• Threading Rules
– A 0 rightChild field at node p is replaced by a
pointer to the node that would be visited after p
when traversing the tree in inorder. That is, it is
replaced by the inorder successor of p.
– A 0 leftChild link at node p is replaced by a
pointer to the node that immediately precedes
node p in inorder (i.e., it is replaced by the
inorder predecessor of p).
5-24
Threaded Binary Tree
A
B C
A
D E F G
B C
H I
D E F G
H I
Inorder sequence: HDIBEAFCG 5-25
A
Memory Representation of Threaded Tree
x->rightThread == TRUE B C
=> x->rightChild is a thread D E F G
(pointer to inorder successor)
x->rightThread == FALSE H I
=> x->rightChild is a pointer Inorder sequence: H D I B E A F C G
to the right child. f - f
A header is
added as the f A f
virtual root
f B f f B f
f D f t E t t D t t E t
u u
s s
r r
s s
p r r
q p
q
before after 5-29
Inserting r as the Right Child of s
5-30
Priority Queue
• Maximum (minimum) finding: In a priority
queue, the element to be deleted is the one
with highest (or lowest) priority. It is easy
to get the maximum (minimum).
• Insertion: An element with arbitrary
priority can be inserted into the queue
according to its priority.
• max (min) priority queue: a data structure
supports the above two operations.
5-31
14 9 30
12 7 6 3 25
10 8 6 5 Max heaps
2 10 11
7 4 20 83 21
10 8 6 50 Min heaps
5-32
Max (Min) Heaps
• A max (min) heap is a complete binary tree in
which the key value in each node is no smaller
(larger) than the key values in its children (if any).
• Heaps are frequently used to implement priority
queues.
• Time complexity of a max (min) heap with n
nodes:
– Max (min) finding: O(1)
– Insertion: O(log n)
– Deletion: O(log n)
5-33
Insertion into a Max Heap
20 20
(a) (b)
15 2 15 2
14 10 14 10
(b)(c1) 20 21 (b)(c2)
Insert 5 Insert 21
15 5 15 20
5
14 10 2 14 10 2 5-34
Deletion from a Max Heap
(a) 21 (b) 2
15 20 15 20
14 10 2 14 10 2
(c) 2 (d) 20
15 20 15 2
14 10 14 10 5-35
Deletion from a Max Heap (Cont.)
(e) 20 10
(f)
15 2 15 2
14 10 14 10
(g) 10 (i) 15
15 2 14 2
14 10 5-36
Binary Search Trees
• A binary search tree is a binary tree. It may
be empty. If it is not empty, then it satisfies
the following properties:
– Every element has a key and no two elements
have the same key (i.e., the keys are distinct)
– The keys (if any) in the left subtree are smaller
than the key in the root.
– The keys (if any) in the right subtree are larger
than the key in the root.
– The left and right subtrees are also binary
search trees.
5-37
Binary Trees
20 30 60
15 25 5 40 70
14 10 22 65 80
2
2 35 80
5-39
Searching a Binary Search Tree
5-40
Insertion into a Binary Search Tree
30 30
5 40 5 40
2 (1) Insert 80 2 80
30
(2) Insert 35
5 40
Time complexity: O(h)
h: tree height
2 35 80
5-41
Deletion from a Binary Search Tree
30 30
5 40 Delete 35 5 40
2 35 80 2 80
30 5
Delete 30
5 40 5 40 2 40
2 80 2 80 80
5-42
Deletion from a Binary Search Tree
• Deletion of a node x:
– x is a leaf node: delete x directly.
– x has one child: move up the position of child to
x.
– x has two children: replace x with either inorder
successor (smallest in the right subtree, no left
child) or inorder predecessor (largest in the left
subtree, no right child).
• Time complexity: O(h), h:tree height
5-43
Selection Trees
• Goal: merge ( 合併 ) k ordered sequences (called
runs) in nondecreasing into a single ordered
sequence.
• Straightforward method: perform k – 1
comparisons each time to select the smallest one
among the first number of each of the k ordered
sequences.
• Better method: winner tree k=8
10 9 20 6 8 9 90 17
15 20 20 15 15 11 95 18
16 38 30 25 50 16 99 20
28
run1 run2 run3 run4 run5 run6 run7 run8
5-44
Winner Tree for k = 8
1
6
2 3
6 8
4 5 6 7
9 6 8 17
8 9 10 11 12 13 14 15
10 9 20 6 8 9 90 17
15 20 20 15 15 11 95 18
16 38 30 25 50 16 99 20
28
1
8
2 3
9 8
4 5 6 7
9 15 8 17
8 9 10 11 12 13 14 15
10 9 20 15 8 9 90 17
15 20 20 25 15 11 95 18
16 38 30 28 50 16 99 20
5-47
1
Winner Tree 6
2 3
6 8
4 5 7
6
9 6 8 17
8 9 10 11 12 13 14 15
10 9 20 6 8 9 90 17
6 Overall
0 winner
(6,8)
Loser Tree (6,9)
1
8 (8,17)
2 3
9 17
(9,10) (6,20) (8,9) (17,90
4 5 ) 7
6
10 20 9 90
9 10 11 12 13 14 15
10 9 20 6 8 9 90 17
5-48
run 1 2 3 4 5 6 7 8
Loser Tree
• Loser tree: A selection tree in which each
nonleaf node retains a pointer to the loser.
• Each leaf node represents the first record of
each run.
• An additional node, node 0, has been added
to represent the overall winner of the
tournament.
5-49
Forests
• Forest: a set of n ≥ 0 disjoint trees.
• If we remove the root of a tree, we obtain a
forest.
– E.g., removing the root of a binary tree
produces a forest of two trees.
A E G
B C D F H I
5-50
Representing a Forest with a Binary Tree
• leftChild=first child
• rightChild=next sibling
A forest of A E G
3 trees B C D F H I
A
A forest is represented B E
by a binary tree.
C F G
D H
I 5-51
Constructing a Binary Tree from Its
Inorder Sequence and Preorder Sequence
B, C D, E, F, G, H, I B D, E, F, G, H, I
C
5-52
Number of Distinct Binary Trees
n = 2, b2=2 n = 3, b3=5
• Number of distinct binary trees with n nodes:
n 1
bn bi bn i 1 , n 1 , and b0 1, b1 1
i 0
bn
bi bn-i-1 5-53
Generating function, let B ( x) bi x i
i 0
5-57