130 likes | 523 Views
Tree Traversal. +. 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. 1. /. C. 2. 17. A. B. 3. 14. 18. 19. 4. 11. 15. 16. 8. 5. 12. 13. 6. 7. 9. 10.
E N D
Tree Traversal + • 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 1 / C 2 17 A B 3 14 18 19 4 11 15 16 8 5 12 13 6 7 9 10
Inorder Traversal • 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.(如果不能向右移動,由上一階層中最後一個未被拜訪的節點繼續尋訪)
Inorder traversal of a binary tree • void inorder (tree_pointer ptr){ /* inorder tree traversal */ if (ptr) { inorder (ptrleft_child); printf (“%d”, ptrdata); inorder (ptrright_child); } }
Preorder and Postorder traversals of a binary tree 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); } }
Level-Order Traversal • The steps of level-order traversal are: • visit the root first (首先拜訪根節點) • then the root’s left child followed by the right child(而後是根節點的左子節點,接著根節點的右子節點) • visit next level from leftmost node to right most node(以相同的方式拜訪下一階層中的節點,由最左邊的節點到最右邊的節點)
An example for level-order Traversal + 1 * E 2 3 * D 4 5 / C 6 7 A B 8 9 Level-order traversal: +*E*D/CAB
Level-order traversal of a binary tree • 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; } }
練習一 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.
練習一 - / 10 9 * 16 + 24 13 Fig 1 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.
繳交日期 • 程式demo: 5/20(三)於實習課