SlideShare a Scribd company logo
1 of 64
LOGO


โครงสร้างข้อมูลทรีและ
      ไบนารีทรี
 (Tree and Binary
       Tree)


                          1
Terminology

     ต้นไม้ (Tree) ประกอบด้วย
    1. Node                            A

      ใช้เก็บข้อมูล
                                   B       C
    2. Branch
      ใช้เชือม Node เข้าด้วยกัน
            ่                     Node A , B,
                                  C
                                  Branch A B,
                                  AC
                      2
 Degree หมายถึง จำานวน Branch ที่สัมพันธ์กับ
   Node แบ่งเป็น                   A

   1. Indegree
                              B         C
      หมายถึง Branch
      ที่เข้าหา Node
                           Node A มี Degree
   2. Outdegree                เท่ากับ 2
      หมายถึง Branch          Indegree = 0
                              Outdegree = 2
      ที่ออกจาก Node
                       3
 Root หมายถึง Node แรกของ Tree


                         Root

                 A

            B            C




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


                             5
 Leaf หรือ External node หมายถึง Node ที่มี
  Outdegree เท่ากับ 0


                      A

                 B        C


                L eaf B,
                C
                     6
 Internal node หมายถึง Node ที่ไม่ใช่ Root
  และ Leaf          A

                  B           C

            D             E

          Root        A
          L eaf
          C           D, E ,
          Internal node B
                      7
 Parent หมายถึง Node ที่มี Outdegree
                     A

                  B           C

             D            E


            Parent            A,
            B
                      8
 Child หมายถึง Node ที่มี Indegree

                           A

                   B           C

             D             E


            C hild         B, C , D,
            E
                       9
 Sibling หมายถึง Node ที่มี Parent เดียวกัน


                             A

                    B            C

              D              E


            Sibling {B, C },
            {D, E }     10
 Path หมายถึง เส้นทางจาก Node หนึงไปยังอีก
                                  ่
  Node หนึ่ง          A

                        B            C

                D                E

       Path จาก A ไป E                      A ->
       B -> E
      ** ทุก Node ใน Tree จะต้องมี Path เดียวเท่านัน **
                                                   ้

                            11
 Ancestor หมายถึง ทุก Node ในเส้นทางจาก
  Root ไปยัง Node ที่ตองการ
                      ้ A


                  B         C

             D          E


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

                    B          C

              D           E


          Descendent ของ A         B,
          C , D, E
          Descendent ของ B
                     13
 Level หมายถึง ระยะทางจาก Root

                  A         L evel
                               0
              B         C   L evel
                               1
         D         E        L evel
                               2


                   14
 Height ของ Tree หมายถึง Level สูงสุด ของ
  Leaf บวกด้วย 1      A           L evel 0

                  B         C    L evel 1
             D          E        L evel 2

           Height = 2 + 1 =
                  3
                   15
 Depth ของ Node หมายถึง ความยาวของ path
  จาก root node ถึง node นั้น ดังนั้น root node
  จึงมีความลึก (Depth) เป็น 0


 Depth ของ Tree หมายถึง ความลึกของ leaf
  node ที่อยู่ลึกที่สุด ซึ่งจะมีคาเท่ากับความสูงของ
                                 ่
  tree เสมอ



                       16
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
 Subtree หมายถึง โครงสร้างที่เชื่อมต่อกัน
   ภายใต้ Root โดย
       Node แรกของ Subtree จะเป็น Root ของ
        Subtree นัน และใช้เป็นชื่อเรียก Subtree
                  ้

       Subtree สามารถแบ่งย่อยเป็น Subtree ได้อก
                                               ี
        จนกว่าจะ Empty

                       18
Subtree




 19
แบบฝึกหัด



1. จากต้นไม้ตอไปนี้ จงหา 1.1 Root
             ่

                         1.2 Leaves

                         1.3 Internal nodes

                        1.4 Ancestors of H

                        1.5 Descendents of F


                   20
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
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
Binary Tree


หมายถึง ต้นไม้ที่แต่ละ Node มี

       Subtree     <= 2

หรือ

       Outdegree   <=     2



             23
ตัวอย่าง        Binary Tree




           24
E mpty
  หรือ
Null Tree




            25
Height of Binary Tree


