30
Chapter 7 - 1 Chapter 7 Search Structures Chapter 7 - 1 TABLE OF CONTENTS Binary Search Tree as a Solution Optimal Binary Search Trees AVL Trees Two-Three Trees (2-3 Trees) Two-Three-Four Trees (2-3-4 Trees) Red-Black Trees

Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

  • Upload
    vannhan

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 1

÷�öOF fj¶�j� æbÚ

Chapter 7

Search Structures

Chapter 7 - 1÷�öOF fj¶�j� æbÚ

TABLE OF CONTENTS

● Binary Search Tree as a Solution

● Optimal Binary Search Trees

● AVL Trees

● Two-Three Trees (2-3 Trees)

● Two-Three-Four Trees (2-3-4 Trees)

● Red-Black Trees

Page 2: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 2

Chapter 7 - 2÷�öOF fj¶�j� æbÚ

1. Binary Search Trees as a Solution

z ��✔ Every element has a unique key✔ Key in a nonempty left subtree < Key of root✔ Key in a nonempty right subtree > Key of root✔ Left & right subtrees are also binary search trees✔ þ�bú�Ó : î² 8.2 ~ î² 8.4

z ��✔ Index r sorted order R�¶V:�¼Ú.✔ ¾R³{��nÒ�c·¢únÒÆúN

z ���✔ Balanced tree r¶ÆN¶±^ ( î² 8.5 ~ î² 8.6 )✔ Too many seeks

– 24�N key Rb'� tree: 5~N seek

Chapter 7 - 3÷�öOF fj¶�j� æbÚ

KF

SD

PA

NR

WS

RF TK YJ

FB

CL

AX

HN

JDDE FT

FIGURE 8.2 Binary search tree representation of the list of keys.

Page 3: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 3

Chapter 7 - 4÷�öOF fj¶�j� æbÚ

KF

FB SD

FIGURE 8.3 Linked representation of part of a binary search tree.

Chapter 7 - 5÷�öOF fj¶�j� æbÚ

FB

JD

RF

SD

AX

YJ

PA

FT

10

6

11

8

13

2

HN

KF

CL

NR

DE

WS

TK

7

0

4

14

1

3

12

5

ROOT

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

9

KeyLeftchild

Rightchild Key

Leftchild

Rightchild

FIGURE 8.4 Recordcontents for a linked representation of the binary tree in Fig. 8.2.

Page 4: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 4

Chapter 7 - 6÷�öOF fj¶�j� æbÚ

KF

SD

PA

NR

WS

RF TK YJ

FB

CL

AX

HN

JDDE FT

LV

FIGURE 8.5 Binary search tree with LV added.

Chapter 7 - 7÷�öOF fj¶�j� æbÚ

KF

SD

PA

NR

WS

RF TK YJ

FB

CL

AX

HN

JDDE FT

LV

NP

MB

ND

NK

LA

TM

UF

TS

FIGURE 8.6 Binary search tree showingthe effect of added keys.

Page 5: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 5

Chapter 7 - 8÷�öOF fj¶�j� æbÚ

2. Optimal Binary Search Trees

z ��

✔ BST������ average search length����!✔ ����� access probability������.

IRU

GR ZKLOH

YRLG

LI

IRU

GR ZKLOH

YRLG LI

Chapter 7 - 9÷�öOF fj¶�j� æbÚ

Extended Binary Tree

z Internal Path Length I = 0 + 1 + 1+ 2 + 3 = 7

z External Path Length E = 2 + 2 + 4 + 4 + 3 + 2 = 17

z E = I + 2n (n: # of internal nodes)➙ I��������, �� E����

IRU

GR ZKLOH

YRLG

LI

,QWHUQDO 1RGH

([WHUQDO 1RGH

Page 6: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 6

Chapter 7 - 10÷�öOF fj¶�j� æbÚ

Cost of Extended Binary Search Tree

z Key� access probability������

✔ ������ = Complete binary tree✔ Smallest value for I: Σi=1 log2i = O(nlog2n)

z Key� access probability������

✔ Key �: a1, a2, …, an (a1 < a2 < … < an)✔ pi: probability of searching ai

✔ Ei: all identifiers x such that ai < x < ai +1✔ qi: ����� key�� Ei������

✔ Cost: Σi=1 pi•level(ai) + Σi=0 qi•(level(failure node i) – 1)✔ Σi=1 pi + Σi=0 qi = 1

∑=

n

i 1

Chapter 7 - 11÷�öOF fj¶�j� æbÚ

Example

z pi = qj = 1/7 for all i and j✔ cost(tree a) = 13/7, cost(tree b) = 15/7

z p1 = .5, p2 = .1, p3 = .05, q0 = .15, q1 = .1, q2 = .05, q3 = .05✔ cost(tree a) = 1.9, cost(tree b) = 1.5

LI

GR ZKLOH

�D�

GR

LI

ZKLOH

�E�

Page 7: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 7

Chapter 7 - 12÷�öOF fj¶�j� æbÚ

Finding Optimal Binary Search Tree

z �� 1: ����� BST��� cost ��

✔ Complexity = O(n N(n))✔ N(n): ���� n BST��� = O(4n/n3/2)

z �� 2: Greedy Algorithm✔ Let n identifiers: a1 < a2 < …< an

✔ Tij: optimal BST for ai+1, …, aj, i < j (ak < ak+1)– rij: root of Tij

– wij: weight of Tij = qi + Σ(qk + pk), i+1≤ k ≤ j– By definition, rii = 0 and wii = qi, 0 ≤ i ≤ n

✔ Goal: Find T0n!

Chapter 7 - 13÷�öOF fj¶�j� æbÚ

T0n� �����

z Tij: optimal BST for ai+1, …, aj and rij = k (i < k ≤ j)

✔ Cost cij = pk + cost(L) + cost(R) + weight(L) + weight(R)– weight(L) = weight(Ti, k-1) = wi, k-1

– weight(R) = weight(Tkj) = wkj

✔ cij = pk + ci, k-1 + ckj + wi, k-1 + wkj = wij + ci, k-1 + ckj

✔ wij + ci, k-1 + ckj = min{wij + ci, k-1 + ckj}, i < l ≤ j✔ ci, k-1 + ckj = min{ci, k-1 + ckj}, i < l ≤ j

ak

/ 5

Page 8: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 8

Chapter 7 - 14÷�öOF fj¶�j� æbÚ

void obst(int p[], int q[], int cost[][M], int root[][M], int weight[][M], int n){

int i, j, k, m, min, minpos;for (i = 0; i < n, i++) {

weight[i][i] = q[i]; root[i][i] = cost[i][i] = 0;cost[i][i+1] = weight[i][i+1] = q[i] + q[i+1] + p[i+1];root[i][i+1] = i + 1;

}weight[n][n] = q[n]; root[n][n] = cost[n][n] = 0;for (m = 2; m <= n; m++)

for (i = 0; i <= n-m; i++) {j = i + m;weight[i][j] = weight[i][j-1] + p[j] + q[j];k = knuth_min(cost, root, i, j);cost[i][j] = weight[i][j] + cost[i][k-1] + cost[k][j];root[i][j] = k;

} }

Program: Function to find an optimal binary search tree

Chapter 7 - 15÷�öOF fj¶�j� æbÚ

z Example✔ n = 4 and (a1, a2, a3, a4) = (do, for, void, while)✔ (p1, p2, p3, p4) = (3, 3, 1, 1)✔ (q0, q1, q2, q3, q4) = (2, 3, 1, 1, 1)

:�� ��

&�� ��

5�� �

:�� ��

&�� ��

5�� �

:�� ��

&�� ��

5�� �

:�� �

&�� �

5�� �

:�� �

&�� ��

5�� �

:�� ��

&�� ��

5�� �

:�� �

&�� �

5�� �

:�� �

&�� �

5�� �

:�� �

&�� �

5�� �

:�� �

&�� �

5�� �

:�� �

&�� �

5�� �

:�� �

&�� �

5�� �

:�� �

&�� �

5�� �

:�� �

&�� �

5�� �

:�� �

&�� �

5�� �

IRU

GR YRLG

ZKLOH

Page 9: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 9

Chapter 7 - 16÷�öOF fj¶�j� æbÚ

3. AVL Trees

z Introduction✔ G. M. Adel’son-Vel’skii & E. M. Landis �����

✔ Height-balanced tree – HB(1) tree

z ��

✔ 2 �� Subtree ��� depth �� ≤ 1✔ 4 ��� ( LL, LR, RL, RR ) ��� tree ���

✔ ����� 5 ������

✔ � : B C G E F D A ��� ��� ( �� 8.11 )✔ Search cost : 1.44log2(N+2) ↔ log2(N+1) of B.S.T

Chapter 7 - 17÷�öOF fj¶�j� æbÚ

D

B F

A C E G

E

C F

B D G

A

FIGURE 8.10 A completely balanced search tree.

FIGURE 8.11 A search tree constructed using AVL procedures.

Page 10: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 10

Chapter 7 - 18÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

MarMar

May

0-1

0

Mar

May

Nov

May

NovMar

(a) Insert March (b) Insert May

(c) Insert November

RR

Rotation

-2

-1

0

0

0

0

Chapter 7 - 19÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

May

NovMar

+1

+1

0

Aug

0

(d) Insert August

May

NovMar

+2 0

Aug

+1

Apr

0

+2

May

Nov

0

Aug

0

Apr

0

Mar

0

+1LL

Rotation

(e) Insert April

Page 11: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 11

Chapter 7 - 20÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

May

Nov

0

Aug

-1

Apr

0

Mar

+1

+2

Jan

0

Mar

Nov

0Aug

0

Apr

0

Jan

0

0

May

-1

LR

Rotation

(f) Insert January

Chapter 7 - 21÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

Mar

May

-1

Aug

-1

Apr

0

Jan

+1

+1

Dec

0

(g) Insert December

Nov

0

Page 12: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 12

Chapter 7 - 22÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

Mar

May

-1

Aug

-1

Apr

0

Jan

0

+1

Dec

(h) Insert July

Nov

0

July

0

Chapter 7 - 23÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

Mar

May

-1

Aug

-2

Apr

0

Jan

+1

+2

Dec

(i) Insert February

Nov

0

July

0

Feb

0

-1

Mar

May

-1

Dec

0

Aug

+1

Jan

0

+1

Feb

Nov

0

July

00

Apr

0

RL

Rotation

Page 13: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 13

Chapter 7 - 24÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

Mar

May

-1

Dec

-1

Aug

+1

Jan

-1

+2

Feb

(j) Insert June

Nov

0

July

-1

June

0

0

Jan

Mar

0

Dec

+1

Aug

+1

Feb

0

0

June

May

-1

July

-1

0Apr

0

LR

Rotation

Apr

0

Nov

0

Chapter 7 - 25÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

Jan

Mar

-1

Dec

+1

Aug

+1

Feb

0

-1

June

May

-2

July

-1

0

Apr

0

Nov

-1

Oct

0

RR

Rotation

(k) Insert October

Page 14: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 14

Chapter 7 - 26÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

Jan

Mar

0

Dec

+1

Aug

+1

Feb

0

0

June May

0

July

-1

0

Apr

0

Nov

0

Oct

0

RR

Rotation

(k) Insert October

Chapter 7 - 27÷�öOF fj¶�j� æbÚ

� Insertion into an AVL tree

Jan

Mar

-1

Dec

+1

Aug

+1

Feb

0

-1

June May

0

July

-1

0

Apr

0

Nov

-1

Oct

-1

(l) Insert September

Sept

0

Page 15: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 15

Chapter 7 - 28÷�öOF fj¶�j� æbÚ

4. Two-Three Trees (2-3 Trees)

z Definition✔ Internal node: 2-node or 3-node

– 2-node: one element with two children– 3-node: two element with three children

✔ All external nodes are at the same level.

z Properties✔ n: number of elements, h: height of 2-3 trees✔ 2h – 1 ≤ n ≤ 3h – 1✔ log3(n + 1) ≤ h ≤ log2(n + 1)

Chapter 7 - 29÷�öOF fj¶�j� æbÚ

Searching a 2-3 Tree

W\SHGHI VWUXFW WZRBWKUHH WZRBWKUHHBSWU�VWUXFW WZRBWKUHH ^

HOHPHQW GDWDBO� GDWDBU�WZRBWKUHHBSWU OHIWBFKLOG� PLGGOHBFKLOG� ULJKWBFKLOG�

`�

WZRBWKUHHBSWU VHDUFK���WZRBWKUHHBSWU W� HOHPHQW [�^

ZKLOH �W�VZLWFK �FRPSDUH�[� W�� ^

FDVH �� W W�!OHIWBFKLOG� EUHDN�FDVH �� W W�!PLGGOHBFKLOG� EUHDN�FDVH �� W W�!ULJKWBFKLOG� EUHDN�FDVH �� UHWXUQ W�

`UHWXUQ 18//�

`

Page 16: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 16

Chapter 7 - 30÷�öOF fj¶�j� æbÚ

Insertion Into a 2-3 Tree

��

���� ��� ��

��

�� ���� ��

� ��

�� ��

�� �� ����� ��

Chapter 7 - 31÷�öOF fj¶�j� æbÚ

��

��

�� ��

�� �� ��

�� ��

�� �� ����

� ��

Page 17: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 17

Chapter 7 - 32÷�öOF fj¶�j� æbÚ

YRLG LQVHUW�� �WZRBWKUHHBSWU W� HOHPHQW \�^

WZRBWKUHHBSWU T 18//� S� WHPS�

LI ��� W�� QHZBURRW�W� \� 18//��HOVH ^

LI ���S ILQGBQRGH� W� \��� H[LW���� � GXSOLFDWH �ZKLOH ���

LI �S�!GDWDBU�NH\ ,17B0$;� ^ � ��QRGH �SXWBLQ�S� \� T�� EUHDN�

`HOVH ^ � ��QRGH �

LI �S W� ^ � VSOLW WKH URRW �QHZBURRW�W� \� T�� EUHDN�

`HOVH S GHOHWH���

` ` `

Program: Insertion into a 2-3 tree

Chapter 7 - 33÷�öOF fj¶�j� æbÚ

Deletion From a 2-3 Tree

z ����

✔ Leaf node����� ��: leaf node �����

– Inorder predecessor���, or– Inorder successor���

✔ Leaf node�����

– 3-node: ����, Node ���������

– 2-node: Node���� �����

• Rotation• Combine

Page 18: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 18

Chapter 7 - 34÷�öOF fj¶�j� æbÚ

Example – ����

�� ��

�� ���� �� �� ��

�� ��

���� �� �� ��� ��

� ��

�� ��

���� �� �� � ��

Chapter 7 - 35÷�öOF fj¶�j� æbÚ

Example: Rotate & Combine

�� ��

���� ��� ��

��

�� �� ��

� ��

��

���� � ���� ��

Page 19: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 19

Chapter 7 - 36÷�öOF fj¶�j� æbÚ

Steps in deleting from a leaf node, p

Step 1: Modify p as necessary to reflect its status after the desired element has been deleted.

Step 2: while (p has zero elements && p is not the root) {let r be the parent of p;let q be the left or right sibling of p (as appropriate);if (q is a 3-node) rotate;else combine;p = r;

}

Step 3: If p has zero elements, then p must be the root. The left child of p becomes the new root and p is deleted.

Chapter 7 - 37÷�öOF fj¶�j� æbÚ

5. Two-Three-Four Trees (2-3-4 Trees)

z Definition: Definition of 2-3 Tree +✔ 4-node: three elements and four children

✔ n: number of elements, h: height of 2-3-4 trees✔ 2h – 1 ≤ n ≤ 4h – 1✔ log4(n + 1) ≤ h ≤ log2(n + 1)

z Node Structure

GDWDBO GDWDBP GDWDBU

OHIWBFKLOG OHIWBPLGBFKLOG ULJKWBPLGBFKLOG ULJKWBFKLOG

Page 20: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 20

Chapter 7 - 38÷�öOF fj¶�j� æbÚ

An Example 2-3-4 Tree

�� �� �������� ��� � �

�� �� ��

��

Chapter 7 - 39÷�öOF fj¶�j� æbÚ

Insertion Into a 2-3-4 Tree

z 2-3 Tree� Insertion Algorithm����

✔ Leaf node� split��� backward splitting ��

✔ Root�� tree� traverse��, 4-node�����

✔ �, ��

– ��: Backward leaf to root pass ��

– ��: Node utilization ��

z 4-node������������

✔ It is the root of the 2-3-4 tree.✔ Its parent is a 2-node.✔ Its parent is a 3-node.

Page 21: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 21

Chapter 7 - 40÷�öOF fj¶�j� æbÚ

Transformation: Root 4-node

[ \ ]

D E F G

W

\

[

D E

]

F G

W

Chapter 7 - 41÷�öOF fj¶�j� æbÚ

Transformation: Child of a 2-node

Z [ \

D E F G

]

H

[ ]

Z

D E

\

F G

H

[ \ ]

E F G H

Z

D

Z \

[

E F

]

G H

D

Page 22: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 22

Chapter 7 - 42÷�öOF fj¶�j� æbÚ

Transformation: Child of a 3-node

Y Z [

D E F G

\ ]

H

Z [ \

E F G H

Y ]

D

I

I

Z \ ]

Y

D E

[

F G

H I

Y [ ]

Z

E F

\

G H

D I

Chapter 7 - 43÷�öOF fj¶�j� æbÚ

YRLG LQVHUW��� �WZR��SRLQWHU W� HOHPHQW \�^

WZR��SRLQWHU S W� U 18//�

LI ��� W�� QHZBURRW�W� \��HOVH ^

LI �IRXUBQRGH� W�� VSOLWBURRW�W��ZKLOH ��� ^

LI �IRXUBQRGH�S�� ^LI �QRGHBW\SH�U� WZRBQRGH� VSOLWBFKLOGBRI��S� U��HOVHVSOLWBFKLOGBRI��S� U��S U�

`U S�VZLWFK �FRPSDUH�\� S�� ^

FDVH HTXDO� H[LW���� � GXSOLFDWH �FDVH OHDI� SXWBLQ�\� S�� UHWXUQ�FDVH OFKLOG� S S�!OHIWBFKLOG� EUHDN�FDVH OPFKLOG� S S�!OHIWBPLGBFKLOG� EUHDN�FDVH UPFKLOG� S S�!ULJKWBPLGBFKLOG� EUHDN�FDVH UFKLOG� S S�!ULJKWBFKLOG�

` ` ` `

Program: Insertion into a 2-3-4 tree

Page 23: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 23

Chapter 7 - 44÷�öOF fj¶�j� æbÚ

Deletion From a 2-3-4 Tree

z Backward leaf to root restructuring path���

✔ Tree�����, ���� 3-/4-node������

✔ Insertion������, ��������!

z Algorithm (p: current node, q: child node, r: sibling of q)✔ p = leaf: root����� 2-node. Otherwise, 3-/4-node✔ q ≠ 2-node: No restructuring. Just forward to q.✔ q = 2-node & r = 2-node:

– p = 2-node: p, q, r���������

– p ≠ 2-node: p���, q, r��� (insertion���)✔ q = 2-node & r ≠ 2-node: rotate (�����)

Chapter 7 - 45÷�öOF fj¶�j� æbÚ

Deletion Transformation: Rotate

Z ]

Y

D E

I

[ \

F G H

p

q r

[ ]

I

p

q Y Z

D E

\

G HF

r

Y \ ]

X

D E

Z [

F G

I

H

p

q r

J

Z \ ]

I

p

J

q X Y

D E

[

G HF

r

Page 24: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 24

Chapter 7 - 46÷�öOF fj¶�j� æbÚ

6. Red-Black Trees

z Red-Black Tree: 2-3-4 tree� binary tree ����

✔ Black pointer: child pointer of the original 2-3-4 tree✔ Red pointer: sibling relationship of the 2-3-4 tree

[ \

D E F

\

[

D E

F or

[

\

E F

D

[ \ ]

D E F G [

D E

]

F G

\

Chapter 7 - 47÷�öOF fj¶�j� æbÚ

Properties of Red-Black Tree

z Basic Properties✔ Binary search tree✔ Every root to external path: same number of black links✔ No root to external path: 2 or more consecutive red links

z Rank������� Properties✔ Binary search tree✔ Rank(external node) = 0✔ Rank(parent internal node of external node) = 1✔ rank(x) ≤ rank(parent(x)) ≤ rank(x) + 1✔ rank(x) < rank(grand_parent(x))

Page 25: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 25

Chapter 7 - 48÷�öOF fj¶�j� æbÚ

Properties of Red-Black Tree (Cont’d)

z Intuitive Definition of Rank✔ Suppose a node x of a 2-3-4 tree T.✔ Rank of the corresponding nodes in red-black tree =

height(T) – level(x) + 1

z Every red-black tree RB with n (internal) nodes✔ height(RB) ≤ 2 log2(n + 1)✔ height(RB) ≤ 2rank(RB)✔ rank(RB) ≤ log2(n + 1)

�Definition: Rank of a tree = Rank of the roor

Chapter 7 - 49÷�öOF fj¶�j� æbÚ

Operations On a Red-Black Tree

z Searching a Red-Black Tree✔ ��� BST������������

✔ Pointer color�������

z Insertion✔ Top Down Insertion: 4-node splitting transformation✔ Bottom Up Insertion: BST�� �����

z Deletion✔ 2-3-4 tree�������� red-black tree� ��

z Red-Black Tree���

✔ � , ��� link� color����� (��)✔ ��������������

Page 26: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 26

Chapter 7 - 50÷�öOF fj¶�j� æbÚ

Top Down Insertion

z Transforming a node q with two red links (4-node)✔ q��� link� black link���

✔ q� parent� left child���, parent� left color = red– Right child������������

✔ ���, 2�� red link�������

– gp → p → q (gp: grand parent of q, p: parent of q)– gp → p� X, p → q� Y����, XY ��� ��

4������� (LL, LR, RL, RR)– AVL ���������� rotation

z Leaf ���� red link�������

Chapter 7 - 51÷�öOF fj¶�j� æbÚ

Transformation: Root 4-node

[

D E

]

F G

\

[

D E

]

F G

\

Page 27: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 27

Chapter 7 - 52÷�öOF fj¶�j� æbÚ

Transformation: Child of a 2-/3- node

[

D E

]

F G

\

]

H

[

D E

]

F G

\

]

H

[

E F

]

G H

\

]

D

[

E F

]

G H

\

]

D

Chapter 7 - 53÷�öOF fj¶�j� æbÚ

Y

D E

[

F G

Z

\

H

]

I

Y

D E

[

F G

Z

\

]

IH

LL rotation

Y

D E

[

F G

Z

\

]

IH

Color Change

Y

D E

[

F G

Z

\

]

IH

Page 28: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 28

Chapter 7 - 54÷�öOF fj¶�j� æbÚ

Z

E F

\

G H

[

]

I

Y

D

Z

E F

Y

[

]

I

RL rotation

\

G H

D

Z

E F

\

G H

[

]

IY

DZ

E F

Y

[

]

I

LR rotation

\

G H

D

Chapter 7 - 55÷�öOF fj¶�j� æbÚ

Bottom Up Insertion

z Algorithm✔ Leaf node�����, red link��� leaf node���

✔ ���, red link�������.– ��� red link: p → q (X), q → r (Y)– s� q� sibling����, XYZ violation ��

• X = L, if p → q is a left link. X = R otherwise.• Y = L, if q → r is a left link. Y = R otherwise.• Z = r (for red) if s ≠ NULL and p → s is a left link.

Z = b (for black) otherwise.✔ LLr� LRr, LLb� LRb���������

✔ Bottom up�� color change ��� propagation

Page 29: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 29

Chapter 7 - 56÷�öOF fj¶�j� æbÚ

Z

D E

[

\

]

HGF

//U

Z

D E

[

\

]

HGF

[

E F

Z

\

]

HGD

/5U

[

E F

Z

\

]

HGD

Chapter 7 - 57÷�öOF fj¶�j� æbÚ

Z

D E

[

\

]

HGF

[

E F

Z

\

]

HGD

//E Z

D E

\

[

]

HG

F

/5E Z

D E

\

[

]

HG

F

1RWH� 2QO\ � URWDWLRQ SHU LQVHUWLRQ�

Page 30: Search Structures - YUynucc.yu.ac.kr/~hrcho/Courses/FS/Chapter07.pdf · Chapter 7 - 1 ÷ ö O F f j ¶ j æ b Ú Chapter 7 Search Structures ÷ ö O F f j ¶ j æ b Ú Chapter 7 -

Chapter 7 - 30

Chapter 7 - 58÷�öOF fj¶�j� æbÚ

Deletion From a Red-Black Tree

z ��� leaf node� parent�������?✔ Red link: leaf node� 3-/4- node��� (����)✔ Black link: leaf node� 2-node

– Leaf node���� backward restructuring ��

z Backward restructuring�����

✔ 2-3-4 deletion transformation���.✔ Insertion������ transformation.