Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Chapter 5 張啟中. Definition of Tree A tree is a finite set of one or more nodes such that A tree is a finite set of one or more nodes such that.

Similar presentations


Presentation on theme: "Review of Chapter 5 張啟中. Definition of Tree A tree is a finite set of one or more nodes such that A tree is a finite set of one or more nodes such that."— Presentation transcript:

1 Review of Chapter 5 張啟中

2 Definition of Tree A tree is a finite set of one or more nodes such that 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 T1T1 T2T2 TnTn

3 Terminology A B C D EF G HIJ L M node leaf parent B children E siblings B, D 1 2 3 4 level root K ancestors A, D, H height or depth = maximum level of any node in the tree = 4 degree 3 degree of a tree = maximum of the degree of the nodes = 3

4 Representation of Trees Tree 的表示方法,常用的有下列三種  List Representation  Left Child-Right Sibling Representation  Representation as a Degree-Two Tree

5 Representation of Trees (1) List Representation  利用 generalized lists 來表示 (A( B(E(K, L), F), C(G), D(H(M), I, J ) ) )  利用 fixed size 的 Node ABF0EKL0CG00 本圖未完 成,留給 同學練習

6 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. DataChild 1Child 2Child 3Child 4 … Child k Wasting memory!

7 Representation of Trees (2) A BCD EF G HIJ L M K data left childRight sibling

8 Representation of Trees (3) A B C D E FG H I J L M K

9 Relation of Tree Data Structures Binary Tree Complete Binary TreeBinary Search Tree Search Struct Max Heap TreeWinner Max PQ

10 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 BC D EE

11 Binary Trees VS. Trees category item TreesBinary Trees The order of the subtreesnone distinctions between left and right Empty (zero nodes)NoYes degree0..n 0..2 ( 註 ) 註:修正老師上課時的說法

12 Full Binary Tree A full binary tree of depth k is a binary tree of depth k having 2 k – 1 nodes, k ≥ 0. A BC D IH G M 1 2 3 4 level EF JKLNO

13 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 BC DGFE IH

14 Properties of Binary Trees Lemma 5.2 [Maximum number of nodes]  The maximum number of nodes on level i of a binary tree is 2 i-1, i ≥ 1.  The maximum number of nodes in a binary tree of depth k is 2 k – 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 n 0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n 0 = n 2 + 1.

15 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.

16 Binary Tree Representations Array Representation  Use Lemma 5.4 Linked Representation

17 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

18 Binary Tree Representations (Array) A BC D G FE IH — A B C D E F G — — — 0 1 2 3 4 5 6 7 8 9 16

19 Binary Tree Representations (Linked) A B C D E LeftChild data RightChild data LeftChild Right Child A0 B0 C0 D0 0E0 root

20 Binary Tree Representations (Linked) A B C D EH0I0 F0G0 root

21 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; };

22 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.

23 Binary Tree Traversal ( Inorder LVR ) + *E *D /C AB A / B * C * D + E

24 Binary Tree Traversal ( Postorder LRV ) + *E *D /C AB A B / C * D * E +

25 Binary Tree Traversal ( Preorder VLR ) + *E *D /C AB + * * / A B C D E

26 Binary Tree Traversal ( Level-Order ) + *E *D /C AB + * E * D / C A B

27 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)

28 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 data; inorder(CurrentNode->RightChild); }

29 Implement of Binary Tree Traversal Iterative void Tree::NonrecInorder() //nonrecursive inorder traversal using a stack { Stack s; //declare and initialize stack TreeNode *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 data << endl; CurrentNode = CurrentNode->RightChild; } else break; }

30 Implement of Binary Tree Traversal void Tree::LevelOrder() //Traverse the binary tree in level order { Queue q; TreeNode *CurrentNode = root; while (CurrentNode) { cout data<<endl; if (CurrentNode->LeftChild) q.Add(CurrentNode->LeftChild); if (CurrentNode->RightChild) q.Add(CurrentNode->RightChild); CurrentNode = *q.Delete(); }

31 Traversal without Stack 二種方式  每個 node 都增加一個欄位紀錄 parent node 的位 置。  將 binary trees 改成 threaded binary Trees.


Download ppt "Review of Chapter 5 張啟中. Definition of Tree A tree is a finite set of one or more nodes such that A tree is a finite set of one or more nodes such that."

Similar presentations


Ads by Google