คำาถามที่ 1
มี Node อยู่ทั้งหมด 7 Nodes
1.1 จะสร้าง Tree ให้มี Height สูงสุดได้เท่าไร อย่างไร
1.2 จะสร้าง Tree ให้มี Height ตำ่าสุดได้เท่าไร อย่างไร


          Hmax           =            N

          Hmin           =            log2N + 1


                 จำานวนเต็มที่มากที่สดที่น้อยกว่าหรือเท่ากับ log2N
                                     ุ

                                26
คำาถามที่ 2
Tree ที่มี Height = 3
2.1 จะมี Node สูงสุดได้เท่าไร อย่างไร
2.2 จะมี Node ตำ่าสุดได้เท่าไร อย่างไร


          Nmax    =          2H - 1
          Nmin    =          H


                        27
Complete Binary Tree
    Binary Tree ที่มี Node เต็มทุก
Level
Nearly Complete Binary Tree

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

                   28
Complete และ Nearly Complete Binary Tree




                 29
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
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
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
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
Binary Tree Traversal


1. Depth-first
   Descendent ทั้งหมดของ Child จะต้องถูก
   ประมวลผลก่อน Child ถัดไป

2. Breath-first
   ประมวลผลทีละ Level จากบนลงล่าง




                   34
1. Depth-first




NLR             LNR    LRN



           35
แบบ 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
แบบ 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
แบบ 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
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
แบบฝึกหัด
1. จากต้นไม้ตอไปนี้ จงหาลำาดับการประมวลผล
             ่
   แบบ Depth-first ทั้งหมด
                                    A


                   B                        C


           D               E            R       S


       F       G       L




                               40
2. จากต้นไม้ต่อไปนี้ จงหาลำาดับการประมวลผลแบบ
   Breath-first
                                   A


                  B                        C


          D               E            R       S


      F       G       L




                              41
Binary Tree Application

  1.    Expression Tree
   2.   Huffman Code




            42
1. Expression Tree


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


                   43
ตัวอย่าง




   44
การท่องใน Expression Tree

                         Prefix Expression
1. Preorder Traversal

2. Postorder Traversal   Postfix Expression

3. Inorder Traversal     Infix Expression




                 45
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
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
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
การสร้าง Expression Tree จาก Postfix Expression


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

จากนิพจน์ Postfix
    abc+*d+
จะได้ Expression Tree ดังนี้




                    50
แบบฝึกหัด


จงแสดง Expression Tree
จากนิพจน์ Postfix ทีกำาหนด
                    ่
ให้ A B * C D / + E F - *




            51
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
เนื่องด้วยมีตัวอักษรเพียง 5 ตัว ดังนั้น
สมมติให้ 1 character ใช้ 3 bits
     ‘a’      000
     ‘b’      001
     ‘c’      010
     ‘d’      011
     ‘e’      100

  ดังนั้น จำานวน bits ที่ใช้จะลดลง
  เหลือ 300,000 bits
                     53
ถ้าลดจำานวนบิตลงอีก

 a’      0    จะเห็นว่า
’b’      1
’c’     00    จำานวน bits ที่ใช้จะลด
’d’     01    ลง สมมติข้อมูล
              แต่
’e’     10    001010
              สามารถเป็นได้ทั้ง
              aababa หรือ cbda
              54
ใช้ Huffman Code

 เป็นรหัสแทนตัวอักษรที่แต่ละตัวอักษรมีความ
   ยาวของรหัสแตกต่างกัน
 โดยตัวอักษรที่ใช้บ่อย จะมีขนาดสั้น
       ตัวอักษรที่ใช้น้อย จะมีขนาดยาว
 ทังนี้เพื่อทำาให้ข้อมูลมีขนาดเล็กลง
    ้




                    55
ตัวอย่าง การสร้าง Huffman Tree เพื่อหา Huffman Code


 สมมติให้




                       56
57
58
59
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
แบบฝึกหัด
กำาหนดความถีของการใช้งานตัวอักษรต่าง ๆ ดังนี้
            ่
     A = 30% C = 15%            E = 25%
     B = 15% D = 10%            F = 5%
จงสร้าง Huffman Code แทนตัวอักษรดังกล่าว




                     61
General Tree




    หมายถึง Tree ที่สามารถมี Outdegree ได้
ไม่จำากัดจำานวน




                      62
การแปลง General Tree เป็น Binary Tree

  มี 3 ขั้นตอน ดังนี้
  1. ระบุ Child ที่อยู่ทางซ้ายสุด
  2. เชื่อม Sibling เข้าด้วยกัน
  3. ลบ Branch ที่ไม่ต้องการ




                        63
