CpSc 360
Chapter 8 (continued)
A
B-Tree with order M is a tree with
the following properties:
1.
No
node has more than M children
2.
Every
node, except for the root and the terminal nodes, has at least éM/2ù children
3.
The
root, unless the tree only has one node, has at least two children
4.
All
terminal nodes appear on the same level, i.e., are the same distance from the
root
5.
A
nonterminal node with K children
contains K – 1 records. A terminal node contains at least éM/2ù - 1 records and at most M – 1 records
function
Search (ROOT_RRN, KEY, NODE_RRN, NODE_POS)
(*
In this algorithm
Found : a flag to indicate
if the record has been found
K : key of record being searched for
P : holds a pointer to a node
Ri : record i in
current node
*)
Found
= false
read
root at location ROOT_RRN
P =
ROOT_RRN
repeat
N = number of records in current
node
case
KEY = key of record in current node (set i): found
= true
KEY < key(R1) : P = P0
KEY > key(RN) : P = PN
Otherwise : P = Pi–1 (for some i where key(Ri–1)
< KEY < key(Ri))
if P not null then
read node pointed to by P
endif
NODE_POS = i
NODE_RRN = P
endif
Search
= Found
return
B-Tree
Insertion Algorithm (Non-recursive)
function
Insert(ROOT_RRN, In-rec)
(*
In this algorithm
In-rec : the record to be
inserted into the tree
Finished: a flag to indicate if insertion has finished
Found : a flag to indicate
if record has been found in tree
P : holds a pointer to a node
TOOBIG: an oversized node
N : record count
*)
(*
Search tree for In-rec forming stack of node addresses *)
Found
= false
Read
root at ROOT_RRN
P =
ROOT_RRN
Repeat
N = number of records in current
node
case
key(In-rec) = key of record in current node (set i): found
= true
key(In-rec) < key(R1) : P = P0
key(In-rec) > key(RN) : P = PN
Otherwise : P = Pi–1
(for some i where key(Ri–1) < KEY < key(Ri))
if P not null then
push onto stack address of
current node
read node pointed to by P
endif
Insert = false (* report record with key = Key(In-rec)
already in tree *)
else (* insert In-rec
into tree *)
P = nil
Finished = false
Repeat
if current node is not full
then
put In-rec and P in current node
Finished = true
else
copy current node to TOOBIG
insert In-rec and P into TOOBIG
in-rec = center record of TOOBIG
get space for new node, assign address to P
new node = 2nd half of TOOBIG
if stack not empty then
pop top of stack
read node pointed to
else (* tree
grows *)
get space for new node
new node = pointer to old root, In-rec and P
Finished = true
endif
endif
until Finished
insert = true
endif
return
(*
In this algorithm
TWOBNODE : an oversize node
that is about 50% larger than a normal node
A-sibling : an adjacent
sibling node
Out-rec : the
record to be deleted from the tree
*)
(*
Search tree for In-rec forming stack of node addresses *)
Found
= false
read
root at ROOT_RRN
repeat
N = number of records in current
node
case
key(Out-rec) = key of record in current node: found = true
key(Out-rec) < key(R1) : P = P0
key(Out-rec) > key(RN) : P = PN
Otherwise :
P = Pi–1 (for some i where key(Ri–1) < KEY < key(Ri))
if P not null then
push onto stack address of
current node
read node pointed to by P
endif
if
not Found then
delete = false
return
endif
Search for successor record of
Out-rec at terminal level (stacking node addresses)
Copy successor over Out-rec
Terminal node successor now becomes
the Out-rec
endif
(*
remove record and adjust tree *)
Finished
= false
repeat
Remove Out-rec (record Ri)
and pointer Pi
if current node is root or is not
too small then
Finished = true
else
if redistribution
possible then (* an A-sibling > minimum *)
(*
redistribute *)
copy “best” A-sibling, intermediate parent record, and current (too-small) node into TWOBNODE
copy records and pointers from TWOBNODE to “best” A-sibling, parent, and current node so A-sibling and current node are roughly equal size
Finished =
true
else (* concatenate with appropriate A-sibling
*)
Choose best
A-sibling to concatenate with
Put in the leftmost of the current node and A-sibling the contents of both nodes and the intermediate record from the parent
Discard
rightmost of the two nodes
Intermediate
record in parent now becomes Out-rec
until
Finished
if
no records in root then (* tree shrinks
*)
new root is the node pointed to by
the current root
discard old root
endif
delete
= true
return
A B-Tree
Access Method (class discussion)
Record length?
Page size?
Etc
What
objects should be used to characterize the access method?
How
do we define a pointer to an object when we don’t actually know the format of
the object?