31
Review of Chapter 5 張張張

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

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 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

Review of Chapter 5

張啟中

Page 2: 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

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

Page 3: 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

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

Page 4: 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

Representation of Trees

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

Page 5: 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

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

本圖未完成,留給同學練習

Page 6: 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

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!

Page 7: 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

Representation of Trees (2)

A

B C D

E F G H I J

L MK

data

left child Right sibling

Page 8: 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

Representation of Trees (3)A

B

C

D

E

F G

H

I

J

L

M

K

Page 9: 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

Relation of Tree Data Structures

Binary Tree

Complete Binary Tree Binary Search Tree

Search Struct

Max Heap Tree Winner

Max PQ

Page 10: 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

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

Page 11: 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

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 ( 註 )

註:修正老師上課時的說法

Page 12: 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

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

Page 13: 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

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

Page 14: 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

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.

Page 15: 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

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

Page 16: 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

Binary Tree Representations

Array Representation Use Lemma 5.4

Linked Representation

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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

Page 20: 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

Binary Tree Representations (Linked)

A

B C

D E

H 0 I 0

F 0 G 0

root

Page 21: 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

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;

};

Page 22: 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

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.

Page 23: 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

Binary Tree Traversal ( Inorder LVR )

+

* E

* D

/ C

A B A / B * C * D + E

Page 24: 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

Binary Tree Traversal ( Postorder LRV )

+

* E

* D

/ C

A B A B / C * D * E +

Page 25: 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

Binary Tree Traversal ( Preorder VLR )

+

* E

* D

/ C

A B

+ * * / A B C D E

Page 26: 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

Binary Tree Traversal ( Level-Order )

+

* E

* D

/ C

A B

+ * E * D / C A B

Page 27: 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

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)

Page 28: 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

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

Page 29: 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

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;

}}

Page 30: 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

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();

}}

Page 31: 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

Traversal without Stack

二種方式 每個 node 都增加一個欄位紀錄 parent node 的

位置。 將 binary trees 改成 threaded binary Trees.