1 / 17

Data Structures 實習十二 樹

Data Structures 實習十二 樹. Department of Computer Science and Engineering National Taiwan Ocean University. Instructor: Ching-Chi Lin 林清池 助理教授 chingchi.lin@gmail.com. Tree Traversal. 從 root 開始,使用某種 特定的 順序,走過這棵樹所有的節點。 In-order :先走左邊的子樹,再自己,再走右邊的子樹。 Pre-order :先自己,再走左邊的子樹,再走右邊的子樹。

cedric
Download Presentation

Data Structures 實習十二 樹

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. Data Structures實習十二 樹 Department of Computer Science and EngineeringNational Taiwan Ocean University Instructor: Ching-Chi Lin林清池 助理教授 chingchi.lin@gmail.com

  2. Tree Traversal • 從root開始,使用某種特定的順序,走過這棵樹所有的節點。 • In-order:先走左邊的子樹,再自己,再走右邊的子樹。 • Pre-order:先自己,再走左邊的子樹,再走右邊的子樹。 • Post-order:先走左邊的子樹,再走右邊的子樹,再自己。 • Level-order:同樣高度的先走,從左到右。

  3. Tree Traversal • Inorder • A / B * C * D + E • Preorder • + * * / A B C D E • Postorder • A B / C * D * E + + * E * D / C A B = NULL

  4. 二元樹Inorder traversal– PseudoCode • void inorder (tree_pointer ptr) {/* inorder tree traversal */ if (ptr) { inorder (ptrleft_child); printf (“%d”, ptrdata); inorder (ptrright_child); } }

  5. 二元樹Preorder traversal-PseudoCode • void preorder (tree_pointer ptr) {/* preorder tree traversal */ if (ptr) { printf (“%d”, ptrdata); preorder (ptrleft_child); preorder (ptrright_child); } }

  6. 二元樹Postorder traversal-PseudoCode • void postorder (tree_pointer ptr) {/* postorder tree traversal */ if (ptr) { postorder (ptrleft_child); postorder (ptrright_child); printf (“%d”, ptrdata); } }

  7. Level-Order traversal • 首先拜訪根節點。 • 然後是根節點的左子節點,接著根節點的右子節點。 • 以相同的方式拜訪下一階層中的節點,由最左邊的節點到最右邊的節點。

  8. Level-Order traversal + * E * D / C Level-Order traversal: +*E*D/CAB A B

  9. Level-Order traversal-PseudoCode • void level_order (tree_pointer ptr) {/* level order tree traversal */ intfront = rear = 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->left_child)addq(ptr->left_child); if (ptr->right_child)addq(ptr->right_child); }else break; } }

  10. 練習 • 請實做出二元樹(binary tree)的Abstract Data Type。 • 非leaf node為算符。 • leaf node為數字。 • 從檔案讀入一個三元組(3-tuple)式子,並轉成二元樹型態。 • 檔案為純文字檔,請從教學網站下載。 • 請實做出對二元樹進行In-order /Pre-order/Post-order/Level-order的函式。 • 輸入格式:(根,左邊子樹,右邊子樹) • Ex:(-,(/,(*,16,(+,24,13)),9),10)

  11. 練習—輸入格式 • (-,(/,(*,16,(+,24,13)),9),10) - / 10 * 9 + 16 13 24

  12. 3-tuple轉二元樹:演算法 (-,(/,(*,16,(+,24,13)),9),10) (-,(/,(*,16,(+,24,13)),9),10) - (/,(*,16,(+,24,13)),9) / 10 (*,16,(+,24,13)) * 9 (+,24,13) + 16 24 13

  13. 3-tuple轉二元樹:演算法 • (根, 左邊子樹, 右邊子樹) • 每一組tuple之間用逗號隔開。 • 第一個項目(根)一定是運算符號。 • 第二/三項可能是數字或是算式。 • 數字即為leaf node。 • 算式則要進一步使用遞迴展開。 • 如何判斷是哪一個逗點分隔? • 提示:括號必定成對。 • (-,(/,(*,16,(+,24,13)),9),10)

  14. intatoi (const char * str); • #include<stdlib.h> • 輸入一個字串。 • 如果該字串是數字,傳回一整數。 • Ex:int a = atoi("100")

  15. 3-tuple轉二元樹:PseudoCode 1/1 • node* build_tree_from_string (char* formula) { char left[1024],right[1024],op; root = get_node(); if (formula is a number){ root->value = atoi(formula); root->left = NULL; root->right = NULL; }else{ <Separate formula into operator, left_subtree, right_subtree>; root->operator= operator; root->left=build_tree_from_string(left); root->right=build_tree_from_string(right); } return root; }

More Related