Sei sulla pagina 1di 121

Unit III

Red-Black Tree and B-Tree

8/28/16

Department of Computer science


and engineering, Vignan University

Introduction
A Red-Black tree is a binary search tree with an extra bit of data per
node, its color which can be either red or black.
These color bits are used to ensure the tree remains approximately
balanced during insertions and deletions.
It is a self balancing binary search tree.
It was invented in the year 1972 by Rudolf
Bayer.

8/28/16

Department of Computer science


and engineering, Vignan University

A red/black tree is a binary search tree in which each node is colored


either red or black. At the interface, we maintain three invariants:
1. Ordering Invariant This is the same as for binary search trees:
all the keys to the left of a node are smaller, and all the keys to the
right of a node are larger than the key at the node itself.
2. Height Invariant The number of black nodes on every path from
the root to each leaf is the same. We call this the black height of the
tree.
3. Color Invariant No two consecutive nodes are red.

8/28/16

Department of Computer science


and engineering, Vignan University

The balance and color invariants together imply that the longest path
from the root to a leaf is at most twice as long as the shortest path.
Since insert and search in a binary search tree have time
proportional to the length of the path from the root to the leaf, this
guarantees O(log(n)) times for these operations, even if the tree is
not perfectly balanced.
We therefore refer to the height and color invariants collectively as
the balance invariant.

8/28/16

Department of Computer science


and engineering, Vignan University

Properties of Red-Black Tree


The following must be satisfied by a redblack tree
A node is either red or black.
The root is black. This rule is sometimes omitted. Since the root
can always be changed from red to black, but not necessarily
vice versa, this rule has little effect on the analysis.
All leaves (NIL) are black.
If a node is red, then both its children are black.
Every path from a given node to any of its descendant NIL nodes
contains the same number of black nodes.
Notes: the number of black nodes from the root to a node is the
node's black depth; the uniform number of black nodes in all paths
from the root to the leaves is called the black-height of the red
black tree.
8/28/16

Department of Computer science


and engineering, Vignan University

Example of Red-Black tree

8/28/16

Department of Computer science


and engineering, Vignan University

A basic Red-Black tree

A basic Red-Black tree with sentinel


nodes added

A simple example to understand balancing is, a chain of 3 nodes is not


possible in a red black tree. We can try any combination of colors and
see
all
of
them
violate
Red-Black
tree
property.

8/28/16

Department of Computer science


and engineering, Vignan University

Rotations of Red-Black Tree


A rotation is a local operation in a search tree that preserves in-order
traversal key ordering.
We change the pointer structure trough rotation, which is a local
operation in a search tree that preserves the binary-search-tree property.
The figure illustrated below shows the two kinds of rotations: left
rotations and right rotations.
When we do a left rotation on a node x, we assume that its right child y
is not NULL; x may be any node in the tree whose right child is not
NULL.
The left rotation pivots around the link from x to y.
It makes y the new root of the subtree, with x as ys left child and ys
left child as xs right child.

8/28/16

Department of Computer science


and engineering, Vignan University

Rotations in a Red-Black tree

8/28/16

Department of Computer science


and engineering, Vignan University

In AVL tree insertion, we used rotation as a tool to do balancing after


insertion caused the imbalance.
In Red-Black tree, we use two tools to do balancing.
Recoloring
Rotation

We try recoloring first, if recoloring doesnt work, then we go in


rotation.
Following is detailed algorithm.
The algorithm has mainly two cases, depending upon the color of
uncle. If uncle is red, we do require. If uncle is black, we do rotations
and/or recoloring.

8/28/16

Department of Computer science


and engineering, Vignan University

10

The color of a NULL node is considered as BLACK.


Let
x
be
the
newly
inserted
node.
1) Perform standard BST insertion and make the color of newly
inserted nodes as RED.
2) If x is root, change color of x as BLACK (Black height of complete
tree increases by 1).
3) Do the following if the color of the xs parent is not BLACK or x is
not
root.
a) If xs uncle is RED (Grand parent must have been black
from property.
(i) Change
color
of
parent
and
uncle
as
BLACK.
(ii) Color
of
a
grandparent
as
RED.
(iii) Change x = xs grandparent, repeat steps 2 and 3 for new x.
8/28/16

Department of Computer science


and engineering, Vignan University

11

b) If xs uncle is BLACK, then there can be four configurations for x,


