more trees

Post on 05-Jan-2016

31 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

More Trees. Outline. B Tree, B* Tree, B+ Tree 2-3 Tree, 2-3-4 Tree Red-Black Tree (RBT) Left-Leaning Red-Black Tree Double Red & Double Black. B Tree. - PowerPoint PPT Presentation

TRANSCRIPT

2-3-4

Tree

and how it relates to Red Black Tree

Outline

B Tree, B* Tree, B+ Tree

2-3 Tree, 2-3-4 Tree

Red-Black Tree (RBT)

Left-Leaning Red-Black Tree

Double Red & Double Black

RBT insert Example       Same EX. with 2-3-4 tree

B Tree

B-Tree is in memory of R. BayerIt is a generalization of binary search tree in that a node (or, an entry) can have more than two children [wiki].

Degree 為 d 的 B tree: 1) 每個 node 含至多 d 個 child pointers ( 或 d-1 個 elements) 2) 每個 node 至少 1/2 滿 ( 即至少 [ (d-1)/2] 個 elements)

B-Tree of Degree 3

20 28

10 25 30

B* Tree

B-tree 的 node 至少 2/3 滿

B* Tree of Degree 4

20 28

15 26 30

6

10 232 4 35

B+ Tree

• 含 index pages 和 data pages• root node 和 internal nodes

為 index pages (keys only).

• leaf nodes 為 data pages ( 排序的 data )

data 即 element ( 含有 key)• 每個 node 至少 1/2 滿 (Fill Factor

50%).

B+ Tree index page

data page

Number of Keys 4

Number of Pointers 5

Fill Factor 50%

Minimum Keys in each page 2

This B+ tree:

2-3 Tree

2-3 Tree

為 search tree 可為空或 :

每個 internal node 為 2-node ( 有 2 child pointers)

或 3-node ( 有 3 child pointers)

40

10 20 80

2-node

B 3-node C

A

2-3 Tree Insertion insert Case 1: 插入 70

• 先尋找 70. 發現不在其中 .• 須知尋找 70 時 遇到哪 node?

是 含 80 的 node C• node C 只有一個 element,

所以 70 可放 C

40

10 20 70 80

C

A

B

2-3 Tree

insert Case 2: 插入 30

• 會遇到 30 的是 node B• B 為 3-node, 須產生新 node D.• B 含 elements 10, 20, 30• 其中最大 element 30 放 D • 最小 element 10 放 B.• 中間 20 放 B 的 parent A

這叫 Split ( 分裂 ):1. 產生新 node D. 2. 中間 20 推升上層

80

C

A

20 40

10 70

B

Figure 3

30

D

2-3 Tree Insertion (Cont.) insert case 3: 插入 60

• 尋找 60 會遇 node C• C 為 3-node, 需產生新 node E • C 含 elements 60,70,80 • 中間值 70 放在 C 的 parent A • 最小值 60 放 C 最大值 80 放 E• A 為 3-node, 產生新 node F• A 含 elements 20, 40, 70 • 中間值 40 放在 A 的 parent G ( 需產生

G)• 最小值 20 放 A 最大值 70 放 F

2-3 Tree Insertion (Cont.)

40

G

70

F

80

E

60

C

20

A

10

B

30

D

Figure 4 Insertion of 60 into the 2-3 tree of Figure 3

2-3 Tree Deletion

50 80

A

10 20

B60 70

C90 95

D

50 80

A

10 20

B60

C90 95

D

(a) Initial 2-3 tree

(b) 70 deleted

2-3 Tree Deletion (Cont.)

50 80

A

10 20

B60

C95

D

(c) 90 deleted

20 80

A

10

B50

C95

D

(d) 60 deleted Next, delete 95

2-3 Tree Deletion (Cont.)

20

A

10

B50 80

C

(e) 95 deleted

20

10

B80

C

(f) 50 deleted

A

20 80

(g) 10 deleted

A

這叫 Merge (融合 ):1.消去 node D2.上層 80 併入下層

2-3-4 Tree

2-3-4 Tree

它為 search tree 可為空或 :– 每個 internal node 為 2, 3, 或 4 node.(2-node 有 2 child pointers, 3-node 有 3 child

pointers, 4-node 有 4 child pointers)– 所有 external nodes 都在相同 level.

2-3-4 tree 類似 2-3 tree, 但它有 4-node 如下圖

50 60 70

2-3-4 Tree Insertion

There are 3 cases for a 4-node:

Case 1: It is the root

Case 2: Its parent is a 2-node

Case 3: Its parent is a 3-node (fig. omitted)

2-3-4 Tree Insertion

• Case 1: It is the root.

x y z

a b c d

