• Tidak ada hasil yang ditemukan

B-TREES

N/A
N/A
Protected

Academic year: 2024

Membagikan "B-TREES"

Copied!
20
0
0

Teks penuh

(1)

B-TREES

(2)

Multiway Search Trees

d e p v

s t u

b c h j k l r w x y z

a f g i m n o q

A balanced 5-way search tree

(3)

Multiway Search Trees

#define NUMKEY_MAX 4 /* maximum number of keys */

typedef int KeyType;

typedef struct NodeTag {

int count;

KeyType key[ NUMKEY_MAX + 1 ];

InfoType *pInfo[ NUMKEY_MAX + 1 ];

struct NodeTag *pBranch[ NUMKEY_MAX + 1 ];

} NodeType;

d e p key[]

pBranch[]

0 1 2 3 4

count 3

d e p

pInfo[]

(4)

Multiway Search Trees

Nodetype *MSearch(KeyType Target, NodeType *pRoot, int *pPos ) {

if ( pRoot == NULL ) return( NULL );

if ( LT(Target, pRoot-key[1]) )

return( MSearch( Target, pRoot->pBranch[0], pPos ) );

else {

*pPos = 1;

while( *pPos <= pRoot->count && GT(Target,pRoot->key[*pPos]) (*pPos)++;

if ( EQ( Target, pRoot->key[ *pPos ] ) ) return( pRoot );

else

return( MSearch( Target, pRoot->pBranch[*pPos], pPos );

}

} key[]

pBranch[]

count 3

d e p

pInfo[]

(5)

B-Trees

A B-tree of order 5

h i j k l

a b c e f m n p q r t u v x y z

d g o s w

(6)

B-Trees

! B-tree of order m คือ m-way search tree ที่

! แตละจุดเก็บคียไดอยางมาก m-1 คีย

! จุดที่ไมใชราก จะตองเก็บคียอยางนอย ?m/2 - 1 คีย

! รากจะตองเก็บคียอยางนอย 1 (ยกเวนวาตนไมไมมีขอมูล)

! ใบทุกใบของตนไมอยูในระดับเดียวกัน

(7)

Insertion into a B-Tree

f

a b d g h j

e i k m p s

Insert q

k m p q s median

f

a b d g h

j

e i k m

Insert p

q s

f

a b d g h

j

e i k m q s

p

Split

(8)

Growth of a B-Tree

a a g a f g a b f g

f

a b g k

f

a b d g h k m

f

a b d g h j

k m

f

a b d g h j

e i k m r s

Insert: j Insert: e,s,i,r

Insert: d,h,m

(9)

Growth of a B-Trees

f

a b d g h

j

e i k m s

r

x

f

a b d g h

j

k m

e i

r

s x

c

l n t u Insert: c,l,n,t,u

Insert: x

(10)

Growth of a B-Trees

a b d e g h i k n

j

l p s t u x

c f m r

Insert: p

(11)

Binary Search-Trees vs B-Trees

Seach Trees

B-Trees

(12)

Deletion from a B-Tree

สมมติวาตองการลบขอมูลที่คียมีคา x ถาคีย x นี้อยูใน node ภายใน B-tree

1. ใหหาคียที่มีคามากที่สุดที่นอยกวา x หรือหาคียที่มีคานอยที่สุดที่มากกวา x 2. ส ใหคียดังกลาวนี้คือ y ใหยายคีย y นี้ไปแทนที่ ณ ตํ าแหนงที่ x อยู

ซึ่งเหมือนกับกรณีที่ลบคียใน binary search tree หรือ AVL-tree

ใหสังเกตุวา คีย y ที่หาไดนี้ จะตองอยูใน node ที่เปนใบเสมอ จึงเปนการเปลี่ยนป หาการล ใน node ใดๆ เปนการลบคีย y ที่อยูในใบ

f

a b d g h j

m p

f

a b d g h h

m p

Delete : j Delete : h

(13)

Deletion from a B-Tree

f

a b d g h j

m p Delete : h

i

f

a b d g i j

m p

f

a b d g i j

m p

x x

x

f

a b d g j m

p x

A B C D A B C D

(14)

Deletion from a B-Tree

f

a b d g m p x

A B C D E

Delete j : Combine g,m,p,x f

a b d g j m

p x

A B C D E

(15)

Deletion : Example

Delete : h,r,p,d

a b d e g h i k n

j

l p t u x

c f m r s

z

(16)

Secondary Storages

Cost measure

1. เวลาในการเคลื่อนหัวอาน/เขียนขอมูล ไปยัง track ที่ตองการ

2. เวลาในการรอให block ที่เก็บขอมูลบน track หมุนมาที่ตรงหัวอาน 3. เวลาในการอาน/เขียน (ถายเท) ขอมูล 1 ไบต

(b = จํ านวนไบตตอหนึ่ง block)

( )ts

( )ts ( )ts

( t

s

)

disk

magnetic surface

read/write head disk track

(17)

The Height of a B-Tree

. . . . . .

...

.. ...

..

...

.. ...

..

t-1

t-1

t-1 t-1 t-1 t-1 t-1 t-1 t-1 t-1

t-1 t-1

t-1

t-1 1

จํ านวน nodes

1

2

2t

2t2

( t s )

(18)

Searching a B-Tree : Analysis

สมมติวาเวลาในการอาน 1 node ของ B-tree ที่มี order m จาก disk คือ โดยใช binary search ในการหาคียที่ตองการใน node ที่อานได ซึ่งใชเวลา

และจํ านวน nodes ที่ตองอาน (worst case) ใน B-tree (ซึ่งมี n nodes) เพื่อหาคียที่ตองการ จะ เทากับความสูงของตนไม h ดังนั้นเวลาในการหาคียคือ

a bm+

a + bm

a bm +

เวลา

(19)

Insertion into a B-Tree : Analysis

สมมติให B-tree order m สูง h มี p nodes และเก็บขอมูล n ขอมูล การแทรกขอมูลประกอบดวย

1. การหาใบที่ตองการ ซึ่งตองอานขอมูล h+1 ครั้ง

2. อาจเกิดการ split node สมมติวาเกิดขึ้น k ครั้ง โดยที่ k p-2 โดยการ split แตละครั้ง ตองเขียนขอมูล 2 ครั้ง

3. ปดทายดวยการแทรกขอมูลลงใน node ที่ไมเต็ม และเขียนขอมูลอีก 1 ครั้ง

ดังนั้นจํ านวนการอาน/เขียน ในการแทรกขอมูล

a + bm

a bm +

(20)

Deletion from a B-Tree : Analysis

Worst case : กรณีที่มีการ combine จากใบขึ้นไปเรื่อยๆ จนถึงระดับที่ 2

แตเมื่อถึงระดับที่ 1 เกิดการยายคียขึ้นลงใน node ขางเคียง กับคียที่ราก (ใหตนไมมีความสูง h)

จํ านวนการอาน/เขียนทั้งหมดที่มากสุด = 2(h+1)+h+1 = 3(h+1)

... . . .

...

.. ...

.. ...

..

t-1

t-1

t-1 t-1

t-1

t 1

...

R&W

R&W R&W

R&W

R&W R

R

Referensi

Dokumen terkait

Free plants and trees wallpapers cost nothing and help us fight the blues. Why not use them to live a more

• To write programs in C to solve problems using data structures such as arrays, linked lists, stacks, queues, trees, graphs, hash tables, search trees..

You could continuously check out the web link that we provide and also all set to download Trees Volume 1 (Trees Tp) By Warren Ellis When many people are hectic to look for fro in

Traversal tree inorder, postorder dan preorder non rekursif d.. Membangun Binary Search Tree

Double Linked List 10.Linked List Collection dan Circular Linked List 11.Stack 12.Queues dan Priority Queues 13.Binary Tree dan Algoritma Traversal Tree 14.Binary Search Trees

An optimal binary search tree is a BST, which has minimal expected cost of locating each node Search time of an element in a BST is On, whereas in a Balanced-BST search time is Olog

Outcome Measure Instrument/Source Secondary Outcome: Cost Effectiveness Quality of life Length of stay Cost effectiveness DemQol ˆ [25,26] Health Roundtable [24] Secondary Outcome:

Various Trees 2 Binary Search Tree: A binary tree in which the left subtree of a node contains only values less than or equal to the node’s value the right subtree of a node