xs parent (p) and xs grandparent (g) (This is similar to AVL Tree)
i) Left Left Case (p is left child of g and x is left child of
p) (LL).
ii) Left Right Case (p is left child of g and x is the right
child of p) (LR)
iii) Right Right Case (Mirror of case a) (RR)
iv) Right Left Case (Mirror of case c) (RL)

8/28/16

Department of Computer science


and engineering, Vignan University

12

New node Xs uncle is Red

8/28/16

Department of Computer science


and engineering, Vignan University

13

All four cases when Uncle is Black


Case I: Left of Left Rotation

8/28/16

Department of Computer science


and engineering, Vignan University

14

Case II: Left of Right Rotation

8/28/16

Department of Computer science


and engineering, Vignan University

15

Case III: Right of Right Rotation

8/28/16

Department of Computer science


and engineering, Vignan University

16

Case IV: Right of Left Rotation

8/28/16

Department of Computer science


and engineering, Vignan University

17

Red-Black tree properties

8/28/16

Department of Computer science


and engineering, Vignan University

18

Insertions of Red-Black Tree


Idea: Insert x in tree. Color x as red. Only red-black property 3
might be violated. Move the violation up the tree by recoloring until
it can be fixed with rotations and recoloring.
Example: 1 2 3 4 5 6 7 8

8/28/16

Department of Computer science


and engineering, Vignan University

19

Insert node 1
After insert 1. A leaf node so
color it as red. Realize it is rooted
so recolor to black.

8/28/16

Department of Computer science


and engineering, Vignan University

20

Insert node 2: Since node 2 is a new


node so color the node 2 as red. The parent
is black so done. It is balanced and no need
recoloring. It is a Red-Black tree.

8/28/16

Insert node 3: After insert the node 3:


Parent is red. Parent's sibling is black
(null) 3 is outside relative to the
grandparent node 1. So rotate parent
and grandparent by using RR rotation.

Department of Computer science


and engineering, Vignan University

21

Now it is a Red-Black tree

8/28/16

Insert node 4 : On the way above to see


node 2 with 2 red children. Recolor 2
as red and children black. Realize 2 is
rooted so color back to black. When
adding 4 parent is black so done.

Department of Computer science


and engineering, Vignan University

22

Insert node 5

8/28/16

5's parent is red. Parent's sibling is black (null).


The node 5 is outside relative to grandparent (3). So
rotate parent and grandparent of node 5 by using
RR rotation. Now it is a Red-Black tree

Department of Computer science


and engineering, Vignan University

23

Insert node 6 : 6's parent is black so


done.

8/28/16

Insert node 7

Department of Computer science


and engineering, Vignan University

24

7's parent is red. Parent's sibling is black


(null). The node 7 is outside relative to
grandparent (5). So rotate parent and
grandparent of node 7 by using RR rotation
then recolor it. Now it is a Red-Black tree.

8/28/16

Insert node 8
On way down see 6 with 2 red
children. Make 6 red and children black.
This creates a problem because 6's
parent, 4, is also red. Before going to
insert 8, Recolor the tree. Now go for
rotation.

Department of Computer science


and engineering, Vignan University

25

8/28/16

Department of Computer science


and engineering, Vignan University

26

Example 2

8/28/16

Department of Computer science


and engineering, Vignan University

27

Case 1 LL: new node is 120

8/28/16

Department of Computer science


and engineering, Vignan University

28

140>130>120 middle is 130. Do LL rotation.

8/28/16

Department of Computer science


and engineering, Vignan University

29

Case II RR: new node is 145

8/28/16

Department of Computer science


and engineering, Vignan University

30

140<143<145 middle is 143. Do RR rotation.

8/28/16

Department of Computer science


and engineering, Vignan University

31

Both LL and RR are common is one thing.


The parents value is between its child and its parents value and
push your parent up.
So that its child remains as a child and its parent becomes its child,
finally invert parent and its parents color.

8/28/16

Department of Computer science


and engineering, Vignan University

32

Case III LR: new node is 135

8/28/16

Department of Computer science


