Professional Documents
Culture Documents
Tree(Part I)
•Introduction
•Binary Tree
•Binary Tree Traversal
•Additional Binary Tree operations
•Threaded Binary trees
•Heaps
Root
Dusty
leaf
0 K 0 L
0
M 4
J.-J. Chen, EE NTUST 2023/5/3 Tree I-4
Terminology
• Degree
– The degree of a node is the number of subtrees of the node
– The degree of A is 3; the degree of C is 1.
• Leaf node
– The node with degree 0 is a leaf or terminal node.
• Parent
– A node that has subtrees is the parent of the
roots of the subtrees.
• Children
– The roots of these subtrees are the children of
the node.
• Siblings
– Children of the same parent are siblings.
• Ancestors
– The ancestors of a node are all the nodes along the path from
the root to the node.
B C D
E F G H I J
K L M
• 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
• Proof
– The number of non-zero child fields in an n-node tree is exactly
n-1
– The total number of child fields in a k-ary tree with n nodes is nk
– Hence, the number of 0 fields is nk-(n-1) = n(k-1) +1
E F G H I J
A
K L M
B C D
E F G H I J
K L M
A B
D E C
B C
G D
K F
E F G H I J
H
L
K L M
M I
• Universal Representation
– Any tree can be transformed into binary tree by left
child-right sibling representation
• Distinguished Subtrees
– The left subtree and the right subtree are distinguished.
B C D
A
E F G H I J B
K L M E C
G D
K F
H
L
M I
J
J.-J. Chen, EE NTUST 2023/5/3 Tree I-12
ADT of BT
1 A A A
2
B B B C
3 C F G
D E
4 D
H I
5 E
i =1
A A
B C B C
D E F G D E F G
H H I J K L M N O
I
Complete binary tree Full binary tree of depth 4
J.-J. Chen, EE NTUST 2023/5/3 Tree I-17
Full BT with Sequential Node Number
1
4 5 6 7
8 9 10 11 12 13 14 15
• Lemma 5.4
– If a complete binary tree with n nodes is represented
sequentially, then for any node with index, i, 1 i n, we have
1. Parent(i) is at i/2 if i1
2. LeftChild(i) is at 2i if 2i n.
If 2i n, then i has no right child
3. RightChild(i) is at 2i if 2i+1 n.
If 2i +1 n, then i has no right child
J.-J. Chen, EE NTUST 2023/5/3 Tree I-18
Sequential Representation
(1) Good for complete BT but waste space for other BT
(2) insertion/deletion problem
[1] A
[2] B
A [3] -- [1] A
A
[4] C [2] B
B [5] -- [3] C
[6] -- B C [4] D
C [5] E
[7] --
[8] D [6] F
D D E F G
[9] --c [7] G
.. .. [8] H
E
H I [9] I
[16] E
data
left_child right_child
C 0
B C
D 0 D 0 E 0 0 F 0 0 G 0
C 0 0 H 0 0 I 0
* E •Preorder traversal
+**/ABCDE
* D prefix expression
/ C •Postorder traversal
AB/C*D*E+
A B postfix expression
current
Left Right
subtree subtree
current
Left Right
subtree subtree
Left Right
subtree subtree
* D Level-order Traversal:
/ C +* E *D /CAB
A B
+
Breadth-First Traversal (BFS)
* E
* D Level-order Traversal:
/ C +* E *D /CAB
A B
2n possible combinations X2 X1
for n variables
J.-J. Chen, EE NTUST 2023/5/3 Tree I-35
1st version of Satisfiability Problem
enum Boolean{ FALSE,TRUE };
enum TypeOfData { Not, And, Or, True, False};
class SatTree; // forward declaration
class SatNode { class SatTree{
friend class SatTree; public:
private: void PostOrderEval();
SatNode *LeftChild; void rootvalue() { cout << root->value;};
TypesOfData data; private:
Boolean value; SatNode *root;
SatNode *RightChild; void PostOrderEval(SatNode *);
}; };
for all 2n possible truth value combinations for the n variables
{ LeftChild data value RightChild
generate the next combination; 0/1
replace the variables by their values;
evaluate the formula by traversing the tree it points to in postorder
if(formula.rootvalue()) { cout << combination; return; }
}
cout << " no satisfiable combination";
J.-J. Chen, EE NTUST 2023/5/3 Tree I-36
Evaluating a Formula
TRUE ⚫ ⚫ FALSE
dangling D E F G
f A f
f B f f C f
f D f t E t t F t t G t
t H t t I t
inorder traversal:
H, D, I, B, E, A, F, C, G
f f
TEMP 21 CURRENT1
TEMP 11
TEMP 12 CURRENT 2
1
3
2
3
2
1
1
2
3
3
2
1
Server $ $ $ $ $
machine
Required service time $ $ $ $ $
$ payment
Machine Machine
15 2 15 5 15 20
14 10 14 10 2 14 10 2
initial location of new node insert 5 into heap insert 21 into heap
15 2
14 10 x.key
20 21
15 5 15 20
14 10 2 14 10 2
14 10 14 10
19 19
Move 10 to
vacancy
null 15 10
15
14 14 null
10
14 10 14 10
20 20
Move 10 to
vacancy
19 14 19
14
null 10 10
18 17 14 10
15
i: vacant position
j: larger child
Delete 15 Delete 50