binary search tree

MA 513: Data Structures
Lecture Note
http://www.iitg.ernet.in/psm/indexing_ma513/y09/index.html
Partha Sarathi Mandal
[email protected]
Dept. of Mathematics, IIT Guwahati
lTue
9:00-9:55 Wed 10:00-10:55 Thu 11:00-11:55 Class Room : 1G2
lMA514 Data Structure Lab : Tue 14:00-16:55
2-3 Tree : Remove
• With insertion, we split nodes. With removing,
we merge nodes.
• Deletion process needs to begin with a leaf
but you might be deleting a value that is not a
leaf.
– Swap item to delete with inorder successor
Remove - Redistribute
Remove - Redistribute
r
p
r
lx
q
p
ly
lz
P is the left child of r
y
q
lx
lz
Remove - Redistribute
r
q
p
x
r
z
y
q
p
x
P is the middle child of r
y
lz
Remove - Redistribute
r
w
r
z
q
p
x
y
w
q
p
x
P is the right child of r
y
lz
Remove - Redistribute
Remove - Merge
Remove - Merge
P is the left child of r
r
p
r
lx
q
p
ly
lx
ly
Remove - Merge
P is the left child of r
r
p
lx
q
r
lz
p
ly
lx
ly
z
Remove - Merge
Remove - Redistribute
Remove - Redistribute
Remove - Merge
Remove - Merge
Deleting the Root
2-3 Tree
• Start with this tree
Remove 65
• 65 is an internal node – swap with inorder
successor
• Inorder successor will always be in leaf.
Remove 65
• 65 is now in an invalid location but that is okay
because we will remove it
Remove 65
• Since there are 2 data values in the leaf, just
remove data value
Remove 70
• 70 is an internal node – swap with inorder
successor
– Inorder successor will always be in a leaf
Remove 70
• 70 is now in an invalid location but that is okay
we will be removing that node
Remove 70
• Removing leaf leaves us with an invalid 2-3
tree
Remove 70
• Merge nodes to fix tree
Remove 100
• 100 is already leaf, just remove leaf
Remove 100
• Sibling has data item to spare, redistribute
Remove 80
• Swap 80 with inorder successor
Remove 80
• Can’t redistribute so merge nodes
Remove 80
• Can’t redistribute so merge nodes
• Invalid 2-3 tree, continue recursively up the
tree
Remove 80
• Can’t redistribute so merge nodes
Remove 80
• Can’t redistribute so merge nodes
Remove 80
• Root is now empty, set new root pointer
Delete
deleteItem (itemtype item)
node = node where item exists (may be null if no item)
if (node)
if (item is not in a leaf)
swap item with inorder successor (always leaf)
leafNode = new location of item to delete
else
leafNode = node
delete item from leafNode
if (leafNode now contains no items)
fix (leafNode)
Delete
// completes the deletion when node n is empty by either
// removing the root, redistributing values, or merging
nodes.
// Note: if n is internal, it has only one child
fix (Node* n, …) //may need more parameters {
if (n is the root) {
remove the root
set new root pointer
}
else
Let p be the parent of n
Delete
if ( some sibling of n has two items ) {
Distribute items appropriately among n, the
sibling and the parent (remember take from
right first)
if ( n is internal ) {
Move the appropriate child from
sibling n (May have to move many children
if distributing across multiple siblings)
}
}
Delete
else { //merge nodes
Choose an adjacent sibling s of n (remember,
merge left first)
Bring the appropriate item down from p into s
if ( n is internal )
move n’s child to s
Remove node n
if (p is now empty)
fix (p)
} //end if
}//end if
B-Tree
• A B-tree is a rooted tree with an associated integer t called its minimum
degree.
• Every node contains at least t-1 and at most 2t-1 keys, e.g. if t = 2,
between 1 and 3 keys. (Exception - root.)
– So every internal node (non-leaf) has at least t children and at most 2t
children.
• Keys are placed in nodes in a way that satisfies the search tree property —
– if node x has keys key1[x], key2[x], ..., keym[x], then key1[x] < key2[x] < ...<
keym[x], and
– if k1, k2, ..., km+1 are keys in the subtrees of x, then k1 < key1[x], ..., keym[x] <
km+1.
• All leaves are on the same level.
• The root may have 0, 1, ..., 2t-1 keys
• If t = 2, internal nodes may have 2, 3, or 4 children. This is called a 2-3-4
tree.
Starting with the empty B-tree
• We insert a key by searching for an existing leaf into which
it should go. If the leaf is already full, we split it, creating a
new leaf in the process.
• So starting with the empty B-tree:
–
–
–
–
–
–
–
–
Create an empty root node.
Insert keys into root until it is full — has 2t - 1 keys.
When a node is full and there is a new key to be inserted,
split the node around the middle key (median)
put the middle key into the parent
leave the smallest t - 1 keys in existing node
put the biggest t - 1 keys in a new sibling node
if the full node was the root, we create a new root containing
the median, having 2 children.