and engineering, Vignan University

33

First do Left rotation

8/28/16

Department of Computer science


and engineering, Vignan University

34

Second Right Rotation

8/28/16

Department of Computer science


and engineering, Vignan University

35

Case IV RL: new node is 142

8/28/16

Department of Computer science


and engineering, Vignan University

36

First do Right Rotation.

8/28/16

Department of Computer science


and engineering, Vignan University

37

Second do Left Rotation

8/28/16

Department of Computer science


and engineering, Vignan University

38

Example 3: Insert 2, 1, 4, 5, 9, 3, 6, 7

8/28/16

Department of Computer science


and engineering, Vignan University

39

Red-Black tree insertion

8/28/16

Department of Computer science


and engineering, Vignan University

40

Deletions from the Red-Black Tree


Recall the rules for bottom-up deletion in the ordinary binary search
tree;
1. If the node to be deleted is a leaf just delete it.
2. If the node to be deleted has just one child, replace it with that
the child.
3. If the node to be deleted has two children, replace the value of
the node with the value at its inorder predecessor, then recursively
delete the inorder predecessor.

8/28/16

Department of Computer science


and engineering, Vignan University

41

To do the bottom-up deletion in a red-black tree, first perform the ordinary bottom-up deletion
following the rules above. Eventually one of the two base cases will apply (delete a leaf or
delete a node with one just one child). Let u be the node to be deleted at that point.
1. If u is a leaf, we think of deletion as replaced by the null pointer V. Recall that in redblack trees, the null pointers are black.
2. If u has just one child, V, we think deletion as replacement of u by V.
If we delete a node, then the color of the node removed to be notified.

If u is red, then deleting it does not change any red-black


property and no adjustments are necessary. We havent
changed any black heights. Nor will we have created 2 red
nodes in a row. And also, it could not have been the root. If
we delete a red node, the tree is still a red-black tree. A red
node is either a leaf node or must have two children.
If u is black (and it is not the root) deleting it will change the
black-length along some path. The blackness of u that is lost
can be thought of as giving extra blackness to V. We must
adjust the tree to move this extra blackness up the tree and
eventually having it absorbed somewhere. This is just an
accounting trick, but it makes the adjustments more
understandable. It could violate any of root rule, red rule, or
black-height rule.

8/28/16

Department of Computer science


and engineering, Vignan University

42

Rules

1. If the node to be deleted is a red leaf, remove leaves, done.


2. If it is a single-child parent, it must be black. Replace with its
child (must be red) and recolor child black.
3. If it has two internal node children, swap node to be deleted
with its in-order successor.
If in-order successor is red (must be a leaf), remove leaves, done.
If in-order successor is a single childs parent, apply second rule.
In both cases the resulting tree is a legit red-black tree" (we
havent changed the number of black nodes in paths).
4. If in-order successor is a black leaf, or if the node to be deleted
is itself a black leaf, things get complicated.
8/28/16

Department of Computer science


and engineering, Vignan University

43

Delete node 9 has no children.

8/28/16

Department of Computer science


and engineering, Vignan University

44

After deleting number 9 the node with key 8


remains with two right side black children that
belonged to number 9. (Dont forget every leaf is
black, but those leaves are NULL and are not
represented in these drawings. Those leaf nodes
are the sentinels, in our case NULL nodes with
black color values.)

8/28/16

In order to fix this violation we recolor the tree as


follows:
In the next step we delete node number 8. By
deleting number 8 no double black node emerges as
in the previous example. So the structure of the tree
doesnt suffer modification.

Department of Computer science


and engineering, Vignan University

45

In the next step we delete node number 7. By deleting node


number 7 we end up with a tree looking like this:

8/28/16

The tree obtained is unbalanced therefore we need


no restructure it, and do so by a right rotation of
node number 4. Also some re-coloring is needed
after that. The final tree looks like this:

Department of Computer science


and engineering, Vignan University

46

The AVL trees are more balanced compared to Red-Black Trees, but
they may cause more rotations during insertion and deletion.
So if your application involves many frequent insertions and
deletions, then Red-Black trees should be preferred.
And if the insertions and deletions are less frequent and search is a
more frequent operation, then an AVL tree should be preferred over
Red-Black Tree.