ตัวอย่าง การแปลง General Tree เป็น Binary Tree




                    64

More Related Content

What's hot

คู่มือการเขียนรายงาน การอ้างอิง การประกวดโครงงานวิทยาศาสตร์ สมาคมวิทยาศาสตร์ ...
คู่มือการเขียนรายงาน การอ้างอิง การประกวดโครงงานวิทยาศาสตร์ สมาคมวิทยาศาสตร์ ...คู่มือการเขียนรายงาน การอ้างอิง การประกวดโครงงานวิทยาศาสตร์ สมาคมวิทยาศาสตร์ ...
คู่มือการเขียนรายงาน การอ้างอิง การประกวดโครงงานวิทยาศาสตร์ สมาคมวิทยาศาสตร์ ...Totsaporn Inthanin
 
การคำนวณกระจกนูน
การคำนวณกระจกนูนการคำนวณกระจกนูน
การคำนวณกระจกนูนsripai52
 
การศึกษาที่ฮอว์ธอร์น
การศึกษาที่ฮอว์ธอร์นการศึกษาที่ฮอว์ธอร์น
การศึกษาที่ฮอว์ธอร์นwiraja
 
รายงานโครงการเครื่องจักสานงานไม้ไผ่ภูมิปัญญาไทอีสาน2558ศพว2
รายงานโครงการเครื่องจักสานงานไม้ไผ่ภูมิปัญญาไทอีสาน2558ศพว2รายงานโครงการเครื่องจักสานงานไม้ไผ่ภูมิปัญญาไทอีสาน2558ศพว2
รายงานโครงการเครื่องจักสานงานไม้ไผ่ภูมิปัญญาไทอีสาน2558ศพว2Sircom Smarnbua
 
บทที่ 4 หลักการทำงานของระบบคอมพิวเตอร์
บทที่ 4 หลักการทำงานของระบบคอมพิวเตอร์บทที่ 4 หลักการทำงานของระบบคอมพิวเตอร์
บทที่ 4 หลักการทำงานของระบบคอมพิวเตอร์russana
 
การพัฒนาตามแนวพระราชดำริ
การพัฒนาตามแนวพระราชดำริการพัฒนาตามแนวพระราชดำริ
การพัฒนาตามแนวพระราชดำริjeabjeabloei
 
ตัวอย่างโครงการสอน
ตัวอย่างโครงการสอนตัวอย่างโครงการสอน
ตัวอย่างโครงการสอนKrupol Phato
 
ใบความรู้แผ่นพับ
ใบความรู้แผ่นพับใบความรู้แผ่นพับ
ใบความรู้แผ่นพับdevilp Nnop
 
แผ่นพับเสนอผลงานนวัตกรรม หน้า 1
แผ่นพับเสนอผลงานนวัตกรรม หน้า 1แผ่นพับเสนอผลงานนวัตกรรม หน้า 1
แผ่นพับเสนอผลงานนวัตกรรม หน้า 1krupornpana55
 
5.7อนุกรมเลขคณิตแก้ไข
5.7อนุกรมเลขคณิตแก้ไข5.7อนุกรมเลขคณิตแก้ไข
5.7อนุกรมเลขคณิตแก้ไขKrudodo Banjetjet
 
โครงงานชาผักสมุนไพรพื้นบ้าน
โครงงานชาผักสมุนไพรพื้นบ้านโครงงานชาผักสมุนไพรพื้นบ้าน
โครงงานชาผักสมุนไพรพื้นบ้านChok Ke
 
04แผน เรื่อง กำลัง
04แผน เรื่อง กำลัง04แผน เรื่อง กำลัง
04แผน เรื่อง กำลังWijitta DevilTeacher
 
1.4 สถิติเชิงอนุมาน
1.4 สถิติเชิงอนุมาน1.4 สถิติเชิงอนุมาน
1.4 สถิติเชิงอนุมานSomporn Amornwech
 
โครงงานสมบูรณ์
โครงงานสมบูรณ์โครงงานสมบูรณ์
โครงงานสมบูรณ์Kantisa Motalee
 
รูปเล่มวิชาโครงงาน
รูปเล่มวิชาโครงงานรูปเล่มวิชาโครงงาน
รูปเล่มวิชาโครงงานAjBenny Pong
 

