Review of Chapter 5
張啟中
Definition of Tree A tree is a finite set of one or more nodes A tree is a finite set of one or more nodes
such that such that A specially designated node called root. The remaining nodes are partitioned into n0
disjoint sets T1, …, Tn, called subtrees.
root
T1 T2 Tn
Terminology
A
B C D
E F G H I J
L M
node
leaf
parentB
childrenE
siblingsB, D
1
2
3
4
levelroot
K ancestorsA, D, H
height or depth = maximum level of any node in the tree = 4
degree3
degree of a tree = maximum of the degree of the nodes = 3
Representation of Trees
Tree 的表示方法,常用的有下列三種 List Representation Left Child-Right Sibling Representation Representation as a Degree-Two Tree
Representation of Trees (1) List Representation
利用 generalized lists 來表示 (A( B(E(K, L), F), C(G), D(H(M), I, J ) ) ) 利用 fixed size 的 Node
A
B F 0
E K L 0
C G 0
0
本圖未完成,留給同學練習
Representation of Trees (1)
Lemma 5.1If T is a k-ary tree (i.e., a tree of degree k) with n nodes, each having a fixed size as in Figure 5.4, then n(k-1) + 1 of the nk child fileds are 0, n ≥ 1.
Data Child 1 Child 2 Child 3 Child 4 … Child k
Wasting memory!
Representation of Trees (2)
A
B C D
E F G H I J
L MK
data
left child Right sibling
Representation of Trees (3)A
B
C
D
E
F G
H
I
J
L
M
K
Relation of Tree Data Structures
Binary Tree
Complete Binary Tree Binary Search Tree
Search Struct
Max Heap Tree Winner
Max PQ
Binary Trees A binary tree is a finite set of nodes that is ei
ther empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree.
A
B C
D
EE
Binary Trees VS. Trees
category
itemTrees Binary Trees
The order of the subtrees nonedistinctions between
left and right
Empty (zero nodes) No Yes
degree 0..n 0..2 ( 註 )
註:修正老師上課時的說法
Full Binary Tree A full binary tree of depth k is a binary tree
of depth k having 2k – 1 nodes, k ≥ 0.
A
B C
D
IH
G
M
1
2
3
4
level
E F
J K L N O
Complete Binary Tree A binary tree with n nodes and depth k is complete if and only if its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k.
1
2
3
4
level
A
B C
D GFE
IH
Properties of Binary Trees
Lemma 5.2 [Maximum number of nodes] The maximum number of nodes on level i of a
binary tree is 2i-1, i ≥ 1. The maximum number of nodes in a binary tree of
depth k is 2k – 1, k ≥ 1. Lemma 5.3 [Relation between number of leaf nodes and
nodes of degree 2] For any non-empty binary tree, T, if n0 is the
number of leaf nodes and n2 the number of nodes of degree 2, then n0 = n2 + 1.
Properties of Binary Trees
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: parent(i) is at if i ≠1. If i = 1, i is at the root and has no
parent. left_child(i) is at 2i if 2i ≤ n. If 2i > n, then i has no left child. right_child(i) is at 2i + 1 if 2i + 1 ≤ n. If 2i + 1 > n, then i has n
o right child.
Position zero of the array is not used.
2
i
Binary Tree Representations
Array Representation Use Lemma 5.4
Linked Representation
Binary Tree Representations (Array)
A
B
C
D
E
—
A
B
—
C
—
—
—
D
—
E
0
1
2
3
4
5
6
7
8
9
16
skewed binary tree
Binary Tree Representations (Array)
A
B C
D GFE
IH
—
A
B
C
D
E
F
G—
—
—
0
1
2
3
4
5
6
7
8
9
16
Binary Tree Representations (Linked)
A
B
C
D
E
LeftChild data RightChilddata
LeftChild Right ChildA 0
B 0
C 0
D 0
0 E 0
root
Binary Tree Representations (Linked)
A
B C
D E
H 0 I 0
F 0 G 0
root
Binary Tree Representations (Linked)class Tree; //forward declaration
class TreeNode {
friend class Tree;
private:
TreeNode *LeftChild;
char data;
TreeNode *RightChild;
};
class Tree {
public:
//Tree operation
private:
TreeNode *root;
};
Manipulation of Binary Tree Traversal
Inorder (LVR) (Stack) Postorder (LRV) (Stack) Preorder (VLR) (Stack) Level-Order (Queue)
Copying Binary Trees Testing Equality
Two binary trees are equal if their topologies are the same and the information in corresponding nodes is identical.
Binary Tree Traversal ( Inorder LVR )
+
* E
* D
/ C
A B A / B * C * D + E
Binary Tree Traversal ( Postorder LRV )
+
* E
* D
/ C
A B A B / C * D * E +
Binary Tree Traversal ( Preorder VLR )
+
* E
* D
/ C
A B
+ * * / A B C D E
Binary Tree Traversal ( Level-Order )
+
* E
* D
/ C
A B
+ * E * D / C A B
Implement of Binary Tree Traversal Recursive Method
Program 5.1 (p263) --- Inorder Program 5.2 (p264) --- Preorder Program 5.3 (p265) --- Postorder
Iterative Method Program 5.4 (p266) --- Inorder Program 5.5 and 5.6 --- Use Iterator (Inorder)
Implement of Binary Tree Traversal Recusivevoid Tree :: inorder() // Driver calls workhorse for traversal of entire tree. The // driver is declared as a public member function of Tree. { inorder(root); }
void Tree :: inorder(TreeNode *CurrentNode)// Workhorse traverses the subtree rooted at CurrentNode // The workhorse is declared as a private member function of Tree. { if(CurrentNode){ inorder(CurrentNode->LeftChild); cout << CurrentNode->data; inorder(CurrentNode->RightChild); } }
Implement of Binary Tree Traversal Iterativevoid Tree::NonrecInorder()//nonrecursive inorder traversal using a stack{
Stack<TreeNode *> s; //declare and initialize stackTreeNode *CurrentNode = root;while (1) {
while (CurrentNode) { //move down LeftChild fields s.Add(CurrentNode); //add to stack CurrentNode = CurrentNode->LeftChild;
}if (!s.IsEmpty()) { //stack is not empty
CurrentNode = *s.Delete(CurrentNode); cout << CurrentNode->data << endl; CurrentNode = CurrentNode->RightChild;
}else break;
}}
Implement of Binary Tree Traversalvoid Tree::LevelOrder()//Traverse the binary tree in level order{Queue<TreeNode *> q;TreeNode *CurrentNode = root;while (CurrentNode) {
cout << CurrentNode->data<<endl;if (CurrentNode->LeftChild)
q.Add(CurrentNode->LeftChild);if (CurrentNode->RightChild)
q.Add(CurrentNode->RightChild);CurrentNode = *q.Delete();
}}
Traversal without Stack
二種方式 每個 node 都增加一個欄位紀錄 parent node 的
位置。 將 binary trees 改成 threaded binary Trees.