Professional Documents
Culture Documents
Lect5 treeII
Lect5 treeII
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•An Object-Oriented Representation
•Counting Binary Tree
12 25 5 40 15 25
14 10 22
10 15 22 2 Binary search
Not binary
trees
search tree
J.-J. Chen, EE NTUST Tree II-3
Recursive Search of a BST
Complexity: O(h)
where h is the height of the BST
non-leaf
40 40
node
20 60 20 55
10 30 50 70 10 30 50 70
45 55 45 52
52
20
10 40
6 15 30
2 8 18 25 35
10 40
6 15 30
2 8 18 25 35
10 40
6 15 30
2 8 18 25
10
6 15 30
2 8 18 25
10 40
6 15 30
2 8 18 25 35
7
• Which nodes have a degree 2?
• Example: Delete key=10
10 40
6 15 30
2 8 18 25 35
7
• Replace with the largest key in the left subtree
(or the smallest in the right subtree)
• Which node is the largest key in the left subtree?
8 40
6 15 30
2 8 18 25 35
10 40
6 15 30
2 8 18 25 35
10 40
6 15 30
2 8 25 35
30
10 40
2 11 35 80
1 3 15 while (t)
if (k < t →data.first){ // 加入到 big
b →leftChild = t;
b = t; t = t →leftChild;
}
else if (k > t →data.first) { // 加入到 small
s →rightChild = t;
s = t; t = t →rightChild;
}
else { // 在 t 做分割
s →rightChild = t →leftChild;
b →leftChild = t →rightChild;
J.-J. Chen, EE NTUST Tree
small.root = sHead →rightChild; delete II-28
sHead;
Splitting a BST (3)
k = 10 = t.data
sHead
bHead
b
s
t
30
while (t)
10 if (k <40t →data.first){ // 加入到big
b →leftChild = t;
b = t; t = t →leftChild;
2 11 35} 80
else if (k > t →data.first) { // 加入到 small
s →rightChild = t;
s = t; t = t →rightChild;
1 3 15 }
else { // 在 t 做分割
s →rightChild = t →leftChild;
b →leftChild = t →rightChild;
small.root = sHead →rightChild; delete sHead;
big.root = bHead →leftChild; delete bHead;
mid = new pair<K, E >(t →data.first, t →data.second);
delete t;
return;
}
J.-J. Chen, EE NTUST Tree II-29
Splitting a BST (4)
big.root
sHead
k = 10 = t.data
bHead
b
s
t
30
small.root 10 40
while (t)
2 11 35 80 // 加入到big
if (k < t →data.first){
b →leftChild = t;
b = t; t = t →leftChild;
1 3 15 }
else if (k > t →data.first) { // 加入到 small
s →rightChild = t;
s = t; t = t →rightChild;
}
else { // 在 t 做分割
s →rightChild = t →leftChild;
b →leftChild = t →rightChild;
small.root = sHead →rightChild; delete sHead;
big.root = bHead →leftChild; delete bHead;
mid = new pair<K, E >(t →data.first, t →data.second);
J.-J. Chen, EE NTUST delete t; Tree II-30
Chapter 5
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•An Object-Oriented Representation
•Counting Binary Tree
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
1
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
ordered sequence
15 20 20 15 15 11 95 18
16 38 30 25 50 16 99 20
28
15 20 20 25 15 11 95 18
16 38 30 28 50 16 99 20
0
6 Overall
winner
1
8
2 3
9 17
4 5 7
6
10 20 9 90
9 10 11 12 13 14 15
10 9 20 6 8 9 90 17
run 1 2 3 4 5 6 7 8
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
A E G
B C D F H I
B C D F H I C F G
D H
• Definition
– T1, T2, …, Tn: a forest of trees I
B(T1, T2, …, Tn): a binary tree corresponding to this forest
• Algorithm
1. empty, if n = 0
2. has root equal to root(T1);
has left subtree equal to B(T11,T12,…,T1m);
has right subtree equal to B(T2,T3,…,Tn)
E C
B C D
F G D
E F J
G H I
H
I
B C
preorder
D
E G H
J
F I
J
J.-J. Chen, EE NTUST Tree II-44
Chapter 5
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•An Object-Oriented Representation
•Counting Binary Tree
0 1 2
6 7 8 4 9 3 5
Si Sj =
• Two operations considered here
– Disjoint set union S1 S2={0,6,7,8,1,4,9}
– Find(i): Find the set containing the element i.
3 S3, 8 S1
0 1
8 1 0 4 9
6 7
4 9 6 7 8
3 5
• Array indicating a node’s parent
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 -1 2 0 0 0 4
• Improvement
– The complexity can be improved by using weighting rule
for union.
https://www.youtube.com/watch?v=ibjEGG7ylHk
degenerate tree
J.-J. Chen, EE NTUST Tree II-51
Weighted Rule for Union (i,j)
• Definition
– If the # of nodes in the tree with root i is less than the # in
the tree with root j, then make j the parent of i; otherwise
make i the parent of j.
– 大者為樹根
• Data Structure
– A count field in the root node is used to indicate the total #
of elements in the tree
1 1 2
0 4 n-1 0
1 2 3
1 2 3 n-1
Initially,
parent[k] = -1
for all k
• Lemma 5.5:
– Assume that we start with a forest of trees, each having one node.
Let T be a tree with m nodes created as a result of a sequence of
unions each performed using function WeightedUnion.
The height of T is no greater than
.
log 2 m + 1
• For the processing of an intermixed sequence of u – 1 unions
and f find operations, the time complexity is
O(u + f*log u).
0 1 2 3 4 5 6 7
0 2 4 6
1 3 5 7
(b) Height-2 trees following union (0, 1), (2, 3), (4, 5), and (6, 7)
0 4 0
1 2 5 6 1 2 4
3 7 3 5 6
• Example:
– The first run of find operation will collapse the tree.
Therefore, all following find operation of the same element
only goes up one link to find the root.
[-8]
0
Root
6 7 1 2 4
Trail
3 5 6
Lead
J.-J. Chen, EE NTUST 60
7
Collapsing Find
[-8] [-8]
0 0
1 2 4 1 2 4 6 7
3 5 6 3 5
7 After
Before collapsing
collapsing
0 4, 3 1, 6 10, 8 9, 7 4, 6 8, 3 5, 2 11, 11 0
0 3 6 8 2 5 7 11
4 1 10 9
4 7 10 8 1 5 11
4 7 2 10 8 1 5
11 9
(d) trees following 11≡0
J.-J. Chen, EE NTUST Tree II-64
Chapter 5
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•An Object-Oriented Representation
•Counting Binary Tree
array
CompleteBinaryTree BST
MaxPQ
MaxHeap WinnerTree
Inherited
by BST
Inherited by
MaxHeap & WinnerTree
J.-J. Chen, EE NTUST Tree II-70
Chapter 5
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•AnObject-Oriented Representation
•Counting Binary Tree
• Solution
– The answers to the above questions are the same !!
b1 = 1 b2 = 2
b3 = 5
A
B D, E, F, G, H, I
B, C D, E, F, G, H, I
C
B D, E, F, G, H, I B D
C E F,G,H,I
C
D B D
B
F C E F
C E
I G I
G,H
H
J.-J. Chen, EE NTUST Tree II-75
Valid Order ?
• Given
– The pre-order sequence A B C
– The in-order sequence C A B
• Questions
– Is the given order corresponds to any tree
• Answers
– No, there is no binary tree according to this order pair
A
Construct Violate the
by inorder C B preorder
8 4
A A A A
A
B B B
B
B C
C
C C C
bn
bn = i bi bn −i −1 , n 1, and
n −1
b0 = 1
bi bn-i-1
• Assume we let B( x) = bi xi
i 0
which is the generating function for the number of binary trees.
1 − 1 − 4x
B( x ) =
2x
1 1 / 2 1/ 2
B( x) =
1 −
( −4 x ) n
=
( −1) m 2 m +1 m
2 x
2 x n0 n m0 m + 1
1 2n
bn = bn = O(4n / n3 / 2 )
n +1 n
J.-J. Chen, EE NTUST Tree II-84
Example
• Let bn be the number of distinct binary tree with n
nodes. We have
bn = i bi bn −i −1 , n 1, and
n −1
b0 = 1
What are values of b4 and b7?
1 2n
The f ormula of bn is
n +1 n
1 8 1 8 7 6 5
so b 4 = = = 14
4 + 1 4 5 1 2 3 4
1 14 1 14 13 12 11 10 9 8
b7 = = = 429
7 +1 7 8 7!