What's hot (20)

คู่มือการเขียนรายงาน การอ้างอิง การประกวดโครงงานวิทยาศาสตร์ สมาคมวิทยาศาสตร์ ...
คู่มือการเขียนรายงาน การอ้างอิง การประกวดโครงงานวิทยาศาสตร์ สมาคมวิทยาศาสตร์ ...คู่มือการเขียนรายงาน การอ้างอิง การประกวดโครงงานวิทยาศาสตร์ สมาคมวิทยาศาสตร์ ...
คู่มือการเขียนรายงาน การอ้างอิง การประกวดโครงงานวิทยาศาสตร์ สมาคมวิทยาศาสตร์ ...
 
การคำนวณกระจกนูน
การคำนวณกระจกนูนการคำนวณกระจกนูน
การคำนวณกระจกนูน
 
การศึกษาที่ฮอว์ธอร์น
การศึกษาที่ฮอว์ธอร์นการศึกษาที่ฮอว์ธอร์น
การศึกษาที่ฮอว์ธอร์น
 
บทที่ 5
บทที่ 5บทที่ 5
บทที่ 5
 
ความร้อน
ความร้อนความร้อน
ความร้อน
 
พลังงานสะสมในตัวเก็บประจุ
พลังงานสะสมในตัวเก็บประจุพลังงานสะสมในตัวเก็บประจุ
พลังงานสะสมในตัวเก็บประจุ
 
Ar 7-705-6327-1131-doc
Ar 7-705-6327-1131-docAr 7-705-6327-1131-doc
Ar 7-705-6327-1131-doc
 
รายงานโครงการเครื่องจักสานงานไม้ไผ่ภูมิปัญญาไทอีสาน2558ศพว2
รายงานโครงการเครื่องจักสานงานไม้ไผ่ภูมิปัญญาไทอีสาน2558ศพว2รายงานโครงการเครื่องจักสานงานไม้ไผ่ภูมิปัญญาไทอีสาน2558ศพว2
รายงานโครงการเครื่องจักสานงานไม้ไผ่ภูมิปัญญาไทอีสาน2558ศพว2
 
บทที่ 4 หลักการทำงานของระบบคอมพิวเตอร์
บทที่ 4 หลักการทำงานของระบบคอมพิวเตอร์บทที่ 4 หลักการทำงานของระบบคอมพิวเตอร์
บทที่ 4 หลักการทำงานของระบบคอมพิวเตอร์
 
การพัฒนาตามแนวพระราชดำริ
การพัฒนาตามแนวพระราชดำริการพัฒนาตามแนวพระราชดำริ
การพัฒนาตามแนวพระราชดำริ
 
ตัวอย่างโครงการสอน
ตัวอย่างโครงการสอนตัวอย่างโครงการสอน
ตัวอย่างโครงการสอน
 
ใบความรู้แผ่นพับ
ใบความรู้แผ่นพับใบความรู้แผ่นพับ
ใบความรู้แผ่นพับ
 
การเขียนแผนปฏิบัติงาน
การเขียนแผนปฏิบัติงานการเขียนแผนปฏิบัติงาน
การเขียนแผนปฏิบัติงาน
 
แผ่นพับเสนอผลงานนวัตกรรม หน้า 1
แผ่นพับเสนอผลงานนวัตกรรม หน้า 1แผ่นพับเสนอผลงานนวัตกรรม หน้า 1
แผ่นพับเสนอผลงานนวัตกรรม หน้า 1
 
5.7อนุกรมเลขคณิตแก้ไข
5.7อนุกรมเลขคณิตแก้ไข5.7อนุกรมเลขคณิตแก้ไข
5.7อนุกรมเลขคณิตแก้ไข
 
โครงงานชาผักสมุนไพรพื้นบ้าน
โครงงานชาผักสมุนไพรพื้นบ้านโครงงานชาผักสมุนไพรพื้นบ้าน
โครงงานชาผักสมุนไพรพื้นบ้าน
 
04แผน เรื่อง กำลัง
04แผน เรื่อง กำลัง04แผน เรื่อง กำลัง
04แผน เรื่อง กำลัง
 
1.4 สถิติเชิงอนุมาน
1.4 สถิติเชิงอนุมาน1.4 สถิติเชิงอนุมาน
1.4 สถิติเชิงอนุมาน
 
