SlideShare a Scribd company logo
1 of 17
Download to read offline
•   Introduction Trees
                                                                    •   Binary Trees
Thammarat Simarojana
                                                                    •   Tree Traversal
Department of Business Computer                                     •   Expression Trees
Faculty of Management Sciences                                      •   Binary Search Trees
Rajabhat Rajanagarindra University                                  •   AVL Trees




                                                            1
                                                                                                                                   2




  – เปนที่รวมรวมโหนดตาง ๆ และมีคุณสมบัติ Recursively              – ราก (Root) สมาชิกตัวแรกที่อยูบนสุดของทรีมี 1 ตัวเทานั้น
  – ประกอบดวยสมาชิกที่เรียกวา โหนด (Node) ซึ่งอาจจะเปน           – โหนด (Node) สมาชิกแตละตัวในทรี
    Empty Tree หรือ Tree ที่ประกอบดวย N โหนด                       – โหนดพอแม (Parent Node) โหนดที่มีโหนดยอยเปนสวนประกอบ
                                                                      ตั้งแต 1 ขึ้นไป
  – แตละโหนดเชื่อมกัน เสนที่เชื่อม เรียกวา Edge                  – โหนดลูก (Child Node) เปนโหนดยอยของโหนดแมจะมี 2 ชนิด คือ
  – Tree ประกอบ N โหนดจะมี Edge ทั้งหมด N-1 เสน                         • โหนดลูกดานซาย
                                                                         • โหนดลูกดานขวา
                                                                3                                                                  4
root

    – ลีฟโหนด (Leaf Node) เปนโหนดสุดทายที่ไมมีโหนดลูกยอย                parent node                          A                                       0
      อีก
                                                                                                     B                       C                           1
    – ความลึก หรือ ความสูง (Depth or High) คาตัวเลขความลึก                                                          G                   H               2
                                                                       child node B          D               E
      หรือความสูง สูงสุดของทรี
                                                                                                     F                           J               K       3

                                                                                                                         L                               4
                                                                                         Leaf node                                               depth = 4
                                                                   5                                                                                                 6




            A                                 A                                      A                                                           A

    B               C                B                C                      B                   C                                   B                       C

D       E       G       H        D        E       G       H             D        E       G               H                   D               E       G           H

    F               J       K         F               J        K             F                   J               K                   F                       J       K

                L                                 L                                       L                                                          L


    Sibling is B, C                  Sibling is D, E                        Ancestor D is B, A                               Ancestor F is E, B, A

                                                                   7                                                                                                 8
A                                                         A                                   • โหนดของทรีเปน 0 หรือไมมีโหนดที่เรียกวา Empty
                                                                                                               • โหนดแตละโหนดสามารถมีสมาชิกไดไมเกิน 2 โหนด
       B                        C                              B                        C
                                                                                                               • กําหนดเรียกชื่อทรียอยดานซายวา Left Subtree และ ทรียอยทาง
D           E           G                H                D            E        G                 H
                                                                                                                 ดานขวาวา Right Subtree
       F                         J              K               F                        J            K
                                                                                                                            A                   A                        A
                           L                                                      L
                                                                                                                    B           C          B            C           B          C
Descendant B : D, E, F                                  Descendant C : G, H, J, K, L
                                                                                                                D                                   D           D                  D
                                                                                                          9                                                                            10




    • การเก็บไบนารีทรีในหนวยความจํา มี 2 แบบ
        – เก็บแบบลิงคลิสต                                                                                         ROOT                                                INFO
        – เก็บแบบอารเรย (แบบเรียงลําดับ)                                                                                                                      LEFT           RIGHT
                                                                                                                                    A
    • การเก็บไบนารีทรีแบบลิงคลิสต                                                                                                                                      A
                                                                                                                        B                       C
        – ใชอารเรย 3 อารเ รยขนานกันคือ INFO, LEFT และ RIGHT และใชตัวแปรพอยเตอร ROOT ดัง
          รายละเอียดดังนี้
              • INFO[k]           เก็บขอมูลของโหนด   N                                                        x D x            E x     x G x           H

              • LEFT[k]           เก็บตําแหนงของโหนดลูกดายซายของโหนด  N
                                                                                                                    x F x                       J x         x K x
              • RIGHT[k]          เก็บตําแหนงของโหนดลูกดายซายของโหนด N
              • ROOT              เก็บตําแหนงรากของทรี ถาทรีวาง ROOT=NULL                                                            x J x
              • AVIAL                เก็บโหนดที่ลบหรือโหนดวางของทรี
                                                                                                          11                                                                           12
NAME         SSN     SALARY        LEFT       RIGHT                     สามารถเขียนเปนโครงสรางทรีได ดังนี้
               1   Davis       1001      20000            0          10                     ROOT            = 11 คือ Harris
               2                                                                            LEFT[11]        = 8 คือ Cohen
                   Kelly       2510      30000            0          0                      RIGHT[11]       = 6 คือ Lewis
               3   Green       2254      25200            1          0                                                                                     Harris