8/28/16

Department of Computer science


and engineering, Vignan University

47

Red-Black Tree Animation

8/28/16

Department of Computer science


and engineering, Vignan University

48

Advantages
The main advantage of Red-Black trees over AVL trees is that a
single top-down pass may be used in both insertion and deletion
routines.
Quick search, insertion, deletion always balanced.

8/28/16

Department of Computer science


and engineering, Vignan University

49

Disadvantages
The disadvantage is that the search operation is a bit slower as
compared to an AVL tree.

8/28/16

Department of Computer science


and engineering, Vignan University

50

B-Tree
In 1970, two computer scientists working for the Boeing Company
in Seattle created a mew tree structure they called the B-tree.
It is also an extension of Binary Search Tree.

8/28/16

Department of Computer science


and engineering, Vignan University

51

Properties
B-tree of order m (m means maximum of m childrens it can have like m = 4, 5)
satisfies following properties:
1. Each node has at most m childrens.
2. Each internal node has at least [m/2] childrens.
3. Root has at least 2 children if it is not leaf.
4. A Non leaf node with K children has (K-1) keys.
5. All leaves appear in the same level.
6. Every path from the root to a leaf has the same length.
7. The elements stored in a given subtree all have keys that are between the keys in the
parent node on either side of the subtree pointer.
8. All keys of a node are sorted in increasing order. The child between two keys k1 and
k2 contains all keys in range from k1 and k2.
9. for any non-leaf node:
i) An element at index i is greater than all the elements in subtree number i of the node,
and
ii) An element at index i is less than all the elements in subtree number i + 1 of the node.

8/28/16

Department of Computer science


and engineering, Vignan University

52

Example structure of B-Tree

8/28/16

Department of Computer science


and engineering, Vignan University

53

Following is an example B-Tree of minimum degree 3

8/28/16

Department of Computer science


and engineering, Vignan University

54

From the definition of B-Tree, it should be apparent that a B-tree is a


perfectly balanced m-way tree in which each node, with the possible
exception of the root, is at least half full.
Table 3-1 defines the minimum and maximum numbers of subtrees
in a non root node for B-trees of different orders.

8/28/16

Department of Computer science


and engineering, Vignan University

55

Entries in B-trees of various orders

8/28/16

Department of Computer science


and engineering, Vignan University

56

Unlike a binary-tree, each node of a b-tree may have a variable


number of keys and children.
The keys are stored in non-decreasing order.
Each key has an associated child that is the root of a subtree
containing all nodes with keys less than or equal to the key but
greater than the preceding key.
A node also has an additional rightmost child that is the root for a
subtree containing all keys greater than any keys in the node.

8/28/16

Department of Computer science


and engineering, Vignan University

57

A b-tree has a minimum number of allowable children for each node


known as the minimization factor.
If t is this minimization factor, every node must have at least t 1 keys. Under certain circumstances, the root node is allowed to
violate this property by having fewer than t - 1 keys.
Every
node may
have at most 2t - 1 keys or,
equivalently, 2t children.

8/28/16

Department of Computer science


and engineering, Vignan University

58

Since each node tends to have a large branching factor (a large


number of children), it is typically necessary to traverse relatively
few nodes before locating the desired key.
If access to each node requires a disk access, then a b-tree will
minimize the number of disk accesses required.
The minimization factor is usually chosen so that the total size of
each node corresponds to a multiple of the block size of the
underlying storage device.
This choice simplifies and optimizes disk access. Consequently, a
b-tree is an ideal data structure for situations where all data
cannot reside in primary storage and accesses to secondary
storage are comparatively expensive (or time consuming).
8/28/16

Department of Computer science


and engineering, Vignan University

59

Trees can be used to store entire records from a database, serving as


an in-memory representation of the collection of records in a file.

Trees can also be used to store indices of the collection of


records in a file.
In either case, if the collection of records is quite large, the tree may
be so large that it is unacceptable to store it all in memory at once.

For example, if we have a database file holding 230 records,


and each index entry requires 8 bytes of storage, a BST holding the
index would require 230 nodes, each taking 16 bytes of memory
(assuming 32-bit pointers), or 16 GB of memory.

