Professional Documents
Culture Documents
Trees
Trees
Root
D u sty
H o n ey B ear
B ru n h ild e
G ill
T a n se y
B ra n d y
T e rry
T w eed
C o y o te
Z oe
C ro c u s
P rim ro se
N u g g et
N ous
B e lle
leaf
CHAPTER 5
Definition of Tree
Level
A
3
2
2
0
40
30
L
2 1
F
30
C
G
1
2
31
0
3
H
M
CHAPTER 5
30
4
D
I
2
30
33
4
4
Terminology
CHAPTER 5
Representation of Trees
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of sub-trees
data
link n
M
CHAPTER 5
Binary Trees
E
F
G
H
L
M
I
J
CHAPTER 5
10
CHAPTER 5
11
Samples of Trees
Complete Binary Tree
A
B
A
B
D
4
E
CHAPTER 5
12
i-1
i 1
k
2
2
1
i 1
CHAPTER 5
13
CHAPTER 5
14
Full BT VS Complete BT
A
B
D
H
C
E
D
I
G
M
CHAPTERFull
5 binary tree of depth 15
4
16
[1]
[2]
[3]
(1) waste space
[4]
(2) insertion/deletion
[5]
problem
[6]
[7]
[8]
A
[9]
Sequential Representation
B
C
D
E
A [1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
.
[16]
A
B
-C
---D
-.
E
A
B
C
D
E
F
G
H
I
5
H CHAPTER
I
17
Linked Representation
typedef struct node *tree_pointer;
typedef struct node {
int data;
tree_pointer left_child, right_child;
};
data
left_child
data
right_child
left_child
CHAPTER 5
right_child
18
19
D
C
/
B
inorder traversal
A/ B * C * D + E
infix expression
preorder traversal
+ * * / AB C D E
prefix expression
postorder traversal
AB / C * D * E +
postfix expression
level order traversal
+ * E * D / C AB
CHAPTER 5
20
21
22
23
O(n)
CHAPTER 5
24
Value in root
+
*
*
/
A
NULL
A
NULL
/
B
NULL
B
NULL
*
Action
printf
printf
printf
printf
Call of inorder
11
12
11
13
2
14
15
14
16
1
17
18
17
19
CHAPTER 5
Value in root
C
NULL
C
NULL
*
D
NULL
D
NULL
+
E
NULL
E
NULL
Action
printf
printf
printf
printf
printf
25
26
if (ptr) {
printf(%d, ptr->data);
if (ptr->left_child)
addq(front, &rear,
ptr->left_child);
if (ptr->right_child)
addq(front, &rear,
ptr->right_child);
}
else break;
}
}
+ * E * D / C AB
CHAPTER 5
27
postorder
CHAPTER 5
28
CHAPTER 5
29
A variable is an expression.
If x and y are expressions, then x, xy,
xy are expressions.
Parentheses can be used to alter the normal
order of evaluation ( > > ).
Example: x1 (x2 x3)
satisfiability problem: Is there an assignment to
make an expression true?
CHAPTER 5
30
X1
X3
X3
X2
X1
2n possible combinations
for n variables
postorder traversal (postfix evaluation)
node structure
left_child
data
value
right_child
Post-order-eval function
void post_order_eval(tree_pointer node)
{
/* modified post order traversal to evaluate a propositional
calculus tree */
if (node) {
post_order_eval(node->left_child);
post_order_eval(node->right_child);
switch(node->data) {
case not: node->value =
!node->right_child->value;
break;
CHAPTER 5
36
CHAPTER 5
37
A
dangling
C
B
dangling
inorder traversal:
H, D, I, B, E, A, F, C, G
CHAPTER 5
38
left_thread
left_child
TRUE
TRUE: thread
data
right_child
right_thread
FALSE
FALSE: child
f
D
f
t
--
f
C
f
E
f
t
t
CHAPTER 5
40
41
CHAPTER 5
42
43
Examples
Insert a node D as a right child of B.
root
root
A
B
parent
(1)
B
parent
(3)
C
child
empty
CHAPTER 5
(2)
child
44
*Figure 5.24: Insertion of child as a right child of parent in a threaded binary tree (p.217
(3)
(2)
(4)
nonempty
(1)
(1)
(2)
(3)
(4)
CHAPTER 5
46
Heap
CHAPTER 5
47
[1]
[2]
[4]
10
[3]
12
[5]
[6]
[1]
14
[2]
7
[4]
9
[3]
[1]
[2]
25
Property:
The root of max heap (min heap) contains
the largest (smallest).
30
[1]
[2]
[4]
10
[3]
7
[5]
[1]
[6]
[2]
4
[4]
50
20
10
[3]
[1]
83
[2]
21
11
structure MaxHeap
objects: a complete binary tree of n > 0 elements organized so that
the value in each node is at least as large as those in its children
functions:
for all heap belong to MaxHeap, item belong to Element, n,
max_size belong to integer
MaxHeap Create(max_size)::= create an empty heap that can
hold a maximum of max_size elements
Boolean HeapFull(heap, n)::= if (n==max_size) return TRUE
else return FALSE
MaxHeap Insert(heap, item, n)::= if (!HeapFull(heap,n)) insert
item into heap and return the resulting heap
else return error
Boolean HeapEmpty(heap, n)::= if (n>0) return FALSE
else return TRUE
Element Delete(heap,n)::= if (!HeapEmpty(heap,n)) return one
instance of the largest element in the heap
and remove it from the heap
else return error
CHAPTER 5
50
machine service
amount of time (min heap)
amount of payment (max heap)
factory
time tag
CHAPTER 5
51
Data Structures
CHAPTER 5
52
(1)
(n)
(1)
(n)
O(n)
(1)
Sorted linked
list
Max heap
O(n)
(1)
O(log2n) O(log2n)
15
21
20
20
2
14 10
initial location of new node
15
14 10
CHAPTER 5
20
15
14 10
54
55
20
2
15
14
10
15
10
15
14
10
14
CHAPTER 5
56
CHAPTER 5
57
CHAPTER 5
58
Heap
a min (max) element is deleted. O(log2n)
deletion of an arbitrary element O(n)
search for an arbitrary element O(n)
CHAPTER 5
59
25
12
10
15
60
30
20
5
22
70
40
65
CHAPTER 5
80
60
CHAPTER 5
61
O(h)
CHAPTER 5
62
5
2
30
30
30
40
80
40
40
Insert 80
CHAPTER 5
35
80
Insert 35
63
CHAPTER 5
64
30
5
80
T2
T1
1
2
2
T1
X
T2
CHAPTER 5
65
40
40
60
20
10
70
30 50
70
30 50
10
45
55
45
55
20
52
52
Before deleting 60
After deleting 60
CHAPTER 5
66
1
2
T1
T2
T3
1
2
T1
T2
CHAPTER 5
T3
67
Selection Trees
(1)
(2)
winner tree
loser tree
CHAPTER 5
68
winner tree
sequential
allocation
scheme
(complete
binary
tree)
6
2
17
11
12
13
10
10
20
15
16
20
38
20
30
15
25
15
50
11
16
ordered sequence
run 1 run 2
run 3 run 4
run 5 run 6
CHAPTER 5
14
15
90
17
100
110
18
20
run 7 run 8
69
*Figure 5.35: Selection tree of Figure 5.34 after one record has been
output and the tree restructured(nodes that were changed are ticked)
1
2
4
8
10
9
9
5
9
10
20
15
11
15
12
8
7
8
13
14
90
17
15
17
15
20
20
25
15
11
100
18
16
38
30
25
50
16
110
20
Analysis
K: # of runs
n: # of records
setup time: O(K)
restructure time: O(log2K)
(K-1)
log2(K+1)
71
overall
winner
8
9
8
2
9
9 15
17
10
Run 1
15 9
20
9
10
9
2
20
3
10
8
12
11
6
4
15
90
14
13
8
5
15
9
6
90
7
15
17
8
Forest
Forest
A
D
B
I
E
G
C
D
H
I
CHAPTER 5
73
74
Forest Traversals
Preorder
Inorder
CHAPTER 5
75
inorder: EFBGCHIJDA
preorder: ABEFCGDHIJ
A
B
A
C
E
F
B
D
preorder
B
E
F
G H
C
G
D
H
I
J
CHAPTER 5
76
Set Representation
0
8
4
Si Sj =
77
0
6
8
4
78
set
pointer
name
S1
0
6
S2
S3
4
1
9
2
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
parent
-1
-1
-1
int find1(int i)
{
for (; parent[i]>=0; i=parent[i]);
return i;
}
void union1(int i, int j)
{
parent[i]= j;
}
CHAPTER 5
80
union operation
O(n) n-1
n-1
find operation
O(n2) n
n-2
i
i 2
0
degenerate tree
union(0,1), find(0)
union(1,2), find(0)
.
.
.
union(n-2,n-1),find(0)
CHAPTER 5
83
log28 +1
85
0
1
2
5
7
find(7) find(7) find(7) find(7) find(7) find(7) find(7) find(7)
go up
reset
3
1
1
2
12 moves (vs. 24 moves)
CHAPTER 5
86
Applications
example
0 4, 3 1, 6 10, 8 9, 7 4, 6 8,
3 5, 2 11, 11 0
{0, 2, 4, 7, 11}, {1, 3, 5}, {6, 8, 9, 10}
CHAPTER 5
87
preorder:
inorder:
A
ABC DE F G HI
B C AE D G H F I
A
D, E, F, G, H, I
B, C
A
B
C
D, E, F, G, H, I
F
G
I
H
C
CHAPTER 5
88