tree traversal 1 inorder traversal: a/b*c*d+e (lvr) infix form preorder traversal: +**/abcde...

10
Tree Traversal 1 Inorder traversal: A/B*C*D+E (LVR) Infix form Preorder traversal: +**/ABCDE (VLR) Prefix form Postorder traversal: AB/C*D*E+ (LRV) Postfix form + * E D * C / B A 1 2 3 4 5 8 11 14 17 9 6 7 10 12 13 15 16 18 19

Upload: scott-french

Post on 04-Jan-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

Tree Traversal

1

Inorder traversal: A/B*C*D+E (LVR) Infix form

Preorder traversal: +**/ABCDE (VLR) Prefix form

Postorder traversal: AB/C*D*E+ (LRV) Postfix form

+

* E

D*

C/

BA

1

2

3

4

5 8

11

14

17

96 7 10

12 13

15 16

18 19

Page 2: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

Inorder Traversal

2

Informally, inorder traversal calls for moving down the tree toward the left until you can go no farther.( 非正式地講法 , 中序向左移動 , 一直到達一個空節點為止 )

Then you “visit” the node, move one node to the right and continue.( 然後“拜訪”此空節點的父節點 , 並且由它的右子節點繼續尋訪 )

If you can not move to the right, go back one more node.( 如果不能向右移動 , 由上一階層中最後一個未被拜訪的節點繼續尋訪 )

Page 3: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

Inorder traversal of a binary tree

3

void inorder (tree_pointer ptr){ /* inorder tree traversal */ if (ptr) { inorder (ptrleft_child); printf (“%d”, ptrdata); inorder (ptrright_child); } }

Page 4: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

Preorder and Postorder traversals of a binary tree

4

void preorder (tree_pointer ptr){ /* preorder tree traversal */ if (ptr) { printf (“%d”, ptrdata); printf (ptrleft_child); printf (ptrright_child); } }

void postorder (tree_pointer ptr){ /* postorder tree traversal */ if (ptr) { postorder(ptrleft_child); postorder(ptrright_child); printf (“%d”,ptrdata); } }

Page 5: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

Level-Order Traversal

5

The steps of level-order traversal are:1. visit the root first

( 首先拜訪根節點 )

2. then the root’s left child followed by the right child( 而後是根節點的左子節點 , 接著根節點的右子節點 )

3. visit next level from leftmost node to right most node( 以相同的方式拜訪下一階層中的節點 , 由最左邊的節點到最右邊的節點 )

Page 6: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

An example for level-order Traversal

6

+

* E

D*

C/

BA

1

2

4

6

8 9

7

5

3

Level-order traversal: +*E*D/CAB

Page 7: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

Level-order traversal of a binary tree

7

void level_order (tree_pointer ptr){ /* level order tree traversal */ int front = near = 0; tree_pointer queue [MAX_QUEUE_SIZE]; if (!ptr) return; /* empty tree */ addq(ptr); for (;;) { ptr = deleteq(); if (ptr) { printf(“%d”, ptrdata); if (ptrleftChild) addq(ptrleft_child); if (ptrrightChild) addq(ptrright_child); } else break; } }

Page 8: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

練習一

8

You are required to implement a binary tree abstract data type. The functions of the ADT include Gen_BinTree, Pre_traversal ,

In_traversal , Post_traversal and Level_traversal. The input is a 3-tuple list as follows (-, (/, (*, 16, (+, 24, 13)), 9), 10). The Gen_BinTree function will transfer the 3-tuple list to a binary

tree structure as Fig 1. The Pre_traversal function will perform the preorder traversal of the

binary tree. The In_traversal function will perform the inorder traversal of the

binary tree.

             

        

Page 9: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

練習一

9

The Post_traversal function will perform the postorder traversal of the binary tree.

The Level_traversal function will perform the level-order traversal of the binary tree.

The program should be written in C language. The output will left for you to decide.

             

        

*

-

/

16 +

9

10

24 13Fig 1

Page 10: Tree Traversal 1  Inorder traversal: A/B*C*D+E (LVR)  Infix form  Preorder traversal: +**/ABCDE (VLR)  Prefix form  Postorder traversal: AB/C*D*E+

繳交日期 程式 demo: 5/20(三) 於實習課

10