An alternative would be to store the entire tree in a file on disk,


and only load the immediately relevant portions of it into memory.
8/28/16

Department of Computer science


and engineering, Vignan University

60

Height of B-Trees

For n greater than or equal to one, the height of an n-key b-tree T of


height h with a minimum degree t greater than or equal to 2.
h logt [n+1/2]

8/28/16

Department of Computer science


and engineering, Vignan University

61

Searching in B-Tree
Search is similar to search in Binary Search Tree. Let the key to be searched be
k.
We start from root and recursively traverse down.
For every visited non-leaf node, if the node has key, we simply return the node.
Otherwise we recur down to the appropriate child (The child which is just
before the first greater key) of the node.
If we reach a leaf node and dont find k in the leaf node, we return NULL.

Searching a B-tree is much like searching a binary search tree, except that
instead of making a binary, or "two-way," branching decision at each node, we
make a multiway branching decision according to the number of the node's
children.
More precisely, at each internal node x, we make an (n[x] + 1)-way branching
decision.

8/28/16

Department of Computer science


and engineering, Vignan University

62

Searching a B-Tree for Key 21

8/28/16

Department of Computer science


and engineering, Vignan University

63

First compare the key value with the node 10 then it is smaller than the key
value.
So go to the middle node in the root value 20 and compare it with key
value 21.
Then it is also smaller than the key value 21.
Again search the key value 21 with the third node 30 then it is larger than
the key value.
So go to right subtree of node 20 and left subtree of node 30. Now
compare the key value 21 with the node 21.
It matches and returns the value.

8/28/16

Department of Computer science


and engineering, Vignan University

64

Insertion in B-Tree

For example insert: 5, 3, 21, 9, 1, 13, 2, 7, 10, 12, 4, and 8. Where t = 2 so


2*t-1= 2*2-1 = 3.
Insert 5

Insert 3

*5*

*3*5*

8/28/16

Department of Computer science


and engineering, Vignan University

65

Insert 21

* 3 * 5 * 21 *

Insert 9

*9*

c
*3*5*

* 21 *

Node a splits creating 2 children: b and c

8/28/16

Department of Computer science


and engineering, Vignan University

66

Insert 1 and 13

*9*
b

c
*1*3*5*

* 13 * 21 *

Nodes b and c have room to insert more elements.

8/28/16

Department of Computer science


and engineering, Vignan University

67

Insert 2
*3*9*
b

d
*1*2*

c
*5*

* 13 * 21 *

Node b has no more room, so it splits creating node d.

8/28/16

Department of Computer science


and engineering, Vignan University

68

Insert 7 and 10

*3*9*
b

d
*1*2*

a
c

*5*7*

* 10 * 13 * 21 *

Nodes d and c have room to add more elements

8/28/16

Department of Computer science


and engineering, Vignan University

69

Insert 12

* 3 * 9 * 13 *
b
*1*2*

d
*5*7*

c
* 10 * 12 *

e
* 21 *

Nodes c must split into nodes c and e

8/28/16

Department of Computer science


and engineering, Vignan University

70

Insert 4

* 3 * 9 * 13 *
b

*1*2*

d
*4*5*7*

c
* 10 * 12 *

e
* 21 *

Node d has room for another element.

8/28/16

Department of Computer science


and engineering, Vignan University

71

Insert 8

*9*

*3*7*

* 13 *

*1*2*

*4*5*

8/28/16

h
*8*

c
* 10 * 12 *

Department of Computer science


and engineering, Vignan University

e
* 21 *

72

Another example of insertion


Let us understand the algorithm with an example tree of minimum
degree t as 3 and a sequence of integers 10, 20, 30, 40, 50, 60, 70, 80 and
90 in an initially empty B-Tree.
Initially root is NULL. Let us first insert 10.

8/28/16

Department of Computer science


and engineering, Vignan University

73

Let us now insert 20, 30, 40 and 50. They all will be inserted in root because maximum
number of keys a node can accommodate is 2*t 1 which is 5.

8/28/16

Department of Computer science


and engineering, Vignan University

74

Let us now insert 60. Since root node is full, it will first split into two, then 60 will be
inserted
into
the
appropriate
child.

8/28/16

Department of Computer science


and engineering, Vignan University

