Transcript + x
Trees Faculty of Informatics, BUU 1 Introduction to Trees • ต ้นไม้ (tree) สามารถใช ้จาลองโครงสร ้างต่างๆได ้ เช่น ่ ว โครงสร ้างขององค ์กร โครงสร ้างของสารประกอบอิมตั ระบบแฟ้ มในคอมพิวเตอร ์ เป็ นต ้น ่ • ต ้นไม้(tree) คือกราฟเชือมต่ อแบบไม่มท ี ศ ิ ทาง(connected ่ มวี งจร undirected graph) ทีไม่ – Theorem: มีทางเดินอย่างง่ายเพียงเส ้นทางเดียวระหว่างสอง โหนด(จุด)ใดๆ • ป่ า(forest)คือเซตของต ้นไม้ทไม่ ี่ มโี หนด(จุด)ร่วมกัน ่ น pendant หรือ • จุดใบไม้(leaf) 2ในต ้นไม้หรือป่ า เป็ นจุดทีเป็ จุดสันโดษ(isolated) จุดภายใน(internal)คือจุดใดๆที่ ไม่ใช่จด ุ ใบ (ดังนั้นจุดภายในมีดก ี รี ≥ ___ ) Faculty of Informatics, BUU 2 กราฟรูปใดต่อไปนีเ้ ป็ นต ้นไม ้ Faculty of Informatics, BUU 3 Forest Faculty of Informatics, BUU 4 Basic Definition ้ • ในวิชาเคมี ใชกราฟแทนโมเลกุ ล – จุดยอดของกราฟแทนอะตอม – แต่ละด ้านแทนพันธะ(bond) ระหว่างอะตอม Faculty of Informatics, BUU 5 Rooted Trees • ต้นไม้ทมี ี่ ราก(rooted tree) คือ ต ้นไม้ทมี ี่ การระบุให ้จุดใดจุด ่ หนึ่ งเป็ นราก(root)โดยทุกๆด ้านจะมีทศ ิ ทางออกจากจุดราก เมือ กาหนดให ้จุดๆ หนึ่ งเป็ นรากของต ้นไม้แล ้ว เราสามารถจะ ่ กาหนดทางเดินทีเฉพาะเจาะจง(unique) จากรากไปยังจุดยอด ่ ๆ ในต ้นไม้ได ้เสมอ อืน ่ ดียnวกั ้นไม้เมี นแต่ จะสามารถสร ้างเป็ นต ้นไม้ทมี • สังเกตว่าต ้นไม้ทไม่ ี่ มต รี ากที โหนด ี่ ่ เลือากจุ รากได ้ n ต ้นที แตกต่ งกัดนรากคนละจุดกัน root root จุดใบแทนด ้วยจุดสีเขียว จุดภายในแทนด ้วยจุดสีน้าตาล Faculty of Informatics, BUU 6 Rooted Trees • Example Faculty of Informatics, BUU 7 ่ ยวข้ ่ คาศ ัพท ์ทีเกี องกับต้นไม้ พ่อ/แม่ (Parent) ถ ้า T เป็ นต ้นไม้แบบมีราก และ v เป็ นจุดใน T ที่ ่ ไม่ใช่ราก แล ้วพ่อ/แม่ของจุด v คือ จุด u เมือมี ทางเดินจากจุด u ลงมาหาจุด v โดย u และ v ประชิดกัน และ u อยูใ่ กล ้รากมากกว่า v ลู ก (Child) ่ u เป็ นพ่อ/แม่ของ v แล ้วเราเรียก v ว่าลูกของ เมือ u ่ องก ัน (Siblings) พีน้ ่ พ่อ/แม่เดียวกัน จุด 2 จุดหรือมากกว่า 2 จุด ทีมี Faculty of Informatics, BUU 8 ่ ยวข้ ่ คาศ ัพท ์ทีเกี องกับต้นไม้ บรรพบุรุษ (Ancestor) ่ ใช่รากของต ้นไม้ คือจุดทุก บรรพบุรษ ุ ของจุดใด ๆ ทีไม่ ่ บ จุดทีอยู ่ นวิถจี ากรากจนถึงจุดนั้น ลู กหลาน (Descendant) ่ v เป็ นบรรพ ลูกหลานของจุด v คือ จุดทุก ๆ จุดทีมี บุรษ ุ ใบ (Leaf) ่ มล จุดใด ๆ ทีไม่ ี ก ู เรียกว่าใบ จุดภายใน (Internal vertex/nodes) ่ ลก จุดทุกจุดทีมี ู คือจุดภายในหรือเราอาจกล่าวได ้ว่า ่ เป็ นใบ จุดภายในคือจุดทีไม่ Faculty of Informatics, BUU 9 Rooted-Tree Terminology Example root node a internal vertex parent of g c b d e f leaf h i Faculty of Informatics, BUU g siblings 10 Rooted-Tree Terminology Example ancestors of h and i a c b d e h f g i Faculty of Informatics, BUU 11 Rooted-Tree Terminology Exercise c o • จงหาจุด parent, k, l children, siblings, g a, c ancestors, และ b l, k, q descendants ของโหนด f e n h d r m root a c g q i f p j k Faculty of Informatics, BUU l 12 m-ary trees • ต ้นไมท้ มี ี่ ราก จะเรียกว่า m ภาค(m-ary tree) ถ ้าทุกๆจุดมีลก ู (children)ไม่มากกว่า m จุด และ เรียกว่าต ้นไม้ m ภาคเต็ม ต ้น(full m-ary tree) ถ ้าทุกๆจุดภายในมีลก ู เท่ากับ m จุด • ต ้นไม ้ 2-ary(มีลก ู ไม่เกิน 2 จุด) จะเรียกว่าต ้นไม้ทวิภาค (binary tree) ่ กๆจุด • ทฤษฎี ี่ รากเป็ น2 binary tree ก็ตอ ่ เมือทุ 3 : ต ้นไม้ทมี (ยกเว ้นจุดราก) มีดก ี รี ≤ ___, และจุดรากมีดก ี รี ≤ ___ Faculty of Informatics, BUU 13 ต้นใดเป็ นต้นไม้แบบ m ภาคเต็ม ต้น? Full 2–ary tree Full 3–ary tree Full 5–ary tree Not full 3–ary tree Faculty of Informatics, BUU 14 Some Tree Theorems ้ั • ทฤษฎีบท 1 ต ้นไม้ทมี ี่ ทงหมด n จุด จะมีจานวนกิง่ (ด ้าน) e = n−1 ด ้าน Faculty of Informatics, BUU 15 Some Tree Theorems • ทฤษฎีบท 2 ต ้นไม้ m ภาคเต็มต ้น(full m-ary ่ จด tree) ทีมี ุ ภายในจานวน i จุด จะมีจานวนจุด ้ ทังหมด n=mi+1 จุด และมีจด ุ ใบ =(m−1)i+1 จุด Faculty of Informatics, BUU 16 Some Tree Theorems • ทฤษฎีบท 3 ต ้นไม ้แบบ m ภาคทีม ่ จ ี ด ุ จุดและมีจด ุ ภายในจานวน i จุดและมีจด ุ ใบ จานวน จุด จะได ้ว่า • i = (n-1)/m และ = [(m-1)n+1]/m • n = mi + 1 จุด และ = (m-1)i + 1 • n = (m - 1)/(m-1) และ i = ( -1)/(m-1) Faculty of Informatics, BUU 17 ตัวอย่าง • นางสาวตะวันฉายเป็ นผู ้เริม ่ สง่ จดหมายลูกโซ ่ อวยพรวันเกิดออกไปยังเพือ ่ น ๆ และขอร ้อง ว่าให ้แต่ละคนทีไ่ ด ้รับจดหมายสง่ จดหมาย ต่อไปถึงคนอืน ่ ๆ อีก 4 คน อยากทราบว่า จะมีคนทัง้ หมดกีค ่ นทีไ่ ด ้อ่านจดหมายฉบับนี้ รวมทัง้ นางสาวตะวันฉาย ถ ้ากาหนดว่าไม่ม ี ใครได ้รับจดหมายเกินหนึง่ ฉบับ และเมือ ่ ่ น ิ้ สุดลง มี กระบวนการสง่ จดหมายลูกโซส ทัง้ หมด 100 คนทีไ่ ด ้อ่านจดหมายนีแ ้ ต่ไม่ ยอมสง่ จดหมายต่อ Faculty of Informatics, BUU 18 ตัวอย่าง กระบวนการสง่ จดหมายลูกโซเ่ ปรียบได ้กับต ้นไม ้ 4 ภาค และจุดภายในคือ คนทีส ่ ง่ จดหมายต่อใน ขณะทีใ่ บคือผู ้ทีไ่ ด ้รับจดหมายแล ้วไม่สง่ ต่อ ให ้ i แทนจานวนจุดภายใน และ n แทนจานวนคน ทัง้ หมด ในทีน ่ ี้ m = 4 จากทฤษฎี จะได ้ว่า n = (m -1)/m-1 = [(4 100)-1]/(4-1) = 133 ดังนัน ้ มีผู ้ได ้รับจดหมายทัง้ หมด 133 คน และ i = ( -1)/(m-1) = (100-1)/(4-1) = 33 ดังนั น ้ จานวนคนทีส ่ ง่ จดหมายต่อมีทงั ้ หมด 33 คน Faculty of Informatics, BUU 19 Some More Tree Theorems • นิ ยาม: ระดับ(level) ของจุดคือความยาวของ ทางเดินอย่างง่ายจากราก(root)ไปยังจุดนัน ้ level 2 level 3 Faculty of Informatics, BUU 20 Some More Tree Theorems • ความสูง(height) ของต ้นไม ้ คือระดับของจุดทีม ่ าก ทีส ่ ด ุ ระด ับของราก (จุด a) = 0 ระด ับของจุด b, j, k = 1 ระดับของจุด c, e, f, l = 2 ระด ับของจุด d, g, i, m, n = 3 ระดับของจุด h = 4 of Informatics, BUU ้ ความสู งของต้Faculty ด ังนัน นไม้ T = 4 21 Some More Tree Theorems • ต ้นไม ้ m ภาค(m-ary tree) ทีม ่ รี าก ทีม ่ ค ี วามสูง h เรียกว่าต ้นไม ้สมดุล(balanced) ถ ้าจุดใบทุกจุดอยูท ่ ี่ ระดับ h หรือ h−1 • ทฤษฎีบท: มีจานวนใบอย่างมากทีส ่ ด ุ mh ใบ ใน ต ้นไม ้ m ภาค(m-ary tree) ทีม ่ ค ี วามสูง h – ทฤษฎีบทย่อย : ต ้นไม ้ m-ary ทีม ่ จ ี ด ุ ใบ ใบมีความ สูง h≥logm ถ ้าเป็ นต ้นไม ้เต็มกิง่ (full m-ary) และสมดุลความสูง h=logm Faculty of Informatics, BUU 22 Applications of Trees Binary Search Tree Format • ข ้อมูลแต่ละตัวจะถูก ตัวอย่าง: เก็บลงในจุดของต ้นไม ้ โดยการจัดเรียงข ้อมูล 3 ในต ้นไม ้สาหรับทุกๆค่า x คือ, – ทุกๆจุดทีอ ่ ยูใ่ นต ้นไม ้ 1 5 ้ ย่อยด ้านซาย(left subtree)ของ x จะมีคา่ น ้อยกว่า x 0 2 – ทุกๆจุดทีอ ่ ยูใ่ นต ้นไม ้ ย่อยด ้านขวา(right subtree)ของ x จะมีคFaculty า่ of Informatics, BUU 7 12 9 8 15 11 23 Applications of Trees :Codes Codes That Can Be Decoded Fixed-length codes: – Every character is encoded using the same number of bits. – To determine the boundaries between characters, we form groups of w bits, where w is the length of a character. – Examples: ■ ASCII Prefix codes: – No character is the prefix of another character. – Examples: ■ Huffman codes Faculty of Informatics, BUU 24 Why Prefix Codes ? • พิจารณาการกาหนดรหัสซงึ่ ไม่เป็ น a prefix code: a = 01 m = 10 n = 111 o = 0 r = 11 s = 1 t = 0011 • ดังนัน ้ ถ ้าท่านสง่ ข ้อความ “You are a star.”ไปให ้เพือ ่ น โดยท่านกาหนดรหัสของ “star” คือ “1 0011 01 11” • เมือ ่ เพือ ่ นท่านได ้รับข ้อความและเกิด Faculty of Informatics, BUU 25 Representing a Prefix-Code Dictionary Our example : ‹space› = 000 A = 0010 E = 0011 s = 010 c = 0110 g = 0111 h = 1000 i = 1001 l = 1010 t = 1011 e = 110 n = 111 1 0 0 0 1 1 0 s ‹spc› 0 A 1 E 0 1 0 c 0 1 g 0 h Faculty of Informatics, BUU 1 i 1 1 0 l 0 e 1 n 1 t 26 Huffman code เป็ นรหัสทีม ่ ค ี วามยาวไม่คงทีแ ่ ละมีน้ าหนักน ้อยทีส ่ ด ุ ใน บรรดารหัสprefix code ขัน ้ ตอนวิธข ี องการสร ้าง Huffman code โดยใชต้ ้นไม ้ ทวิภาคมีดงั นี้ 1. เขียนตัวอักษรและความถีก ่ ากับจุดยอดแต่ละจุดแล ้ว เรียงความถีจ ่ ากน ้อยไปหามาก ื่ มจุดยอดทางซาย ้ 2 จุดกับจุดยอดใหม่ กากับจุด 2. เชอ ยอดใหม่ด ้วยผลบวกของความถี่ 3. เรียงความถีจ ่ ากน ้อยไปหามาก 4. ทาขัน ้ ตอนที2 ่ และ 3 ซ้าไปเรือ ่ ยๆจนจุดยอดทัง้ หมด ื่ มกันเป็ นกราฟต ้นไม ้ทวิภาค เชอ Faculty of Informatics, BUU 27 Applications of Trees • Q: Use Huffman coding to encode the following five symbols with given frequencies: A: 45 , B: 13 ,C: 12 , D: 16 , E: 9, F: 5 f:5 e:9 c:12 b:13 d:16 Faculty of Informatics, BUU a:45 28 Huffman’s Algorithm 14 f:5 e:9 c:12 b:13 Faculty of Informatics, BUU d:16 a:45 29 Huffman’s Algorithm c:12 14 b:13 f:5 d:16 a:45 e:9 Faculty of Informatics, BUU 30 Huffman’s Algorithm 25 c:12 14 b:13 f:5 d:16 a:45 e:9 Faculty of Informatics, BUU 31 Huffman’s Algorithm 14 f:5 25 d:16 e:9 c:12 Faculty of Informatics, BUU a:45 b:13 32 Huffman’s Algorithm 30 14 f:5 25 d:16 e:9 c:12 Faculty of Informatics, BUU a:45 b:13 33 Huffman’s Algorithm 25 c:12 30 14 b:13 f:5 a:45 d:16 e:9 Faculty of Informatics, BUU 34 Huffman’s Algorithm 55 25 c:12 30 14 b:13 f:5 a:45 d:16 e:9 Faculty of Informatics, BUU 35 Huffman’s Algorithm 55 a:45 25 c:12 30 14 b:13 f:5 d:16 e:9 Faculty of Informatics, BUU 36 Huffman’s Algorithm 100 0 1 55 a:45 0 0 1 25 0 30 1 0 1 c:12 b:13 14 d:16 100 101 0 1 111 f:5 e:9 1100 1101 Faculty of Informatics, BUU 37 Tree Traversal • Is a procedure that systematically visits every vertex of an ordered rooted tree. – Visiting a vertex = processing the data at the vertex. • อัลกอริธม ึ ในการท่องไปบนต ้นไม ้(Traversal algorithms) • Pre-order traversal NLR (node,left,right) • In-order traversal LNR (left,node,right) • Post-order traversal LRN (left,right, node) Faculty of Informatics, BUU 38 Tree Traversal • Let T be an ordered rooted tree with root r. Suppose that T1, T2, …, Tn are the subtrees at r from left to right in T. 55 T1 r 24 3 49 46 45 94 T2 60 52 58 88 76 Faculty of Informatics, BUU 91 39 Preorder Traversal • Begins by visiting r. • Next traverses T1 in preorder, then T2 in preorder, …, then Tn in preorder. • Ends after Tn has been traversed. Faculty of Informatics, BUU 40 Preorder Traversal • Visit root, visit subtrees left to right. Faculty of Informatics, BUU 41 Preorder Traversal Faculty of Informatics, BUU 42 Inorder Traversal • Begins by traversing T1 in inorder • Next visit r, then traverses T2 in inorder, …, then Tn in inorder. • Ends after Tn has been traversed. Faculty of Informatics, BUU 43 Inorder Traversal • Visit leftmost subtree,visit root, visit other subtrees left to right. Faculty of Informatics, BUU 44 Inorder Traversal Faculty of Informatics, BUU 45 Postorder Traversal • Begins by traversing T1 in postorder • Next traverses T2 in postorder,then T3 in postorder,...,then Tn in postorder • End by visiting r Faculty of Informatics, BUU 46 Postorder Traversal • Visit subtree left to right,visit root. Faculty of Informatics, BUU 47 Postorder Traversal Faculty of Informatics, BUU 48 Postorder Traversal • Applications: compute space used by files in a directory and its subdirectories. Faculty of Informatics, BUU 49 Tree Traversal Shortcut • Preorder list is obtained by – listing each vertex the first time this curve passes it. – Result: a, b, d, h, e, i, j, c, f, g, k. • Inorderlist is obtained by – listing a leaf the first time the curve passes it and – listing each internal vertex the second time the curve passes it. – Result: h, d, b, i, e, j, a, f, c, k, g. • Postorderlist is obtained by – listing a vertex the last time it is passes on the way back up to its parent. – Result: h, d, i, j, e, b, f, k, g, c, a. Faculty of Informatics, BUU 50 Representing Arithmetic Expressions • Complicated arithmetic expressions can be represented by an ordered rooted tree – Internal vertices represent operators – Leaves represent operands • Build the tree bottom-up – Construct smaller subtrees – Incorporate the smaller subtrees as part of larger subtrees Faculty of Informatics, BUU 51 Example (x+y)2 + (x-3)/(y+2) + + x / 2 y – + x 3 y 2 Faculty of Informatics, BUU 52 Infix Notation • Traverse in inorder adding parentheses for each operation + + x / 2 y – + x 3 y 2 ( ((x + y ) 2 ) +( ( x – 3 ) / ( y + 2 ) ) ) Faculty of Informatics, BUU 53 Prefix Notation (Polish Notation) • Traverse in preorder + + x / 2 y – + x 3 y 2 + + x y 2 / – x 3 + y 2 Faculty of Informatics, BUU 54 Evaluating Prefix Notation • In an prefix expression, a binary operator precedes its two operands • The expression is evaluated right-left • Look for the first operator from the right • Evaluate the operator with the two operands immediately to its right Faculty of Informatics, BUU 55 Example + / + 2 2 2 / – 3 2 + 1 0 + / + 2 2 2 / – 3 2 1 + / + 2 2 2 / 1 1 + / + 2 2 2 1 + / 4 2 1 + 2 1 3 Faculty of Informatics, BUU 56 Postfix Notation (Reverse Polish) • Traverse in postorder + + x / 2 y – + x 3 y 2 x y + 2 x 3 – y 2 + /+ Faculty of Informatics, BUU 57 Evaluating Postfix Notation • In an postfix expression, a binary operator follows its two operands • The expression is evaluated left-right • Look for the first operator from the left • Evaluate the operator with the two operands immediately to its left Faculty of Informatics, BUU 58 Example 2 2 + 2 / 3 2 – 1 0 + / + 4 2 / 3 2 – 1 0 + / + 2 3 2 – 1 0 + / + 2 1 1 0 + / + 2 1 1 / + 2 1 + 3 Faculty of Informatics, BUU 59 Tree Traversal + prefix expression 7 infix expression ^ 5 (- ( + 5(^ ( + 8 (* 3 4 )) 2 ))7 ) ((5+((8+(3*4)) 2 + 8 * 3 ^2))-7) postfix expression 5 8 3 4 * + 2 ^ + 7 - 4 Faculty of Informatics, BUU 60