1 / 62

Terminology

Lecture9 Tree อ.วรวิทย์ วีระพันธุ์ เรียบเรียงโดย อ. เหมรัศมิ์ วชิรหัตถพงศ์ คณะ วิทยาการสารสนเทศ มหาวิทยาลัยบูรพา. Terminology. ต้นไม้ (Tree) ประกอบด้วย 1. Node ใช้เก็บข้อมูล 2. Branch ใช้เชื่อม Node เข้าด้วยกัน. A. B. C. Node A, B, C Branch AB, AC.

zia-beck
Download Presentation

Terminology

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. Lecture9 Treeอ.วรวิทย์ วีระพันธุ์ เรียบเรียงโดย อ.เหมรัศมิ์ วชิรหัตถพงศ์คณะวิทยาการสารสนเทศ มหาวิทยาลัยบูรพา

  2. Terminology ต้นไม้ (Tree) ประกอบด้วย 1. Nodeใช้เก็บข้อมูล 2. Branchใช้เชื่อม Node เข้าด้วยกัน A B C Node A, B, C Branch AB, AC

  3. Degreeหมายถึง จำนวน Branch ที่สัมพันธ์กับ Node แบ่งเป็น 1. Indegreeหมายถึง Branchที่เข้าหา Node 2. Outdegreeหมายถึง Branchที่ออกจาก Node A B C Terminology Node A มี Degree เท่ากับ 2 Indegree = 0 Outdegree = 2

  4. Root หมายถึง Node แรกของ Tree Root A B C Terminology

  5. ดังนั้นจะพบว่าต้นไม้ทั่วไปดังนั้นจะพบว่าต้นไม้ทั่วไป Root มี Indegree = อื่นๆ มี Indegree = 0 Root มี Outdegree = อื่นๆ มี Outdegree = N 1 N

  6. Leaf หรือ External nodeหมายถึง Node ที่มี Outdegreeเท่ากับ 0 A B C Leaf B, C

  7. Internal nodeหมายถึง Node ที่ไม่ใช่ Root และ Leaf A B C D E Root A Leaf D, E, C Internal node B

  8. Parent หมายถึง Node ที่มี Outdegree A B C D E Parent A, B

  9. Childหมายถึง Node ที่มี Indegree A B C D E Child B, C, D, E

  10. Siblingหมายถึง Node ที่มี Parent เดียวกัน A B C D E Sibling {B, C}, {D, E}

  11. Pathหมายถึง เส้นทางจาก Node หนึ่งไปยังอีก Node หนึ่ง A B C D E Path จาก A ไป E A -> B -> E ** ทุก Node ใน Tree จะต้องมี Path เดียวเท่านั้น **

  12. Ancestor หมายถึง ทุก Node ในเส้นทางจาก Root ไปยัง Node ที่ต้องการ A B C D E Ancestor ของ E A, B

  13. Descendentหมายถึง ทุก Node ในเส้นทางจาก Node ที่กำหนดไปจนถึง Leaf A B C D E Descendent ของ A B, C, D, E Descendent ของ B D,E

  14. Levelหมายถึง ระยะทางจาก Root Level 0 A Level 1 B C Level 2 D E

  15. Height ของ Tree หมายถึง Level สูงสุด ของ Leaf บวกด้วย 1 Level 0 A Level 1 B C Level 2 D E Height = 2 + 1 = 3

  16. Depth ของ Node หมายถึง ความยาวของ pathจาก root node ถึง node นั้น ดังนั้น root node จึงมีความลึก (Depth) เป็น 0 Depth ของ Tree หมายถึง ความลึกของ leaf node ที่อยู่ลึกที่สุด ซึ่งจะมีค่าเท่ากับความสูงของ treeเสมอ

  17. Parents Children Siblings Leaves Internal nodes Ancestor of G Descendent of A Height A,B,F B,E,F,C,D,G,H,I {B,E,F}, {C,D}, {G,H,I} C,D,E,G,H,I B,F A,F B,E,F,C,D,G,H,I 3

  18. Subtreeหมายถึง โครงสร้างที่เชื่อมต่อกันภายใต้ Root โดย Node แรกของ Subtreeจะเป็น Root ของ Subtreeนั้น และใช้เป็นชื่อเรียก Subtree Subtreeสามารถแบ่งย่อยเป็น Subtreeได้อีกจนกว่าจะ Empty

  19. Subtree

  20. 1. จากต้นไม้ต่อไปนี้ จงหา 1.1 Root 1.2 Leaves 1.3 Internal nodes 1.4 Ancestors of H 1.5 Descendents of F แบบฝึกหัด

  21. 2.จากต้นไม้ต่อไปนี้ จงหา 2.1 Indegree of node F 2.2 Outdegree of node G 2.3 Siblings of I 2.4 Parent of G 2.5 Children of C

  22. 3. จากต้นไม้ต่อไปนี้ จงหา 3.1 Height of the tree 3.2 Height of subtree G 3.3 Level of node I 3.4 Level of node A 3.5 Height of subtree E

  23. Binary Tree หมายถึง ต้นไม้ที่แต่ละ Node มี Subtree <= 2 หรือ Outdegree <= 2

  24. ตัวอย่าง Binary Tree

  25. Empty หรือ Null Tree

  26. Height of Binary Tree คำถามที่ 1 มี Node อยู่ทั้งหมด 7 Nodes 1.1 จะสร้าง Tree ให้มี Height สูงสุดได้เท่าไร อย่างไร 1.2 จะสร้าง Tree ให้มี Height ต่ำสุดได้เท่าไร อย่างไร Hmax = N Hmin = log2N + 1 จำนวนเต็มที่มากที่สุดที่น้อยกว่าหรือเท่ากับ log2N

  27. คำถามที่ 2 Tree ที่มี Height = 3 2.1 จะมี Node สูงสุดได้เท่าไร อย่างไร 2.2 จะมี Node ต่ำสุดได้เท่าไร อย่างไร Nmax = 2H - 1 Nmin = H

  28. Complete Binary Tree Binary Tree ที่มี Node เต็มทุก Level Nearly Complete Binary Tree Binary Tree ที่มี Node เต็มทุก Level ยกเว้น Level สุดท้าย และ Node ใน Level สุดท้ายอยู่เรียงกันทางซ้ายมือ

  29. Complete และ Nearly Complete Binary Tree

  30. Binary Tree Traversal 1. Depth-firstDescendent ทั้งหมดของ Child จะต้องถูกประมวลผลก่อน Child ถัดไป 2. Breath-firstประมวลผลทีละ Level จากบนลงล่าง

  31. Depth-first N L R L N R L R N

  32. แบบ Preorder N L R วิธีการของ Preorder (val root <node pointer>) 1 if (root is not null) 1 process(root) 2 preOrder(root->leftSubtree) 3 preOrder(root->rightSubtree) 2 return

  33. แบบ Inorder L N R algorithm inOrder (val root <node pointer>) 1 if (root is not null) 1 inOrder(root->leftSubtree) 2 process(root) 3 inOrder(root->rightSubtree) 2 return

  34. แบบ Postorder L R N algorithm postOrder (val root <node pointer>) 1 if (root is not null) 1 postOrder(root->leftSubtree) 2 postOrder(root->rightSubtree) 3 process(root) 2 return

  35. Breath-first algorithm breathFirst (val root <node pointer>) 1 p = root 2 while (p not null) 1 process(p) 2 if (p->left not null) 1 enqueue(p->left) 3 if (p->right not null) 1 enqueue(p->right) 4 if (not emptyQueue) 1 dequeue(p) else 1 p = null 3 return

  36. Binary Tree Application ExpressionTree Huffman Code Binary Search Tree

  37. Expression Tree หมายถึง Binary Tree ที่มีคุณสมบัติดังต่อไปนี้ 1.Leafเก็บOperand 2 Root และ Internal node เก็บOperator 3.Subtreeเป็นSub expression

  38. ตัวอย่าง

  39. การท่องใน Expression Tree 1. Preorder Traversal 2. Postorder Traversal 3. Inorder Traversal Prefix Expression Postfix Expression Infix Expression

  40. Preorder Traversal 1 if (root is not null) 1 print(root->token) 2 prefix(root->leftSubtree) 3 prefix(root->rightSubtree) 2 return + * a + b c d

  41. Postorder Traversal 1 if (root is not null) 1 postfix(root->leftSubtree) 2 postfix(root->rightSubtree) 3 print(root->token) 2 return a b c + * d +

  42. Inorder Traversal 1 if (root is not null) 1 if root->token is operand 1 print(root->token) else 1 print(open parenthesis) 2 infix(root->leftSubtree) 3 print(root->token) 4 infix(root->rightSubtree) 5 print(close parenthesis) 2 return ( ( a * ( b + c ) ) + d )

  43. การสร้าง Expression Tree จาก Postfix Expression พิจารณาทีละ Token จนหมด ถ้า Token เป็น Operand สร้าง Node แล้ว Push ลง Stack ถ้า Token เป็น Operator Pop ขึ้นมา 2 ตัวเชื่อมเป็น Tree โดยใช้ Operator แล้ว Push ลง Stack

  44. + * * / + C - 4 12 3 A B 5 2 ( 12 / 3 ) + ( 4 * ( 5 – 2 )) ( A + B ) * C

  45. นิพจน์ทางคณิตศาสตร์สามารถเขียนได้ 3 รูปแบบคือ infix , prefix , postfix เช่น (a + b) * c มี infix prefix postfix ซึ่งถ้าเราเปรียบเทียบกับ expression tree จะพบว่า inorder traversal (LNR) preorder traversal (NLR) postorder traversal (LRN) expression tree Vs Traversal * + c a b (a + b) * c *+abc ab+c* infix prefix postfix (a + b) * c *+abc ab+c*

  46. สร้างนิพจน์ prefix จาก expression tree algorithm prefix if (root is not null) print(root) prefix(root->leftSubtree) prefix(root->rightSubtree) return + * a + b c d

  47. สร้างนิพจน์ postfix จาก expression tree algorithm postfix if (root is not null) postfix(root->leftSubtree) postfix(root->rightSubtree) print(root) return a b c + * d +

  48. สร้างนิพจน์ infix จาก expression tree algorithm infix if (root is not null) if root is operand print(root) else print(open parenthesis) infix(root->leftSubtree) print(root) infix(root->rightSubtree) print(close parenthesis) return ( ( a * ( b + c ) ) + d )

  49. สร้าง expression tree จากนิพจน์ postfix • อ่านนิพจน์เข้ามาทีละตัวอักษร • ถ้าข้อมูลที่อ่านเข้ามาเป็น operand • สร้าง tree ใหม่จากข้อมูลที่อ่านเข้ามา (เป็น tree ที่มีแค่โหนดเดียว) • push ลง stack • ถ้าข้อมูลที่อ่านเข้ามาเป็น operator • pop ขึ้นมา 2 tree เชื่อมเป็น tree ใหม่โดยใช้ operator ที่อ่านเข้ามาเป็น root • push ลง stack

  50. b a a stack stack a b c + * d + initial read a read b stack

More Related