75

Let us now insert 70 and 80. These new keys will be inserted into the
appropriate leaf without any split

8/28/16

Department of Computer science


and engineering, Vignan University

76

Let
The

8/28/16

us now
middle

insert
key

90. This insertion


will
go
up

Department of Computer science


and engineering, Vignan University

will

cause
to

a
the

split.
parent.

77

8/28/16

Department of Computer science


and engineering, Vignan University

78

Deletion from the B-Tree


Deletion from a B-tree is analogous to insertion but a little more
complicated.
There are two popular strategies for deletion from a B-tree.
1. Locate and delete the item, then restructure the tree to retain its
invariants, Or
2. Do a single pass down the tree, but before entering (visiting) a
node, restructure the tree so that once the key to be deleted is
encountered, it can be deleted without triggering the need for any
further restructuring.

8/28/16

Department of Computer science


and engineering, Vignan University

79

There are two special cases to consider when deleting an element:


1. The element in an internal node is a separator for its child nodes
2. Deleting an element may put its node under the minimum number
of elements and children.

8/28/16

Department of Computer science


and engineering, Vignan University

80

Deletion from a leaf node


1. Search for the value to delete.
2. If the value is in a leaf node, simply delete it from the node.
3. If underflow happens, rebalance the tree as described in section
"Rebalancing after deletion" below.

8/28/16

Department of Computer science


and engineering, Vignan University

81

Deletion from an internal node

Choose a new separator (either the largest element in the left subtree
or the smallest element in the right subtree), remove it from the leaf
node it is in, and replace the element to be deleted with the new
separator.
The previous step deleted an element (the new separator) from a leaf
node. If that leaf node is now deficient (has fewer than the required
number of nodes), then rebalance the tree starting from the leaf
node.

8/28/16

Department of Computer science


and engineering, Vignan University

82

Rebalancing after deletion

The algorithm to rebalance the tree is as follows:


If the deficient node's right sibling exists and has more than the
minimum number of elements, then rotate left
Copy the separator from the parent to the end of the deficient
node (the separator moves down; the deficient node now has the
minimum number of elements)
Replace the separator in the parent with the first element of the
right sibling (right sibling loses one node but still has at least the
minimum number of elements)
The tree is now balanced

8/28/16

Department of Computer science


and engineering, Vignan University

83

Otherwise, if the deficient node's left sibling exists and has more
than the minimum number of elements, then rotate right
Copy the separator from the parent to the start of the deficient
node (the separator moves down; deficient node now has the
minimum number of elements)
Replace the separator in the parent with the last element of the
left sibling (left sibling loses one node but still has at least the
minimum number of elements)
The tree is now balanced

8/28/16

Department of Computer science


and engineering, Vignan University

84

Otherwise, if both immediate siblings have only the minimum number of


elements, then merge with a sibling sandwiching their separator taken off
from their parent
Copy the separator to the end of the left node (the left node may be the
deficient node or it may be the sibling with the minimum number of
elements)
Move all elements from the right node to the left node (the left node now
has the maximum number of elements, and the right node empty)
Remove the separator from the parent along with its empty right child (the
parent loses an element)
If the parent is the root and now has no elements, then free it and make
the merged node the new root (tree becomes shallower)
Otherwise, if the parent has fewer than the required number of
elements, then rebalance the parent
8/28/16

Department of Computer science


and engineering, Vignan University

85

The following specification for deletion from a B-tree should be


interpreted with the understanding that if it ever happens that the
root node x becomes an internal node having no keys, then x is
deleted and x's only child c1[x] becomes the new root of the tree,
decreasing the height of the tree by one and preserving the property
that the root of the tree contains at least one key (unless the tree is
empty).
The various cases for deleting the keys from a B-tree as follows:
Case I: If the key k is in node x and x is a leaf, delete the
key k from x.

8/28/16

Department of Computer science


and engineering, Vignan University

86

8/28/16

Department of Computer science


and engineering, Vignan University

87

6 deleted

8/28/16

Department of Computer science


and engineering, Vignan University

88

