Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
8/28/16
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
8/28/16
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
8/28/16
8/28/16
8/28/16
8/28/16
8/28/16
10
11
8/28/16
12
8/28/16
13
8/28/16
14
8/28/16
15
8/28/16
16
8/28/16
17
8/28/16
18
8/28/16
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
20
8/28/16
21
8/28/16
22
Insert node 5
8/28/16
23
8/28/16
Insert node 7
24
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.
25
8/28/16
26
Example 2
8/28/16
27
8/28/16
28
8/28/16
29
8/28/16
30
8/28/16
31
8/28/16
32
8/28/16
33
8/28/16
34
8/28/16
35
8/28/16
36
8/28/16
37
8/28/16
38
Example 3: Insert 2, 1, 4, 5, 9, 3, 6, 7
8/28/16
39
8/28/16
40
8/28/16
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.
8/28/16
42
Rules
43
8/28/16
44
8/28/16
45
8/28/16
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
47
8/28/16
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
49
Disadvantages
The disadvantage is that the search operation is a bit slower as
compared to an AVL tree.
8/28/16
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
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
52
8/28/16
53
8/28/16
54
8/28/16
55
8/28/16
56
8/28/16
57
8/28/16
58
59
60
Height of B-Trees
8/28/16
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
62
8/28/16
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
64
Insertion in B-Tree
Insert 3
*5*
*3*5*
8/28/16
65
Insert 21
* 3 * 5 * 21 *
Insert 9
*9*
c
*3*5*
* 21 *
8/28/16
66
Insert 1 and 13
*9*
b
c
*1*3*5*
* 13 * 21 *
8/28/16
67
Insert 2
*3*9*
b
d
*1*2*
c
*5*
* 13 * 21 *
8/28/16
68
Insert 7 and 10
*3*9*
b
d
*1*2*
a
c
*5*7*
* 10 * 13 * 21 *
8/28/16
69
Insert 12
* 3 * 9 * 13 *
b
*1*2*
d
*5*7*
c
* 10 * 12 *
e
* 21 *
8/28/16
70
Insert 4
* 3 * 9 * 13 *
b
*1*2*
d
*4*5*7*
c
* 10 * 12 *
e
* 21 *
8/28/16
71
Insert 8
*9*
*3*7*
* 13 *
*1*2*
*4*5*
8/28/16
h
*8*
c
* 10 * 12 *
e
* 21 *
72
8/28/16
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
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
75
Let us now insert 70 and 80. These new keys will be inserted into the
appropriate leaf without any split
8/28/16
76
Let
The
8/28/16
us now
middle
insert
key
will
cause
to
a
the
split.
parent.
77
8/28/16
78
8/28/16
79
8/28/16
80
8/28/16
81
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
82
8/28/16
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
84
85
8/28/16
86
8/28/16
87
6 deleted
8/28/16
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
89
8/28/16
90
13 deleted
8/28/16
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
92
8/28/16
93
7 deleted
8/28/16
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
95
8/28/16
96
2 deleted
8/28/16
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
98
8/28/16
99
4 deleted
8/28/16
100
8/28/16
101
8/28/16
102
8/28/16
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
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
*
* 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
*
* 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 *
*
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
*
* 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
*
* 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*
e
* 12 * 13 *
110
8/28/16
111
8/28/16
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
113
8/28/16
114
Steps involved in splitting a node X (Assumption, the parent of the node X is not full)
8/28/16
115
8/28/16
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
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
118
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
119
8/28/16
120
Answer: (C)
Explanation:
Both recoloring and rotation operations are used during insertion
and deletion.
8/28/16
121