AVAIL          4                                          7                                                                                Cohen                                  Lewis
  5            5   Brown 5454            15000            0              0
               6   Lewis 8541            14000            2              9
               7                                          0                                                                         Brown         Green                   Kelly           Rubin
               8   Cohen       3252      25000            5              3
               9   Rubin       2542      19000            0              0                                                                 Davis
ROOT          10   Evans       1542      25040            0              0
 11           11   Harris      1253      28000            8              6                                                                        Evans
                                                                                     13                                                                                                       14




 • การเก็บไบนารีทรีแบบอารเรย (แบบเรียงลําดับ)                                           การเก็บไบนารีทรีแบบเรียงลําดับ
                                                                                              1   2    3    4    5    6   7     8     9      10     11    12   13    14      15     16
       – ใชอารเรยเก็บเพียงอารเรยเดียว และใชตัวแปรตัวแปรพอยเตอร
                                                                                             45   22   77   11   30       90          15    25                       88
       – รากของทรีเก็บอยูใน TREE[1]
                                                                                                                               TREE
       – ถาโหนด N อยูใน TREE[k] ดังนั้น
            • โหนดดานซายเก็บใน TREE[2*k]                                                ขอสังเกต : การเก็บแบบเรียงลําดับจะ                                       45
                                                                                          สิ้นเปลืองเนื้อที่มากหาเปนไบนารีที่แบบ                                             77
            • โหนดดานขวาเก็บใน TREE[2*k+1]                                               ไมสมบูรณ ถาเทียบกับการเก็บแบบลิงค
                                                                                                                                                         22
                                                                                          ซึ่งมีความลึก = 4 แตแบบเรียงลําดับนี้             11                30                        90
       – ตัวแปร END เก็บจําแหนงสุดทายของทรี                                             ตองใชพื้นที่ = 2d (D = ความลึก)
       – เมื่อโหนด N ที่ TREE[k] ไมมีทรียอยดานซายหรือขวา 2*k หรือ 2*k+1 จะมีคาเกิน   ดังนั้น = 24 = 16 เซลล                                  15 25                          88

         กวา END หรือ TREE[2*k] หรือ TREE[2*k+1] =NULL                                                                                           การเก็บไบนารีทรีแบบลิงคลิสต
                                                                                     15                                                                                                       16
• เปน Binary Trees
• โหนดทุกโหนดตองมีสมาชิก 2 โหนด                                    A       A                   A
• ระดับของโหนดใบ (Leaf Node) จะตองอยูในระดับเดียวกัน
                                                                        B       C       B               C
• คํานวณหาจํานวนโหนดไดจาก 2r+1-1 เมื่อ r เปนความลึกของทรี
  เชน ความลึก (dept) = 5 จะมีจํานวนโหนดไดเทากับ
                                                                                    D       E       F       G
  26- 1 = 63 โหนด


                                                               17                                               18




• เปน Binary Trees
• ระดับของโหนดปลายตางกันเพียง 1 ระดับ
• ปลายโหนดที่ระดับสุดทายจะตองเริ่มตนจากดานซายมือสุดกอน
  แลวจึงทยอยไลชิดมาทางดานขวามือ โดยไมเวนชวง




                                                               19                                               20
• แตละโหนดมีสมาชิก 0 หรือ 2 โหนด                                   ไบนารีแบบขยาย : 2 - ทรี
• กรณีที่โหนดใดมีลูก 2 โหนด เรียกวา โหนดใน (Internal node)
• กรณีที่โหนดใดมีลูก 0 โหนด เรียกวา โหนดนอก (External node)                                  Internal node




                                                                                              External node


                                                               21                                             22




                                                                               General Tree   Binary Tree

• หมายถึง Tree ที่มีสมาชิกไดไมจํากัดจํานวน

                               A


                B              E               F

           C        D                  G       H    I
                                                               23                                             24
A                                                          A
                                                                                                                  A                                               A

    B             E             F                                   B       E       F                                                                   B
                                                                                                    B         E            F
C       D                G      H        I                  C           D       G   H   I                                                       C               E
                                                                                             C           D            G        H    I                   D                 F

                 A                                                          A                                                                                   G

                                                                                                                                                                          H
    B             E             F                                   B       E       F
        D                                                               D                                                                                                     I
C                       G       H        I                  C                   G   H   I
                                                                                        25                                                                                        26




                             กระทําได   3   วิธี คือ                                                                     เปนการทองเขาไปใน Tree จากบนลงลาง และจากซาย
        – พรีออรเดอร (Pre-order) หรือ โหนด-ซาย-ขวา (NLR)                                                               ไปขวาทีละระดับ โดยเริ่มจาก Root Node
        – อินออรเดอร (In-order) หรือ ซาย-โหนด-ขวา (LNR)
                                                                                                 จากภาพ ดานขวา เมื่อทําการ Traverasal แบบ Breath – First จะได
        – โพสทออรเดอร (Post-order) หรือ ซาย-ขวา-โหนด (LRN)

                                                                        A                                                                                   A
        – พรีออรเดอร = A   BDECF
                                                                B           C                                                                       B                 C
        –   อินออรเดอร = D B E A C F                                                             Breath – First =
                                                                                F                                                               D       E                 F
        –   โพสออรเดอร = D E B F C A                 D               E

                                                                                        27                                                                                        28