t (root)

y

x z

a b c d

t

Figure1 when the root is a 4-node

2-3-4 Tree Insertion

• Case 2: Its parent is a 2-node

w x y

a b c d

z

e w

a b c d

x z

ey

Figure 2 when the child of a 2-node is a 4-node

2-3-4 Tree

2-3-4 tree 轉成 binary search tree

則稱為 red-black tree

red-black tree 比 2-3-4 tree 節省空間 因為 2-3-4 node 會浪費不少未存資料的空的空間

Red-Black Tree(RBT)

Red-Black Tree red-black tree 為 binary search tree:

• 每個 node 不是 red 就是black

• 每個 leaf (NULL) 都為 black• red node 的兩個 children 都

為 black. • 每個 path 含相同數目的

black nodes. • red node 不可接著 red node

( 不可紅紅 ) A basic red-black tree

Red-Black Tree

A red-black tree with n internal nodes has height at most 2 log(n+1).

Red-Black tree can always be searched in O (log n) time.

Red-Black Tree

c

S L

L

S

S

L

a b

ca

b

OR

a b c

Figure 1 Transforming a 3-node into two red-black nodes

S for Small L for Large

Left-leaning

Right-leaning

Red-Black Tree

Figure 2 Transforming a 4-node into two red-black nodes

S M LM

S La b c d

b c d

S for Small M, Middle L, Large

a

50

10 70

80

5

7

9 30

40 60

75 90

85 92

1. 將下圖的 Red-Black Tree 轉成 2-3-4 Tree

2. 依序 (1)刪除 60 (2)加入 8

3. 再轉回 Red-Black Tree

上圖轉成的 2-3-4 Tree

50

10 70 80

5 7 9 30 40 60 75 85 90 92

刪除 60

70

wasted space

加入 8

8

7

轉回 Red-Black Tree 50

7

10 90705

80

9

8 75 85 9230

40

Red Black Tree Saves Space

In the example above, the 2-3-4 tree wastes 8 unused space of elements.

The corresponding red-black tree

cuts this waste!

Left-Leaning Red-Black Tree

LLRBT is easier to implement than RBT, especially the deletionIt requires

3-nodes are left-leaning, thus maintains 1-1 correspondence with 2-3-4 trees (see next page).

LL Red-Black Tree

S L

L

S

a b

c

a b c

Transforming a 3-node into LL red-black nodes

S for Small L for Large

Left-Leaning (LL)

Double Red

&

Double Black

During Red-Black Tree insertion, abnormal Double Red may occur as shown next.

41

Red-Black Tree Insertion

我們要對左圖  insert 4 1) 依 binary search tree   把 4 當 3 的 right child 2) 依 red black tree 新加入者為 red

故 3,4 形成右圖 Double Red 違反 Red Rule

2

31

4

2

31

Double Red in 2-3-4 Tree

1 2 3已滿 , 此時 insert

4

1 2 3 4這 node 爆掉了 , 故要調整

之對應的 Red-Black Tree:

2

1 3

4

此時 叫 Double Red 雙紅 , 表示原來 node 爆掉了

3 4

During Red-Black Tree deletion, again, abnormal Double Black may occur as shown next.

Double Black in 2-3-4 Tree7 8

5

3

此時 Delete 5

7 8

3

對應的 Red-Black Tree:

7

3

Double BLACK 雙黑線 , 表示其中有個空 2-3-4 node.故要調整之

8

HW12.7 RBT insert 30

30 30

RBT insert 40

40

30

RBT insert 20

40

30

20

RBT insert 90

40

30

20

90

40

30

20

90

40

30

20

90

RBT insert 10

40

30

20

9010

RBT insert 50

40

30

20

10 90

50

40

30

20

10 50

90

50

30

20

10 9040

RBT insert 70

50

30

20

10 9040

70

50

30

20

10 9040

70

RBT insert 60

50

30

20

10 7040

60

50

30

20

10 9040

70

60

90

RBT insert 80

50

30

20

10 7040

60 90

80

50

30

20

10 7040

60 90

80

70

50

9060

80

30

4020

10

2-3-4 tree insert 30

30

2-3-4 tree insert 40

30 40

2-3-4 tree insert 20

20 30 40

2-3-4 tree insert 90

30

20 40 90

2-3-4 tree insert 10

30

10 20 40 90

2-3-4 tree insert 50

30

10 20 40 50 90

2-3-4 tree insert 70

30 50

10 20 70 9040

2-3-4 tree insert 60

30 50

10 20 60 70 9040

2-3-4 tree insert 80

30 50 70

10 20 6040 80 90

浪費空間 = 6 elements / 15 elements = 40 %

top related