> For the complete documentation index, see [llms.txt](https://garychang.gitbook.io/data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://garychang.gitbook.io/data-structure/lecture2-tree-and-binary-tree/lecture2.1-tree.md).

# 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&#x20;
   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. 表示法

#### 1. 使用Link List表示

![](https://2769815795-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGckN3OAfinKuVIrkMj%2F-LHGZeAGCU8T4to40ViB%2F-LHGa7I8w9S5O8Pwz8Dq%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-13%2011.03.00.jpg?alt=media\&token=9cb22fb2-7b9a-4559-aaae-09886ec0ebf2)

Nodes = n , Degree of tree = k

Available links = n-1（扣掉root，其他節點都有一個有用的Link）

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

$$
\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)

![](https://2769815795-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGckN3OAfinKuVIrkMj%2F-LHGZeAGCU8T4to40ViB%2F-LHGjUL0Hess4Y6Gy634%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-13%2011.44.39.jpg?alt=media\&token=adcdea58-300e-4d62-985d-ea9611339568)

#### 4. 括號法

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

Ａ（Ｂ（ＣＤ）Ｅ（Ｆ）Ｇ（ＨＩＪ））