หมายถึง Binary Trees ที่มีคุณสมบัติดังตอไปนี้                        1. แปลงนิพจนจาก infix เปน Postfix
1. Leaf เก็บ Operand เชน A B C 12 14 20 เปนตน                                2. Push Postfix ทีละตัวเขาสแตก (Stack)
                                                                                3. ถา Push Postfix เปน
2. Root และ Internal node เก็บ Operator เชน + - * / ^ ( )                           • Operand ให Push ลงสแตก (Stack)
3. Subtree เปน Sub expression                                                       • Operator ใหทําการสราง Tree แลว Push ลงสแตก (Stack) คือ
                                                                                         – เอาตัว Operator เปน Root Node
                                                                                         – เอา Top of Stack เปน Right Subtree
                                                                                         – เอา Top of Stack - 1 เปน Left Subtree

                                                                       29                                                                          30




• ตัวอยางเชน (a+(b*c))+(((d*e)+f)*g) เมื่อทํา                             • แปลง infix เปน Postfix จะได ab+cde+**
    เปน Expression Tree จะไดรูป ดังนี้

                                       +
      +                                                        *
                                                                            a     b                                        +
a                *                                     +           g

           b          c                        *           f                                                          a         b

                                           d       e
                                                                       31                                                                          32
+        c        d        e                   +       c                   +

a       b                                      a       b                                            • ไบนารีเซิรชทรี : การคนหาขอมูลในไบนารีทรี โดยจะใชเวลาในการคนหา
                                                                       d                   e
                                                                                                                     2 ความยากงายของการแทรก หรือลบขอมูลขึ้นอยูกับ
                                                                                                                                                                
                                                                                                       โครงสรางตอไปนี้
                                                                                                            • อารเรยท่มการจัดเรียง เราสามารถคนหาไดงายโดยใชเวลา
                                                                                                                        ี ี
                                                           *
    +                 *                                                                                               f(n) = O(log2n) แตการแทรกหรือลบขอมูลยุงยาก
a       b        c         +                       +                       *                                • ลิงคลิสต เราสามารถลบหรือแทรกขอมูลไดงาย แตยุงยากในการคนหา โดยใช
                                                                                                              เวลา f(n) = O(n)
                                               a       b           c               +
                      d            e

                                                                       d                   e
ab+cde+**                                                                                      33                                                                                       34




                                   Binary Search Trees
                                                                                                                     Binary Search Trees

                                                                                                                      (Find)

                          ขอใดเปน Binary Search Tree                                                                            (Max and Min)

                                                                                                                     (Insert)
        8                                  6                                   6

    3       10                         2       8                       2               8                            (Built)

        7        19            1           4                   1               4                                 (Remove)


                                       3                               3               7
                                                                                               35                                                                                       36
(Max and Min)
                                                                                                                       (Max
                                   (Find)
                                                                                • คาต่ําสุด ใหเคลื่อนไปทาง Left Sub Tree ของโหนดใด ๆ จนกวา
ตัวอยาง         ตองการคนหา 20                                                  โหนดนั้นจะไมมี Left Sub Tree
                                                                                • คาสูงสุด ใหเคลือนไปทาง Right Sub Tree ของโหนดใด ๆ จนกวา
                                                                                                     ่
1)   20   < 38   ไปทางซาย
2)   20   > 14   ไปทางขวา                                                         โหนดนั้นจะไมมี Right Sub Tree
3)   20   < 23   ไปทางซาย                 38                                                                         6
4)   20   > 18   ไปทางขวา
                                     14                      56
                                                                          82                                2                       8
                               8           23       45
                                      18                      70
                                                                                                      1          4         7            9
                                            20
                                                                                                            3
                                                                           37                                                                              38




                               (Insert)                                                                              (Built)
                                                                                การสรางไบนารีเซิรชทรี : ใชหลักการเดียวกับการคนหาและแทรก
                 6                                       6                      ตัวอยาง จงสรางไบนารีเซอรซทรีจาก 40, 60, 50, 33, 55, 11
                                                                                โดยสรางทีละ Node
          2           8                         2                     8

 1            4                            1             4
                                                                                      40            40               40                        40
                                                                                                          60                   60       33            60
      3                                         3                 5
                                                                                                                          50                     50
                                                                                  (1) ITEM=40      (2) ITEM=60       (3) ITEM=50             (4) ITEM=33

                                                                           39                                                                              40
(Built)                                                                   (Remove)
                                                                                                         Remove)




               40                         40
      33                60           33         60
                50              11         50
                         55                      55
         (5) ITEM=55                   (6) ITEM=11



                                                                     41                                                                           42




