310 likes | 404 Views
Review of Chapter 5. 張啟中. Definition of Tree. A tree is a finite set of one or more nodes such that A specially designated node called root . The remaining nodes are partitioned into n 0 disjoint sets T 1 , …, T n , called subtrees. root. T 1. T 2. T n. root. level.
E N D
Definition of Tree • A tree is a finite set of one or more nodes 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
root level Terminology A node 1 degree 3 children E C B siblings B, D D 2 E F H I J 3 G parent B ancestors A, D, H M 4 K L leaf height or depth = maximum level of any node in the tree = 4 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
A B F E K L C G 0 0 0 0 Representation of Trees (1) • List Representation • 利用 generalized lists 來表示 (A(B(E(K, L), F), C(G), D(H(M), I, J )) ) • 利用 fixed size 的 Node 本圖未完成,留給同學練習
Representation of Trees (1) • Lemma 5.1 If 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!
data left child Right sibling Representation of Trees (2) A B C D E F H I J G M K L
Representation of Trees (3) A B E C D K F G H L M I J
Relation of Tree Data Structures Binary Tree Search Struct Complete Binary Tree Binary Search Tree Max PQ Max Heap Tree Winner
Binary Trees • A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree. A B C D E E
Binary Trees VS. Trees 註:修正老師上課時的說法
Full Binary Tree • A full binary tree of depth k is a binary tree of depth k having 2k – 1 nodes, k ≥ 0. level 1 A 2 B C 3 D E F G H I J K L M N O 4
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. level 1 A 2 B C 3 D E F G 4 H I
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 no right child. • Position zero of the array is not used.
Binary Tree Representations • Array Representation • Use Lemma 5.4 • Linked Representation
Binary Tree Representations (Array) 0 — A 1 A 2 B B 3 — 4 C C 5 — 6 — D 7 — 8 D E 9 — skewed binary tree 16 E
Binary Tree Representations (Array) 0 — A 1 A 2 B 3 C B C 4 D 5 E 6 F G D E F 7 G 8 — 9 — H I 16 —
Binary Tree Representations (Linked) data LeftChild data RightChild root LeftChild Right Child A 0 A B 0 B C C 0 D D 0 E 0 E 0
A E H 0 I 0 Binary Tree Representations (Linked) root C B D F 0 G 0
classTree; //forward declaration classTreeNode { friend classTree; private: TreeNode *LeftChild; chardata; TreeNode *RightChild; }; class Tree { public: //Tree operation private: TreeNode *root; }; Binary Tree Representations (Linked)
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 * C * D + E A B
Binary Tree Traversal ( Postorder LRV ) + * E * D / C A B / C * D * E + A B
Binary Tree Traversal ( Preorder VLR ) + * E * D / C + * * / A B C D E A B
Binary Tree Traversal ( Level-Order ) + * E * D / C + * E * D / C A B 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 • Recusive void 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 • Iterative void Tree::NonrecInorder() //nonrecursive inorder traversal using a stack { Stack<TreeNode *> s; //declare and initialize stack TreeNode *CurrentNode = root; while (1) { while (CurrentNode) { //move downLeftChild 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; } elsebreak; } }
Implement of Binary Tree Traversal void 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.