Tree2. Terminology
ต้นไม้ (Tree) ประกอบด้วย
1. Node A
ใช้เก็บข้อมูล
B C
2. Branch
ใช้เชือม Node เข้าด้วยกัน
่ Node A , B,
C
Branch A B,
AC
2
3. Degree หมายถึง จำานวน Branch ที่สัมพันธ์กับ
Node แบ่งเป็น A
1. Indegree
B C
หมายถึง Branch
ที่เข้าหา Node
Node A มี Degree
2. Outdegree เท่ากับ 2
หมายถึง Branch Indegree = 0
Outdegree = 2
ที่ออกจาก Node
3
6. Leaf หรือ External node หมายถึง Node ที่มี
Outdegree เท่ากับ 0
A
B C
L eaf B,
C
6
7. Internal node หมายถึง Node ที่ไม่ใช่ Root
และ Leaf A
B C
D E
Root A
L eaf
C D, E ,
Internal node B
7
10. Sibling หมายถึง Node ที่มี Parent เดียวกัน
A
B C
D E
Sibling {B, C },
{D, E } 10
11. Path หมายถึง เส้นทางจาก Node หนึงไปยังอีก
่
Node หนึ่ง A
B C
D E
Path จาก A ไป E A ->
B -> E
** ทุก Node ใน Tree จะต้องมี Path เดียวเท่านัน **
้
11
12. Ancestor หมายถึง ทุก Node ในเส้นทางจาก
Root ไปยัง Node ที่ตองการ
้ A
B C
D E
A ncestor ของ E
A, B
12
13. Descendent หมายถึง ทุก Node ในเส้นทางจาก
Node ที่กำาหนดไปจนถึง Leaf
A
B C
D E
Descendent ของ A B,
C , D, E
Descendent ของ B
13
14. Level หมายถึง ระยะทางจาก Root
A L evel
0
B C L evel
1
D E L evel
2
14
15. Height ของ Tree หมายถึง Level สูงสุด ของ
Leaf บวกด้วย 1 A L evel 0
B C L evel 1
D E L evel 2
Height = 2 + 1 =
3
15
16. Depth ของ Node หมายถึง ความยาวของ path
จาก root node ถึง node นั้น ดังนั้น root node
จึงมีความลึก (Depth) เป็น 0
Depth ของ Tree หมายถึง ความลึกของ leaf
node ที่อยู่ลึกที่สุด ซึ่งจะมีคาเท่ากับความสูงของ
่
tree เสมอ
16
17. Parents A,B,F
Children B,E,F,C,D,G,H,I
Siblings {B,E,F}, {C,D}, {G,H,I}
Leaves C,D,E,G,H,I
Internal nodes B,F
Ancestor of G A,F
Descendent of A B,E,F,C,D,G,H,I
Height 3
17
18. Subtree หมายถึง โครงสร้างที่เชื่อมต่อกัน
ภายใต้ Root โดย
Node แรกของ Subtree จะเป็น Root ของ
Subtree นัน และใช้เป็นชื่อเรียก Subtree
้
Subtree สามารถแบ่งย่อยเป็น Subtree ได้อก
ี
จนกว่าจะ Empty
18
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
21
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
22
26. Height of Binary Tree
คำาถามที่ 1
มี Node อยู่ทั้งหมด 7 Nodes
1.1 จะสร้าง Tree ให้มี Height สูงสุดได้เท่าไร อย่างไร
1.2 จะสร้าง Tree ให้มี Height ตำ่าสุดได้เท่าไร อย่างไร
Hmax = N
Hmin = log2N + 1
จำานวนเต็มที่มากที่สดที่น้อยกว่าหรือเท่ากับ log2N
ุ
26
27. คำาถามที่ 2
Tree ที่มี Height = 3
2.1 จะมี Node สูงสุดได้เท่าไร อย่างไร
2.2 จะมี Node ตำ่าสุดได้เท่าไร อย่างไร
Nmax = 2H - 1
Nmin = H
27
28. Complete Binary Tree
Binary Tree ที่มี Node เต็มทุก
Level
Nearly Complete Binary Tree
Binary Tree ทีมี Node เต็มทุก Level
่
ยกเว้น Level สุดท้าย และ Node ใน Level
สุดท้ายอยู่เรียงกันทางซ้ายมือ
28
30. This association suggests that a complete
binary tree can be implemented with an
array:
A B C D E R S F G L
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
30
31. Then the random-access property of arrays
allows quick access of parent from child
and children from parent.
A B C D E R S F G L
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
31
32. Parent at 0, children at 1, 2
Parent at 1, children at 3, 4
Parent at 2, children at 5, 6
…
Parent at i, children at ?
A B C D E R S F G L
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
32
33. A B C D E R S F G L
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Child at 1, parent at 0
Child at 2, parent at 0
Child at 3, parent at 1
Child at 4, parent at 1
Child at 5, parent at 2
Child at 6, parent at 2
…
Child at i, parent at ?
33
34. Binary Tree Traversal
1. Depth-first
Descendent ทั้งหมดของ Child จะต้องถูก
ประมวลผลก่อน Child ถัดไป
2. Breath-first
ประมวลผลทีละ Level จากบนลงล่าง
34
36. แบบ Preorder
NLR
algorithm preOrder (val root <node pointer>)
1 if (root is not null)
1 process(root)
2 preOrder(root->leftSubtree)
3 preOrder(root->rightSubtree)
2 return
36
37. แบบ Inorder
LNR
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
37
38. แบบ Postorder
LRN
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
38
39. 2. 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
39
43. 1. Expression Tree
หมายถึง Binary Tree ที่มีคุณสมบัติดังต่อไป
นี้
1. Leaf เก็บ Operand
2. Root และ Internal node เก็บ
Operator
3. Subtree เป็น Sub expression
43
45. การท่องใน Expression Tree
Prefix Expression
1. Preorder Traversal
2. Postorder Traversal Postfix Expression
3. Inorder Traversal Infix Expression
45
46. 1. Preorder Traversal
1 if (root is not null)
1 print(root->token)
2 prefix(root->leftSubtree)
3 prefix(root->rightSubtree)
2 return
+*a+bcd
46
47. 2. Postorder Traversal
1 if (root is not null)
1 postfix(root->leftSubtree)
2 postfix(root->rightSubtree)
3 print(root->token)
2 return
a bc+*d+
47
48. 3. 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)
48
49. การสร้าง Expression Tree จาก Postfix Expression
พิจารณาทีละ Token จนหมด
ถ้า Token เป็น Operand
สร้าง Node แล้ว Push ลง Stack
ถ้า Token เป็น Operator
Pop ขึ้นมา 2 ตัวเชื่อมเป็น Tree โดยใช้
Operator
แล้ว Push ลง Stack
49
52. 2. Huffman Code
Problem
บีบอัด file โดยไม่ให้เสียข้อมูลไปเลยได้อย่างไร
ให้ M เป็น ข้อมูลที่เราต้องการบีบอัด สมมติว่ามันมี
ขนาด 100,000 characters ซึ่งประกอบไปด้วยตัว
อักษร a ถึง e เท่านัน้
สมมติให้ 1 character ใช้ 1 byte (8 bits)
ฉะนัน 100,000 characters ใช้ 800,000 bits
้
เราสามารถลดจำานวน bits ทีใช้แต่ละตัว
่
อักษรได้หรือไม่? 52
53. เนื่องด้วยมีตัวอักษรเพียง 5 ตัว ดังนั้น
สมมติให้ 1 character ใช้ 3 bits
‘a’ 000
‘b’ 001
‘c’ 010
‘d’ 011
‘e’ 100
ดังนั้น จำานวน bits ที่ใช้จะลดลง
เหลือ 300,000 bits
53
54. ถ้าลดจำานวนบิตลงอีก
a’ 0 จะเห็นว่า
’b’ 1
’c’ 00 จำานวน bits ที่ใช้จะลด
’d’ 01 ลง สมมติข้อมูล
แต่
’e’ 10 001010
สามารถเป็นได้ทั้ง
aababa หรือ cbda
54
55. ใช้ Huffman Code
เป็นรหัสแทนตัวอักษรที่แต่ละตัวอักษรมีความ
ยาวของรหัสแตกต่างกัน
โดยตัวอักษรที่ใช้บ่อย จะมีขนาดสั้น
ตัวอักษรที่ใช้น้อย จะมีขนาดยาว
ทังนี้เพื่อทำาให้ข้อมูลมีขนาดเล็กลง
้
55
60. 1 0
ดังนั้นจะได้
1 0 1 0
a 00001
1 0 1 0 b 00000
c 0001
1 0 d 011
e 010
1 0 f 001
g 11
h 10
60
62. General Tree
หมายถึง Tree ที่สามารถมี Outdegree ได้
ไม่จำากัดจำานวน
62
63. การแปลง General Tree เป็น Binary Tree
มี 3 ขั้นตอน ดังนี้
1. ระบุ Child ที่อยู่ทางซ้ายสุด
2. เชื่อม Sibling เข้าด้วยกัน
3. ลบ Branch ที่ไม่ต้องการ
63
Editor's Notes ในเมื่อเรารู้ว่ามีแค่ไม่กี่จำนวน เราก็กำหนดโค้ดเองได้ดังในแผ่นใส อย่างนี้ถึงจะ compress ได้ แต่ก็ไม่มีทางตีความกลับคืนได้ เพราะความหมายไม่แน่นอน