• การลบโหนดในกรณีนี้ เปนวิธีที่งายที่สุด คือ สามารถลบโหนดที่                 – ถา N มีโหนดลูกเพียงโหนดเดียว ลบ N โดยแทนตําแหนงของ N ใน
  ตองการไดทันที โดยไมสงผลกระทบตอ Tree                                       โหนดแมดวยตําแหนงของลูกโหนดของ N

           6                                           6
                                                                                      60                                          60
     2                  8                        2               8             25             75                          25                 66
1          4                                1         4                   15        50     66                        15         50

    3               5                                      5                   33                                          33
                                                                                    44                                          44
                                                                     43                                                                           44
• ถา N มีโหนดลูก 2 โหนด โดยกําหนดให S(N) คือ inorder ของ
      โหนด N (ซึ่ง S(N) จะไมมีโหนดลูกดานซาย) ทําการลบโหนด N               •   ถูกคนควาโดย Adel’son - Velskii และ Landis
      ออกจาก Tree โดยลบ S(N) จาก Tree กอน แลวจึงแทนโหนด N                  •   ความสูงของตนไมยอยดานซายและดานขวาตางกันไมเกิน 1 ระดับ
      ใน Tree ดวย S(N)                                                      •   เปน Binary Search Trees
                60                                         60                •   การตรวจสอบวาเปน AVL Tree หรือไม มี balance factor ตรวจสอบโดย
                                                25                               balance factor = ความสูงของตนไมยอยซาย - ความสูงของตนไมยอยขวา ซึ่ง
         25             75                       33                75            คาของแฟกเตอรนี้ของแตละโหนดในตนไมตองเปน 0, 1 หรือ -1 เทานั้นจึง
    15        50     66                       15          50      66             จะเปน AVL Tree

         33                                        33
              44                                     44                 45                                                                                      46




                                     AVL Trees
                                                                                 a           2     1                                    1       2           b
•    บํารุงรักษางาย
                                                                                     1       0     0   0                        0       0       0       1
•    เปน Binary Search Trees ชนิดพิเศษ
•    ความสูงของ Left Subtree กับ Right Subtree ตางกันไมเกิน 1                  0       0                                                              0   0
•    ตอง Balance Tree                                                                                                              2       1
                                                                                             2     2
•    ความลึกของ Tree เปน O(log N)
                                                                                 c 1             0 0   1                    1       1       0       0       d

                                                                                 0       0             0   0          0     0       0       0

                                                                        47                                                                                      48
b

   a

                                               • ถาในระหวางการใชงานตนไมที่มีการเพิ่มหรือลบโหนดแลว ทําให
                                                 คาของ balance factor ไมเปนไปตามนิยามแฟคเตอร ก็ตองมีการ
                                                 ปรับตนไมโดยวิธีการหมุน ซึ่งมีอยู 2 แบบ
                                                   – หมุนครั้งเดียว (Single Rotation)
    c                                d             – หมุนสองครั้ง (Double Rotation)




                                          49                                                                                    50




                                                               1 3
                                                                A                                   A
                                                   0       0        2           0
• การหมุนครั้งเดียว กระทําได 2 แบบ คือ                B                    C               B               D

   – กระทําที่ Left Subtree                                1    D
                                                                    0
                                                                                                    E               C
   – กระทําที่ Right Subtree                       0   E   0
• การหมุน 2 ครั้ง กระทําได 2 แบบ คือ
                                                                5                                           5
   – กระทําที่ Left Subtree
                                                   2                            8               2                       7
   – กระทําที่ Right Subtree
                                               1           4            7               1               4       6           8

                                          51       3           6                                3                               52
2                                                  4
            2                                           2
    1                3                        1                  4             1                 4                            2                    5

                             4                                                         3                  5              1            3                6
                                                        3             5

                                 5                                                                            6




                                                                          53                                                                               54




                                     (Double Rotation)                                                            (Double Rotation)


                     K3                                     K2                             K1                                         K2
        K1                                  K1                                                       K3
                                 D                                   K3            A
                K2                                                                                                           K1                   K3
                                                                                                K2
A                                       A           B            C                                        D
                                                                          D                                                       B           C
        B                C                                                                                           A                                 D
                                                                                           B         C


                     Left - Right double rotation                                                    Right - Left double rotation
                                                                          55                                                                               56
4                                                       4
                                                                                                                                         1                                                          2
            4                                               4                                               2                        6                          2                               7
                                                                                                                                                  3                                 1                         3
                                                                                                        1       3            5               15             1           3               6               15
    2                6                      2                                6
                                                                                           2
1       3       5          7        1   1           3               5                 15                                         2 7              16                            5                14           16
                                16 3                                    17
                                                                                                                                             14
                                                                                            3 16
                          15
                                    2

                                                                                                57                                                                                                                 58




            4                                                           7                                                        7                                                          7
    2                7                              4                                      15                       4                             15                        4                                15
