1 / 10

Tree Traversal

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.

keladry
Download Presentation

Tree Traversal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.(如果不能向右移動,由上一階層中最後一個未被拜訪的節點繼續尋訪)

  3. Inorder traversal of a binary tree • void inorder (tree_pointer ptr){ /* inorder tree traversal */ if (ptr) { inorder (ptrleft_child); printf (“%d”, ptrdata); inorder (ptrright_child); } }

  4. Preorder and Postorder traversals of a binary tree void preorder (tree_pointer ptr){ /* preorder tree traversal */ if (ptr) { printf (“%d”, ptrdata); printf (ptrleft_child); printf (ptrright_child); } } void postorder (tree_pointer ptr){ /* postorder tree traversal */ if (ptr) { postorder(ptrleft_child); postorder(ptrright_child); printf (“%d”,ptrdata); } }

  5. 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(以相同的方式拜訪下一階層中的節點,由最左邊的節點到最右邊的節點)

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

  7. 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”, ptrdata); if (ptrleftChild) addq(ptrleft_child); if (ptrrightChild) addq(ptrright_child); } else break; } }

  8. 練習一 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.

  9. 練習一 - / 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.

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

More Related