โครงงานสมบูรณ์
โครงงานสมบูรณ์โครงงานสมบูรณ์
โครงงานสมบูรณ์
 
รูปเล่มวิชาโครงงาน
รูปเล่มวิชาโครงงานรูปเล่มวิชาโครงงาน
รูปเล่มวิชาโครงงาน
 

Viewers also liked

โครงสร้างข้อมูลแบบต้นไม้
โครงสร้างข้อมูลแบบต้นไม้โครงสร้างข้อมูลแบบต้นไม้
โครงสร้างข้อมูลแบบต้นไม้tumetr
 
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)tumetr
 

Viewers also liked (6)

โครงสร้างข้อมูลแบบต้นไม้
โครงสร้างข้อมูลแบบต้นไม้โครงสร้างข้อมูลแบบต้นไม้
โครงสร้างข้อมูลแบบต้นไม้
 
Tree
TreeTree
Tree
 
Trees
TreesTrees
Trees
 
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
โครงสร้างข้อมูลแบบลิงค์ลิสต์ (linklist)
 
Tree.two
Tree.twoTree.two
Tree.two
 
Tree
TreeTree
Tree
 

Tree

  • 1. LOGO โครงสร้างข้อมูลทรีและ ไบนารีทรี (Tree and Binary Tree) 1
  • 2. 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
  • 4.  Root หมายถึง Node แรกของ Tree Root A B C 4
  • 5. ดังนั้นจะพบว่าต้นไม้ทั่วไป Root มี Indegree = 0 Root มี Outdegree = N อื่นๆ มี Indegree = 1 อื่นๆ มี Outdegree = N 5
  • 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
  • 8.  Parent หมายถึง Node ที่มี Outdegree A B C D E Parent A, B 8
  • 9.  Child หมายถึง Node ที่มี Indegree A B C D E C hild B, C , D, E 9
  • 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
  • 20. แบบฝึกหัด 1. จากต้นไม้ตอไปนี้ จงหา 1.1 Root ่ 1.2 Leaves 1.3 Internal nodes 1.4 Ancestors of H 1.5 Descendents of F 20
  • 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
  • 23. Binary Tree หมายถึง ต้นไม้ที่แต่ละ Node มี Subtree <= 2 หรือ Outdegree <= 2 23
  • 24. ตัวอย่าง Binary Tree 24
  • 25. E mpty หรือ Null Tree 25
  • 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
  • 29. Complete และ Nearly Complete Binary Tree 29
  • 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
  • 35. 1. Depth-first NLR LNR LRN 35
  • 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
  • 42. Binary Tree Application 1. Expression Tree 2. Huffman Code 42
  • 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
  • 50. ตัวอย่าง จากนิพจน์ Postfix abc+*d+ จะได้ Expression Tree ดังนี้ 50
  • 51. แบบฝึกหัด จงแสดง Expression Tree จากนิพจน์ Postfix ทีกำาหนด ่ ให้ A B * C D / + E F - * 51
  • 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
  • 56. ตัวอย่าง การสร้าง Huffman Tree เพื่อหา Huffman Code สมมติให้ 56
  • 57. 57
  • 58. 58
  • 59. 59
  • 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
  • 61. แบบฝึกหัด กำาหนดความถีของการใช้งานตัวอักษรต่าง ๆ ดังนี้ ่ A = 30% C = 15% E = 25% B = 15% D = 10% F = 5% จงสร้าง Huffman Code แทนตัวอักษรดังกล่าว 61
  • 62. General Tree หมายถึง Tree ที่สามารถมี Outdegree ได้ ไม่จำากัดจำานวน 62
  • 63. การแปลง General Tree เป็น Binary Tree มี 3 ขั้นตอน ดังนี้ 1. ระบุ Child ที่อยู่ทางซ้ายสุด 2. เชื่อม Sibling เข้าด้วยกัน 3. ลบ Branch ที่ไม่ต้องการ 63
  • 64. ตัวอย่าง การแปลง General Tree เป็น Binary Tree 64

Editor's Notes

  1. ในเมื่อเรารู้ว่ามีแค่ไม่กี่จำนวน เราก็กำหนดโค้ดเองได้ดังในแผ่นใส
  2. อย่างนี้ถึงจะ compress ได้ แต่ก็ไม่มีทางตีความกลับคืนได้ เพราะความหมายไม่แน่นอน