1       3       6         15                2                   6                14                16       2               6            14            16       2                   6               13             16
            5        14        16       1       3       5               13                              1       3       5        13                         1       3           5           12           14
                13                                                                                                          12



                                                                                                59                                                                                                                 60
1.   จงตอบคําถามตอไปนี้โดยดูจาก Tree
                                                            1.1 Root Node        คือ โหนดใด
                                                                                                                                             A
                                                            1.2 Leaf Node คือโหนดใด
                                                            1.3 Parent Node ของ F คือโหนดใด
                      End of Chapter                        1.4 ตนไมตนนี้มีความสูง (High) เทาไหร
                                                                                                                             B                               C

                                                            1.5 Sibling Node ของ G คือโหนดใด
                                                                                                                     D               E               F           G
                                                            1.6 Ancestor Node ของ I คือโหนดใด
                                                            1.7 Descendant Node ของ H คือโหนดใด                              H               I                        J
                                                            1.8 ตนไมตนนี้เปน Binary Trees หรือไม
                                                            เหตุใดจึงคิดเชนนั้น                                     K
                                                            1.9 ตนไมตนนี้เปน Complete Binary Trees
                                                            หรือไม เหตุใดจึงคิดเชนนั้น
                                                  61                                                         L                                                       62




2.   จงแปลง General Trees เปน Binary Trees            3.     จงแสดงผลลัพธหลังจากการทองเขาไปในตนไม (Tree Traversals)
                                                             3.1 แบบ Pre-order
                          A                                  3.2 แบบ In-order                                    A
                                                             3.3 แบบ Post-order
                                                                                                   B                             C
              B       C       D       E                      3.4 แบบ Breath – First
                                                                                               D         E               F               G
          F       G               H       I
                                                                                                   H             I                               J

                                  J       K   L                                                          K                                               L
                                                  63                                                                                                                 64
4.   จงสราง Expression Trees จากนิพจนตอไปนี้                  7.   จงสรางตนไม AVL จากขอมูลที่กําหนดใหตอไปนี้
        4.1 A * B + C / D - E                                         7.1 15 31 52 50 48 9 60 57 28
        4.2 A + ( B * C ) - ( D / E )                                 7.2 44 19 51 16 15 23 54 47 62
        4.3 ( A / B ) + ( C – D * E ) + F                             7.3 I H N L M F D B K C G E                       J A O

5. จงสราง Binary Search Trees จากขอมูล ดังนี้
         42 31 45 78 96 12 26 40 52 56 35 10

