• 沒有找到結果。

# 感謝同學們在加分題建議 .

N/A
N/A
Protected

Share "感謝同學們在加分題建議 . "

Copied!
33
0
0

(1)

## 樹

(2)

(3)

### •

A4 大小一張 , 雙面 , 抄到你開心為止 ( 期末考沿用 )

(4)

### 樹

http://www.ahneninfo.com/de/ahnentafel.htm

(5)

### •

Definition: A tree is a finite set of one or more n odes such that

### •

(1) There is a specially designated node called the root.

### •

(2) The remaining nodes are partitioned into disjo int sets , where each of these sets is a tree.

### •

(3) are called the subtrees of the root.

(6)

Root

Node

Degree

Leaf

Terminal nodes

Children

Siblings

Degree of a tree

Ancestors

Level

Height or Depth

A

B C D

E F G H I J

K L M

(7)

A

B C … ㄅ

1 2 k

### •

Data Child 1 Child 2 Child 3 … Child k

(8)

### •

Left child-right sibling representation

### •

1. 每個 node 最多只有一個最左邊的 child ( 是廢話 )

### •

2. 每個 node 也最多只有一個最靠近他的右邊的 sibling ( 也是廢話 )

Data left child Right sibling

(9)

A

B C D

E F G H I J

K L M

A

B C D

E F G H I J

K L M

(10)

### •

Root 沒有右邊的 child ( 也就是說原本的 LC-RS 樹裡面 root 不會有兄弟 姊妹 - 廢話 )

A

B C D

E F G H I J

K L M

A B

C

D E

F

G

H I M J

K

L

(11)

### •

Definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and t he right subtree.

(12)

### •

1. 在 level i 的 node 數目最多為

### •

i=1 時 , 為 root 那一層 , 所以只有一個 node, 也就是最 多有個 node. ( 成立 )

(13)

### •

2. 一棵 depth 為 k 的 binary tree, 最多有個 node, .

. 喔耶 .

(14)

### 一些證明 part 3

3. 對於任何不是空的 binary tree, 假設為 leaf node 數 目 , 為 degree 2 的 node 數目 , 則 .

(4)

(4) 減 (1) 得 . 喔耶 .

(15)

### •

Definition: a full binary tree of depth k is a bina ry tree of depth k having nodes, .

### •

1

2 3

4 5 6 7

depth=3 的 full binary tree

(16)

### •

Definition: A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes n umbered from 1 to n in the full binary tree of dept h k.

1

2 3

4 5

1

2 3

4 5 6

1 2

1

2 3

5 6

Yes Yes Yes No

1 2

4 5

No

(17)

### •

Hint: 高度為 k 的 full binary tree 有個 node

(18)

### •

2 3

4 5 6 7

[0] [1] [2] [3] [4] [5] [6] [7]

(19)

### 舉例 :

A

B D

C E

[0] [1] [2] [3] [4] [5] [6] [7]

“skewed binary tree” 歪斜 binary tree

(20)

### •

leftChild(i) 的 index=?

### •

rightChild(i) 的 index=?

4)

1

2 3

4 5 6 7

(21)

### •

Data left child right child

left child right child data

left A righ t left B righ

t

left C righ t root

(22)

### •

1. 走左邊的 child 那邊的 node 們 ( 用 L 表示 Left bran ch)

### •

2. 走右邊的 child 那邊的 node 們 ( 用 R 表示 Right bra nch)

### •

3. 處理自己這個 node 的資料 ( 用 V 表示 Visit)

1

2 3

4 5 6 7

(23)

VLR: preorder

LVR: inorder

LRV: postorder

(24)

## Binary tree with arithmetic expression

### •

Preorder  prefix

Inorder  infix

### •

Postorder  postfix

### •

( 亂寫一個 expression 看看 )

*

- 5

2 3

(25)

### •

void inorder(treePointer ptr) { inorder(ptr->leftChild);

visit(ptr);

inorder(ptr->rightChild);

}

(26)

### •

for(;node;node=node->leftChild) push(node);

node=pop();

if (!node) break;

printf(“%d”, node->data);

node=node->rightChild;

}

(27)

### Level-order traversal

for(;;) {

ptr=delete();

if (ptr) {

printf(“%d”, ptr->data);

} else break;

}

(28)

### •

Push(element) 把 element 放進 queue 裡面

### •

Pop() 把 element 拿出來 . 這個 element 有最高的 prior ity

### •

( 可以想像 , 放進去的時候有做一些排序 )

(29)

### •

Definition: A max tree is a tree in which the key v alue in each node is no smaller (larger) than the k ey values in its children (if any).

### •

Definition: A max heap is a complete binary tree th at is also a max tree. A min heap is a complete bin ary tree that is also a min tree.

Root 是不是永遠都是最大 ?

14

12

10 8 6

7

(30)

### •

1. 既然是 complete binary tree, 所以一定要加在下一 個該出現的地方 , 把新的 el ement 放在那邊 .

### •

2. 循序往 root 的方向移動 , 一直到不違反 parent > chil d 的規則為止

14

12

10 8 6

7

20

(31)

### •

1. 首先既然是 complete binary tree 拿掉的位置就沒有別的選擇 .

### •

2. 把拿掉的位置的 element,

21

15

14 10

20

(32)

### •

Time complexity 是多少 ??

### •

Push operation = O(??)

### •

Pop operation = O(??)

(33)

### •

Binary search tree 的部分不難 , 紙本題目有出 , 想要 先做的人可以先看 5.7

### •

• By definition, a pseudo-polynomial-time algorithm becomes polynomial-time if each integer parameter is limited to having a value polynomial in the input length.. • Corollary 42

We solve the three-in-a-tree problem on

– Taking any node in the tree as the current state induces a binomial interest rate tree and, again, a term structure.... An Approximate

• The randomized bipartite perfect matching algorithm is called a Monte Carlo algorithm in the sense that.. – If the algorithm ﬁnds that a matching exists, it is always correct

– Taking any node in the tree as the current state induces a binomial interest rate tree and, again, a term structure.... An Approximate Calibration

– Taking any node in the tree as the current state induces a binomial interest rate tree and, again, a term structure.... Binomial Interest Rate

The main disadvantage of the Derman-Kani tree is the invalid transition probability problem, in which the transition probability may become greater than one or less than zero.

In particular, we present a linear-time algorithm for the k-tuple total domination problem for graphs in which each block is a clique, a cycle or a complete bipartite graph,