Case-II
If key k is in node x and x is an internal node, there are three cases
to consider:
Case-II-a
If the child y that precedes k in node x has at least t keys (more than
the minimum), then find the predecessor key k' in the subtree rooted
at y. Recursively delete k' and replace k with k' in x
Case-II-b
Symmetrically, if the child z that follows k in node x has at least t
keys, find the successor k' and delete and replace as before. Note
that finding k' and deleting it can be performed in a single
downward pass.
8/28/16

Department of Computer science


and engineering, Vignan University

89

8/28/16

Department of Computer science


and engineering, Vignan University

90

13 deleted

8/28/16

Department of Computer science


and engineering, Vignan University

91

Case-II-c
Otherwise, if both y and z have only t1 (minimum number) keys, merge k
and all of z into y, so that both k and the pointer to z are removed from x. y
now contains 2t 1 keys, and subsequently k is deleted.

8/28/16

Department of Computer science


and engineering, Vignan University

92

8/28/16

Department of Computer science


and engineering, Vignan University

93

7 deleted

8/28/16

Department of Computer science


and engineering, Vignan University

94

Case-III
If key k is not present in an internal node x, determine the root of the
appropriate subtree that must contain k. If the root has only t 1 keys,
execute either of the following two cases to ensure that we descend to a
node containing at least t keys. Finally, recurse to the appropriate child of
x.
Case-III-a
If the root has only t1 keys but has a sibling with t keys, give the root an
extra key by moving a key from x to the root, moving a key from the roots
immediate left or right sibling up into x, and moving the appropriate child
from
the
sibling
to
x.

8/28/16

Department of Computer science


and engineering, Vignan University

95

8/28/16

Department of Computer science


and engineering, Vignan University

96

2 deleted

8/28/16

Department of Computer science


and engineering, Vignan University

97

Case-III-b
If the root and all of its siblings have t1 keys, merge the root with
one sibling. This involves moving a key down from x into the new
merged node to become the median key for that node.

8/28/16

Department of Computer science


and engineering, Vignan University

98

8/28/16

Department of Computer science


and engineering, Vignan University

99

4 deleted

8/28/16

Department of Computer science


and engineering, Vignan University

100

8/28/16

Department of Computer science


and engineering, Vignan University

101

Example of deleting the key from the B-tree

8/28/16

Department of Computer science


and engineering, Vignan University

102

8/28/16

Department of Computer science


and engineering, Vignan University

103

