2.1 - Tree

介紹Tree的定義及其表示法

1. 定義

Tree是由n個節點(node)所構成之集合,不可為空。

  1. 至少有一個Root

  2. 其餘的節點可分為T1~Tm個互斥集合,稱為Root的子樹(subtree)。

2. 術語

                   Level
       A          -  0(1)
    / |  \ 
   B  E   G       -  1(2)
 / |  | / | \ 
 C D  F H I  J    -  2(3)
  1. Degree of nodes

    1. Degree of A = 3

    2. Degreee of B = 2

  2. Leaf: Degree = 0

  3. Non-leaf: A, B, E, G

  4. Child: B is a child of A

  5. Parent: A is the parent of B

  6. Sibling: 有相同Parent彼此稱之

  7. Ancestors & Descendants

  8. Level of nodes

  9. Degree of a Tree = Max(degrees of nodes)

  10. Height of a Tree = Max(levels of nodes)

  11. Forest: >=0個互斥的樹所形成之集合。

3. 表示法

Nodes = n , Degree of tree = k

Available links = n-1(扣掉root,其他節點都有一個有用的Link)

Empty links = n*k - (n-1)

nk(n1)nk=k1k\frac{n*k - (n-1) }{n*k} = \frac{k-1}{k}

非常浪費空間的表示法。

2. 使用Binary Tree表示。

3. Sibling-Child表示法。

  1. 節點中的child指標指向leftmost-child(A->B)

  2. 節點中的sibling指標指向right-sibling(B->C, C->D)

4. 括號法

       A    
    / |  \ 
   B  E   G 
 / |  | / | \ 
 C D  F H I  J  

A(B(CD)E(F)G(HIJ))

Last updated