Académique Documents
Professionnel Documents
Culture Documents
Data Structures
Chapter 5 Trees
Forests
Definition: A forest is a set of n 0 disjoint trees.
When we remove a root from a tree, well get a
forest. E.g., Removing the root of a binary tree
will get a forest of two trees.
E
C
Figure 5.34
Three-tree forest
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)
Has right subtree equal to B(T2, , Tn)
A
D
Set Representation
Trees can be used to represent sets.
Disjoint set Union
If Si and Sj are two disjoint sets, then their union
Si Sj = {all elements x such that x is in Si or Sj}.
Find (i)
Find the set containing element i.
S3
S2
S1
Set I = {0 , 6 ,7 ,8 }
Set 3 = {2 , 3 , 5}
Unions of Sets
To obtain the union of two sets, just set the
parent field of one of the roots to the other root.
Possible Representations of S1 S2
S2
S1
0
4
S2
Possible Representations of S1 S2
S1
S2
4
S1
Set
Name
Pointer
S1
S2
S3
Array Representation
We could use an array for the set name.
Or the set name can be an element at the
root.
Assume set elements are numbered 0
through n-1.
i
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
parent
-1
-1
-1
Root
Array Representation
void Union1( int i , int j )
{
parent[i] = j ;
}
EX: S1 S2
Union1( 0 , 2 ) ;
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
parent
-1
2
-1
-1
EX: Find1( 5 ) ;
int Find1( int i )
{
for(;parent[i] >= 0 ; I = parent[i]) ;
return i ;
}
i =2
Union operation
O(n)
Find operation
O(n2)
Weighting Rule
Definition [Weighting rule for union(i, j) ]
If the number of nodes in the tree with root i is less
than the number in the tree with root j, then make j
the parent of i ; otherwise make i the parent of j.
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
parent
-1
-2
-3
-1
0
-1
0
-1
-1
-1
-1
-1
-1
-1
11
21
23
unoin2 (0 , 1 )
unoin2 (0 , 2 )
unoin2 (0 , 3 )
temp = -2
temp = -3
n-1
Weighted Union
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 .
[-1]
[-1]
[-1]
[-1]
[-1]
[-1]
[-1]
[-2]
[-2]
[-2]
(b) Height-2 trees following union (0, 1), (2, 3), (4, 5), and (6, 7)
[-4]
[-4]
[-8]
2
3
6
7
2
3
4
5
6
7
Collapsing Rule
Definition [Collapsing rule]
If j is a node on the path from i to its root and parent[i] root(i),
then set parent[j] to root(i).
Root
2
3
4
5
Trail
6
Lead
7
Example 5.3
[-1]
[-1]
[-1]
[-1]
[-1]
[-1]
[-1]
[-1]
[-1]
[-1]
[-1]
[-1]
10
11
[-2]
[-2]
[-2]
[-2]
[-1]
[-1]
[-1]
[-1]
11
10
[-3]
[-4]
[-3]
[-2]
10
11
9
(c) Tree following 74, 68, 35, and 211
[-3]
[-4]
[-3]
10
11
8
9
preorder: A B C D E F G H I
inorder: B C A E D G H F I
Stack permutation
(2/4)
E, D, G, H, F, I
B, C
preorder: A B C (D E F G H I)
inorder: B C A (E D G H F I)
A
D
B
C E
F, G, H, I
2
3
(1, 2, 3)
(1, 3, 2)
2
3
3
(2, 1, 3)
(2, 3, 1)
3
(3, 2, 1)
bn bi bn i , n 1
i 1
bn
bi
bn-i-1
B( x) bi x i
i 0
1 1 4x
2x
1/ 2
1/ 2
1
n
m 2 m 1 m
B( x)
1
4
x
)
1
)
2
x
n
2x
n0
m0 m 1
1 2n
bn
n 1 n
bn O(4 n / n 3 / 2 )