The minimum degree for this B-tree is t = 3, so a node (other than the
root) cannot have less than 2 keys. Nodes that are modified are
lightly shaded.
(a) It is the initial tree where deletion is going to be done.
(b) Deletion of F. This is case 1: simple deletion from a leaf.
(c) Deletion of M. This is case 2a: the predecessor L of M is moved
up to take M's position.
(d) Deletion of G. This is case 2c: G is pushed down to make node
DEGJK, and then G is deleted from this leaf (case 1).
(e) Deletion of D. This is case 3b: the recursion can't descend to node
CL because it has only 2 keys, so P is pushed down and merged with
CL and TX to form CLPTX; then, D is deleted from a leaf (case 1).
(e') After (d), the root is deleted and the tree shrinks in height by one.
(f) Deletion of B. This is case 3a: C is moved to fill B's position and
E is moved to fill C's position.
8/28/16

Department of Computer science


and engineering, Vignan University

104

Example 2
Delete: 2, 21, 10, 3, 4 from the below b-tree

*9
*

*3*7
*

* 13 *

*1*2
*

*4*5
*

8/28/16

*8
*

c
* 10 * 12
*

Department of Computer science


and engineering, Vignan University

* 21
*
105

Delete 2
Node b can lose an element without underflow

*9
*

*3*7
*
b
*1*
8/28/16

* 13 *

*4*5
*

*8
*

c
* 10 * 12
*

Department of Computer science


and engineering, Vignan University

* 21
*
106

Delete 21
Deleting 21, makes the node e to underflow, so elements are redistributed between nodes c, g, and e.

*9 a
*
g

f
*3*
7*
b
*1*
8/28/16

* 12 *

d
*4*
5*

*8
*

c
* 10 *

Department of Computer science


and engineering, Vignan University

*
13 *
107

Delete 10
Deleting 10, causes node c to underflow. This causes the parent, node
g to recombine with nodes f and a. This causes the tree to shrink one
level

b
*1*

8/28/16

d
*4*5
*

*3*7*9*

e
*8
*

Department of Computer science


and engineering, Vignan University

* 12 *
13 *

108

Delete 3
Because 3 is a pointer to nodes below it, deleting 3 requires keys to be redistributed between nodes a and d.

b
*1*

8/28/16

*4*7*9*

h
*5*

e
*8
*

Department of Computer science


and engineering, Vignan University

* 12 *
13 *

109

Delete 4
Deleting 4 requires a redistribution of the keys in the subtrees of 4; however, nodes b and d do not have enough keys to redistribute without causing an
underflow. Thus, nodes b and d must be combined

*7*9*

*1*5*

8/28/16

*8*

Department of Computer science


and engineering, Vignan University

e
* 12 * 13 *

110

A more concrete example for node deletion

8/28/16

Department of Computer science


and engineering, Vignan University

111

Splitting the B-Tree


-Find the middle value (of old keys in the node and the new key). Keep records with keys smaller than middle in the old node.
- Put
records with keys greater than middle in the new node.
- Push middle record up into the parent node.

8/28/16

Department of Computer science


and engineering, Vignan University

112

Consider the below B Tree (in figure 3-45) with minimum degree D = 3.
Now, let us try to insert a key F into this tree.
Clearly F must go into the leaf node which already has keys (A, B, C, D,
and E).
maximum number of keys a node can have for D=3 is 2D-1, which is equal
to 6-1 = 5.
As we can see that the node already has 5 keys, adding F into the node will
overflow and violate the tree property.
Hence, we have to fix this, and when a node overgrows, we split it.

8/28/16

Department of Computer science


and engineering, Vignan University

113

Example of splitting the B-tree

8/28/16

Department of Computer science


and engineering, Vignan University

114

Steps involved in splitting a node X (Assumption, the parent of the node X is not full)

Find the median of the full node. (Here it would be C)


Create a new leaf node and copy into it all the keys which appear after the
median.
Move up the median in an appropriate position in the parent of this node.
Add an additional child pointer (after the median) from the parent node to
the new node.
Add the new key at the right location in the child nodes of the median.

8/28/16

Department of Computer science


and engineering, Vignan University

115

Merging two nodes of a B Tree


Consider the B Tree in figure (3).
Let us try to delete an element G from the tree.

8/28/16

Department of Computer science


and engineering, Vignan University

116

As the minimum number of keys a node must have is D-1, which is 3-1 =
2. If we just delete G from the node, it would still satisfy the B Tree
property.
But the moment we delete G, it will also lose one of the child pointers,
hence we need to merge the two nodes containing (D,E) and (J,K).
The tree in figure (4) is representing the structure after merger. You
may notice that the merge was pretty straightforward in this case, as the
merged node didnt violate any of the tree properties.

8/28/16

Department of Computer science


and engineering, Vignan University

117

Steps involved in merging two nodes Y and Z with a common parent node X

Find the key K in node X which separates the two nodes Y and Z
Shift all the keys of Z to Y (maintain the existing order) and free Z
Move down the key K from node X to node Y

8/28/16

Department of Computer science


and engineering, Vignan University

118

Advantages of B-tree usage of databases

The B-tree uses all of the ideas described above. In particular, a B-tree:
Keeps keys in sorted order for sequential traversing
Uses a hierarchical index to minimize the number of disk reads
Uses partially full blocks to speed insertions and deletions
Keeps the index balanced with an elegant recursive algorithm
In addition, a B-tree minimizes waste by making sure the interior
nodes are at least half full. A B-tree can handle an arbitrary number of
insertions and deletions.

8/28/16

Department of Computer science


and engineering, Vignan University

119

Which of the following operations are used by Red-Black trees to


maintain balance during insertion/deletion? a) Recoloring of nodes
b) Rotation (Left and Right)
(A) Only a
(B) Only b
(C) Both a and b
(D) Neither a nor b

8/28/16

Department of Computer science


and engineering, Vignan University

120

Answer: (C)
Explanation:
Both recoloring and rotation operations are used during insertion
and deletion.

8/28/16

Department of Computer science


and engineering, Vignan University

121

Potrebbero piacerti anche