6.. จากตนไมในขอ 5. จงปรับปรุงตนไมเมื่อลบ 96 12 31 42
                                                            65                                                                  66

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Tree

  • 1. Introduction Trees • Binary Trees Thammarat Simarojana • Tree Traversal Department of Business Computer • Expression Trees Faculty of Management Sciences • Binary Search Trees Rajabhat Rajanagarindra University • AVL Trees 1 2 – เปนที่รวมรวมโหนดตาง ๆ และมีคุณสมบัติ Recursively – ราก (Root) สมาชิกตัวแรกที่อยูบนสุดของทรีมี 1 ตัวเทานั้น – ประกอบดวยสมาชิกที่เรียกวา โหนด (Node) ซึ่งอาจจะเปน – โหนด (Node) สมาชิกแตละตัวในทรี Empty Tree หรือ Tree ที่ประกอบดวย N โหนด – โหนดพอแม (Parent Node) โหนดที่มีโหนดยอยเปนสวนประกอบ ตั้งแต 1 ขึ้นไป – แตละโหนดเชื่อมกัน เสนที่เชื่อม เรียกวา Edge – โหนดลูก (Child Node) เปนโหนดยอยของโหนดแมจะมี 2 ชนิด คือ – Tree ประกอบ N โหนดจะมี Edge ทั้งหมด N-1 เสน • โหนดลูกดานซาย • โหนดลูกดานขวา 3 4
  • 2. root – ลีฟโหนด (Leaf Node) เปนโหนดสุดทายที่ไมมีโหนดลูกยอย parent node A 0 อีก B C 1 – ความลึก หรือ ความสูง (Depth or High) คาตัวเลขความลึก G H 2 child node B D E หรือความสูง สูงสุดของทรี F J K 3 L 4 Leaf node depth = 4 5 6 A A A A B C B C B C B C D E G H D E G H D E G H D E G H F J K F J K F J K F J K L L L L Sibling is B, C Sibling is D, E Ancestor D is B, A Ancestor F is E, B, A 7 8
  • 3. A A • โหนดของทรีเปน 0 หรือไมมีโหนดที่เรียกวา Empty • โหนดแตละโหนดสามารถมีสมาชิกไดไมเกิน 2 โหนด B C B C • กําหนดเรียกชื่อทรียอยดานซายวา Left Subtree และ ทรียอยทาง D E G H D E G H ดานขวาวา Right Subtree F J K F J K A A A L L B C B C B C Descendant B : D, E, F Descendant C : G, H, J, K, L D D D D 9 10 • การเก็บไบนารีทรีในหนวยความจํา มี 2 แบบ – เก็บแบบลิงคลิสต ROOT INFO – เก็บแบบอารเรย (แบบเรียงลําดับ) LEFT RIGHT A • การเก็บไบนารีทรีแบบลิงคลิสต A B C – ใชอารเรย 3 อารเ รยขนานกันคือ INFO, LEFT และ RIGHT และใชตัวแปรพอยเตอร ROOT ดัง รายละเอียดดังนี้ • INFO[k] เก็บขอมูลของโหนด N x D x E x x G x H • LEFT[k] เก็บตําแหนงของโหนดลูกดายซายของโหนด N x F x J x x K x • RIGHT[k] เก็บตําแหนงของโหนดลูกดายซายของโหนด N • ROOT เก็บตําแหนงรากของทรี ถาทรีวาง ROOT=NULL x J x • AVIAL เก็บโหนดที่ลบหรือโหนดวางของทรี 11 12
  • 4. NAME SSN SALARY LEFT RIGHT สามารถเขียนเปนโครงสรางทรีได ดังนี้ 1 Davis 1001 20000 0 10 ROOT = 11 คือ Harris 2 LEFT[11] = 8 คือ Cohen Kelly 2510 30000 0 0 RIGHT[11] = 6 คือ Lewis 3 Green 2254 25200 1 0 Harris AVAIL 4 7 Cohen Lewis 5 5 Brown 5454 15000 0 0 6 Lewis 8541 14000 2 9 7 0 Brown Green Kelly Rubin 8 Cohen 3252 25000 5 3 9 Rubin 2542 19000 0 0 Davis ROOT 10 Evans 1542 25040 0 0 11 11 Harris 1253 28000 8 6 Evans 13 14 • การเก็บไบนารีทรีแบบอารเรย (แบบเรียงลําดับ) การเก็บไบนารีทรีแบบเรียงลําดับ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 – ใชอารเรยเก็บเพียงอารเรยเดียว และใชตัวแปรตัวแปรพอยเตอร 45 22 77 11 30 90 15 25 88 – รากของทรีเก็บอยูใน TREE[1] TREE – ถาโหนด N อยูใน TREE[k] ดังนั้น • โหนดดานซายเก็บใน TREE[2*k] ขอสังเกต : การเก็บแบบเรียงลําดับจะ 45 สิ้นเปลืองเนื้อที่มากหาเปนไบนารีที่แบบ 77 • โหนดดานขวาเก็บใน TREE[2*k+1] ไมสมบูรณ ถาเทียบกับการเก็บแบบลิงค 22 ซึ่งมีความลึก = 4 แตแบบเรียงลําดับนี้ 11 30 90 – ตัวแปร END เก็บจําแหนงสุดทายของทรี ตองใชพื้นที่ = 2d (D = ความลึก) – เมื่อโหนด N ที่ TREE[k] ไมมีทรียอยดานซายหรือขวา 2*k หรือ 2*k+1 จะมีคาเกิน ดังนั้น = 24 = 16 เซลล 15 25 88 กวา END หรือ TREE[2*k] หรือ TREE[2*k+1] =NULL การเก็บไบนารีทรีแบบลิงคลิสต 15 16
  • 5. • เปน Binary Trees • โหนดทุกโหนดตองมีสมาชิก 2 โหนด A A A • ระดับของโหนดใบ (Leaf Node) จะตองอยูในระดับเดียวกัน B C B C • คํานวณหาจํานวนโหนดไดจาก 2r+1-1 เมื่อ r เปนความลึกของทรี เชน ความลึก (dept) = 5 จะมีจํานวนโหนดไดเทากับ D E F G 26- 1 = 63 โหนด 17 18 • เปน Binary Trees • ระดับของโหนดปลายตางกันเพียง 1 ระดับ • ปลายโหนดที่ระดับสุดทายจะตองเริ่มตนจากดานซายมือสุดกอน แลวจึงทยอยไลชิดมาทางดานขวามือ โดยไมเวนชวง 19 20
  • 6. • แตละโหนดมีสมาชิก 0 หรือ 2 โหนด ไบนารีแบบขยาย : 2 - ทรี • กรณีที่โหนดใดมีลูก 2 โหนด เรียกวา โหนดใน (Internal node) • กรณีที่โหนดใดมีลูก 0 โหนด เรียกวา โหนดนอก (External node) Internal node External node 21 22 General Tree Binary Tree • หมายถึง Tree ที่มีสมาชิกไดไมจํากัดจํานวน A B E F C D G H I 23 24
  • 7. A A A A B E F B E F B B E F C D G H I C D G H I C E C D G H I D F A A G H B E F B E F D D I C G H I C G H I 25 26 กระทําได 3 วิธี คือ เปนการทองเขาไปใน Tree จากบนลงลาง และจากซาย – พรีออรเดอร (Pre-order) หรือ โหนด-ซาย-ขวา (NLR) ไปขวาทีละระดับ โดยเริ่มจาก Root Node – อินออรเดอร (In-order) หรือ ซาย-โหนด-ขวา (LNR) จากภาพ ดานขวา เมื่อทําการ Traverasal แบบ Breath – First จะได – โพสทออรเดอร (Post-order) หรือ ซาย-ขวา-โหนด (LRN) A A – พรีออรเดอร = A BDECF B C B C – อินออรเดอร = D B E A C F Breath – First = F D E F – โพสออรเดอร = D E B F C A D E 27 28
  • 8. หมายถึง Binary Trees ที่มีคุณสมบัติดังตอไปนี้ 1. แปลงนิพจนจาก infix เปน Postfix 1. Leaf เก็บ Operand เชน A B C 12 14 20 เปนตน 2. Push Postfix ทีละตัวเขาสแตก (Stack) 3. ถา Push Postfix เปน 2. Root และ Internal node เก็บ Operator เชน + - * / ^ ( ) • Operand ให Push ลงสแตก (Stack) 3. Subtree เปน Sub expression • Operator ใหทําการสราง Tree แลว Push ลงสแตก (Stack) คือ – เอาตัว Operator เปน Root Node – เอา Top of Stack เปน Right Subtree – เอา Top of Stack - 1 เปน Left Subtree 29 30 • ตัวอยางเชน (a+(b*c))+(((d*e)+f)*g) เมื่อทํา • แปลง infix เปน Postfix จะได ab+cde+** เปน Expression Tree จะไดรูป ดังนี้ + + * a b + a * + g b c * f a b d e 31 32
  • 9. + c d e + c + a b a b • ไบนารีเซิรชทรี : การคนหาขอมูลในไบนารีทรี โดยจะใชเวลาในการคนหา d e 2 ความยากงายของการแทรก หรือลบขอมูลขึ้นอยูกับ  โครงสรางตอไปนี้ • อารเรยท่มการจัดเรียง เราสามารถคนหาไดงายโดยใชเวลา ี ี * + * f(n) = O(log2n) แตการแทรกหรือลบขอมูลยุงยาก a b c + + * • ลิงคลิสต เราสามารถลบหรือแทรกขอมูลไดงาย แตยุงยากในการคนหา โดยใช เวลา f(n) = O(n) a b c + d e d e ab+cde+** 33 34 Binary Search Trees Binary Search Trees (Find) ขอใดเปน Binary Search Tree (Max and Min) (Insert) 8 6 6 3 10 2 8 2 8 (Built) 7 19 1 4 1 4 (Remove) 3 3 7 35 36
  • 10. (Max and Min) (Max (Find) • คาต่ําสุด ใหเคลื่อนไปทาง Left Sub Tree ของโหนดใด ๆ จนกวา ตัวอยาง ตองการคนหา 20 โหนดนั้นจะไมมี Left Sub Tree • คาสูงสุด ใหเคลือนไปทาง Right Sub Tree ของโหนดใด ๆ จนกวา ่ 1) 20 < 38 ไปทางซาย 2) 20 > 14 ไปทางขวา โหนดนั้นจะไมมี Right Sub Tree 3) 20 < 23 ไปทางซาย 38 6 4) 20 > 18 ไปทางขวา 14 56 82 2 8 8 23 45 18 70 1 4 7 9 20 3 37 38 (Insert) (Built) การสรางไบนารีเซิรชทรี : ใชหลักการเดียวกับการคนหาและแทรก 6 6 ตัวอยาง จงสรางไบนารีเซอรซทรีจาก 40, 60, 50, 33, 55, 11 โดยสรางทีละ Node 2 8 2 8 1 4 1 4 40 40 40 40 60 60 33 60 3 3 5 50 50 (1) ITEM=40 (2) ITEM=60 (3) ITEM=50 (4) ITEM=33 39 40
  • 11. (Built) (Remove) Remove) 40 40 33 60 33 60 50 11 50 55 55 (5) ITEM=55 (6) ITEM=11 41 42 • การลบโหนดในกรณีนี้ เปนวิธีที่งายที่สุด คือ สามารถลบโหนดที่ – ถา N มีโหนดลูกเพียงโหนดเดียว ลบ N โดยแทนตําแหนงของ N ใน ตองการไดทันที โดยไมสงผลกระทบตอ Tree โหนดแมดวยตําแหนงของลูกโหนดของ N 6 6 60 60 2 8 2 8 25 75 25 66 1 4 1 4 15 50 66 15 50 3 5 5 33 33 44 44 43 44
  • 12. • ถา N มีโหนดลูก 2 โหนด โดยกําหนดให S(N) คือ inorder ของ โหนด N (ซึ่ง S(N) จะไมมีโหนดลูกดานซาย) ทําการลบโหนด N • ถูกคนควาโดย Adel’son - Velskii และ Landis ออกจาก Tree โดยลบ S(N) จาก Tree กอน แลวจึงแทนโหนด N • ความสูงของตนไมยอยดานซายและดานขวาตางกันไมเกิน 1 ระดับ ใน Tree ดวย S(N) • เปน Binary Search Trees 60 60 • การตรวจสอบวาเปน AVL Tree หรือไม มี balance factor ตรวจสอบโดย 25 balance factor = ความสูงของตนไมยอยซาย - ความสูงของตนไมยอยขวา ซึ่ง 25 75 33 75 คาของแฟกเตอรนี้ของแตละโหนดในตนไมตองเปน 0, 1 หรือ -1 เทานั้นจึง 15 50 66 15 50 66 จะเปน AVL Tree 33 33 44 44 45 46 AVL Trees a 2 1 1 2 b • บํารุงรักษางาย 1 0 0 0 0 0 0 1 • เปน Binary Search Trees ชนิดพิเศษ • ความสูงของ Left Subtree กับ Right Subtree ตางกันไมเกิน 1 0 0 0 0 • ตอง Balance Tree 2 1 2 2 • ความลึกของ Tree เปน O(log N) c 1 0 0 1 1 1 0 0 d 0 0 0 0 0 0 0 0 47 48
  • 13. b a • ถาในระหวางการใชงานตนไมที่มีการเพิ่มหรือลบโหนดแลว ทําให คาของ balance factor ไมเปนไปตามนิยามแฟคเตอร ก็ตองมีการ ปรับตนไมโดยวิธีการหมุน ซึ่งมีอยู 2 แบบ – หมุนครั้งเดียว (Single Rotation) c d – หมุนสองครั้ง (Double Rotation) 49 50 1 3 A A 0 0 2 0 • การหมุนครั้งเดียว กระทําได 2 แบบ คือ B C B D – กระทําที่ Left Subtree 1 D 0 E C – กระทําที่ Right Subtree 0 E 0 • การหมุน 2 ครั้ง กระทําได 2 แบบ คือ 5 5 – กระทําที่ Left Subtree 2 8 2 7 – กระทําที่ Right Subtree 1 4 7 1 4 6 8 51 3 6 3 52
  • 14. 2 4 2 2 1 3 1 4 1 4 2 5 4 3 5 1 3 6 3 5 5 6 53 54 (Double Rotation) (Double Rotation) K3 K2 K1 K2 K1 K1 K3 D K3 A K2 K1 K3 K2 A A B C D D B C B C A D B C Left - Right double rotation Right - Left double rotation 55 56
  • 15. 4 4 1 2 4 4 2 6 2 7 3 1 3 1 3 5 15 1 3 6 15 2 6 2 6 2 1 3 5 7 1 1 3 5 15 2 7 16 5 14 16 16 3 17 14 3 16 15 2 57 58 4 7 7 7 2 7 4 15 4 15 4 15 1 3 6 15 2 6 14 16 2 6 14 16 2 6 13 16 5 14 16 1 3 5 13 1 3 5 13 1 3 5 12 14 13 12 59 60
  • 16. 1. จงตอบคําถามตอไปนี้โดยดูจาก Tree 1.1 Root Node คือ โหนดใด A 1.2 Leaf Node คือโหนดใด 1.3 Parent Node ของ F คือโหนดใด End of Chapter 1.4 ตนไมตนนี้มีความสูง (High) เทาไหร B C 1.5 Sibling Node ของ G คือโหนดใด D E F G 1.6 Ancestor Node ของ I คือโหนดใด 1.7 Descendant Node ของ H คือโหนดใด H I J 1.8 ตนไมตนนี้เปน Binary Trees หรือไม เหตุใดจึงคิดเชนนั้น K 1.9 ตนไมตนนี้เปน Complete Binary Trees หรือไม เหตุใดจึงคิดเชนนั้น 61 L 62 2. จงแปลง General Trees เปน Binary Trees 3. จงแสดงผลลัพธหลังจากการทองเขาไปในตนไม (Tree Traversals) 3.1 แบบ Pre-order A 3.2 แบบ In-order A 3.3 แบบ Post-order B C B C D E 3.4 แบบ Breath – First D E F G F G H I H I J J K L K L 63 64
  • 17. 4. จงสราง Expression Trees จากนิพจนตอไปนี้ 7. จงสรางตนไม AVL จากขอมูลที่กําหนดใหตอไปนี้ 4.1 A * B + C / D - E 7.1 15 31 52 50 48 9 60 57 28 4.2 A + ( B * C ) - ( D / E ) 7.2 44 19 51 16 15 23 54 47 62 4.3 ( A / B ) + ( C – D * E ) + F 7.3 I H N L M F D B K C G E J A O 5. จงสราง Binary Search Trees จากขอมูล ดังนี้ 42 31 45 78 96 12 26 40 52 56 35 10 6.. จากตนไมในขอ 5. จงปรับปรุงตนไมเมื่อลบ 96 12 31 42 65 66