Download MSc Computer Science ICS 801 Design and Analysis of Algorithms

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Red–black tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
7/6/2012
Randomized Binary Search Trees
 All the basic operations in a binary search tree run in
O(h) time
MSc Computer Science
 Height of the tree varies on how the items are inserted
and deleted
ICS 801
 If the items are inserted in a strictly increasing order,
the tree will be a chain with height n-1
Design and Analysis of Algorithms
 If we assume that the n keys are inserted in the tree in
random order such that any of the n! permutations are
equally likely
 The expected height of the tree is O(log n)
data Structures
2
Randomized Binary Search Trees
Randomized Binary Search Trees
 Inserting at the root requires use of two operations
 The randomized algorithm is based on the probability of the
1. Rotate left
value being inserted being the root of the tree
 Used to exchange a node with its right child hence the
right child goes one level up
 This can be given by 1/(n+1) where n is the number of nodes
currently in the tree
2. Rotate right
 This means that we will make the node we are currently
 Used to exchange a node with its left child hence the left
child goes one level up
inserting into the tree with probability 1/(n+1)
 The diagrams in the next slide show examples of rotating
left and rotating right
 If we cannot make it the root of the tree, then we will make it the
root of either the left subtree or the right subtree
data Structures
3
Randomized Binary Search Trees
data Structures
4
Randomized Binary Search Tree Algorithm
Node insert(Node h, Value val)
{
if (h == null)
return new Node(val)
if (Math.random()*(h.N + 1) <1)
return rootInsert(h, val)
if (val< h.val)
h.l = insert(h.l, val)
else
h.r = insert(h.r, val)
h.N++;
return h;
}
data Structures
5
data Structures
6
1
7/6/2012
Randomized Binary Search Tree Algorithm
Randomized Binary Search Tree Algorithm
Node rotL(Node h) {
Node x = h.r;
h.r = x.l;
x.l = h;
return x;
}
Node rotR(Node h) {
Node x = h.l;
h.l = x.r;
x.r = h;
return x;
}
Node rootInsert(Node h, Value val)
{
if (h == null)
return new Node(val);
if (val< h.val)) {
h.l = rootInsert(h.l, val);
h = rotR(h);
}
else {
h.r = rootInsert(h.r, val);
h = rotL(h);
}
return h;
}
data Structures
data Structures
7
Example
8
Red-black tree
 A type of self balancing binary search tree
 Given the following tree show how to insert the value
of 10 at the root node
 Typically used to implement associative arrays
 It can search, insert and delete in guaranteed O(log n) time
where n is the number of elements in the tree
 It has one extra bit of storage per node,
 The extra bit is used to store color information
 The color is either red or black
 By constraining the way the nodes can be colored in any path from
the root to the leaf,
 The red-black trees ensure that no path is more than twice as
long as any other
 So the tree is approximately balanced
data Structures
9
Red-black tree
10
Red-black tree Example

If a node has a null child, the null child is considered to be a leaf node
and its color is black

A red-black tree thus satisfies the following properties
1.
data Structures
Every node is either red or black
2. The root is black
3. Every leaf (null) is black
4. If a node is red, then both its children are black
5. For each node, all paths from the node to descendant leaves
contain the same number of black nodes

A red-black tree with n internal nodes has height at most
2 lg(n+1)

The subtree rooted at any node x contains at least 2bh(x)-1 internal
nodes. Where bh(x) is the black height of node x without counting x
data Structures
11
data Structures
12
2
7/6/2012
Red-black tree insertion
Red-black tree insertion
 The following operations are needed to perform an insertion into a
red-black tree
 In insertion, a node is inserted just in the same way it
could be inserted in a normal search tree
1. Left rotation
 After it is inserted, it is colored red
A node becomes the left child of its right child
2. Right rotation
 There is a possibility that the parent of the inserted node
A node becomes the right child of its left child
is colored red
3. Uncle
 This then leads to a violation of property 4
Get the uncle of a given node
4. Grandparent
 Adjustments have to be carried out in the tree to ensure
Get the grandparent of a given node
that it satisfies the violated property
data Structures
 The operations performed on the tree to return the tree to the
required state are described below. Assume the inserted node is z
13
Red-black tree insertion
14
Red-black tree insertion
1. If the parent of the inserted node (z=14) is black,
2. If the parent of z (27) is red,
 There is no violation, end
data Structures
data Structures
 There is a violation of property 4
15
Red-black tree insertion
data Structures
16
Red-black tree insertion
2.1 The uncle of z is red
Recolor the parent (28 and 30 to black and the grandparent (29)
to red as shown below. 29 becomes the new z
data Structures
17
data Structures
18
3
7/6/2012
Red-black tree insertion
Red-black tree insertion
2.2 The uncle of z is black,
 The black height of the paths from 30 does not change because
2.2.1 z (27) is the right child of its parent, and z’s parent is the left
child
the number of black nodes along those paths has not increased.
The uncle turned black and the grand parent red, so the number
of black nodes in those paths has remained the same
 The number of black nodes in the paths via 28 have not changed
either,
One extra black node was added by turning 28 black
An extra black node removed by making 29 (grandparent red)
data Structures
19
Red-black tree insertion
2.2.1
data Structures
20
Red-black tree insertion
left rotate the parent of z, 26 becomes the new z
 The right children of 27 have one less node above them.
This node was red. So the number of black nodes along those
paths has not changed
 The left children of 27 become the right children of 26.
The have one extra red node above them
So the number of black nodes along those paths has not
changed
 The left children of 26 have one extra red node above them
So the number of black nodes along those paths has not
changed
data Structures
21
Red-black tree insertion
22
Red-black tree insertion
2.2.2
z (26) is the left child of its parent which is the
left child of its grandparent
data Structures
data Structures
2.2.2 Right rotate the grand parent (29) and color 29 red
and 27 black
23
data Structures
24
4
7/6/2012
Red-black tree insertion
Red-black tree insertion
 The right children of 29 have an extra node above them (27)
 In case z is the root, just color it black
This node is black, but since 29 witch originally was black
becomes red, the number of black nodes in the paths remain the
same
All paths in the tree get one extra black node.
So if they originally had an equal number of black nodes, they
still have an equal number of black nodes
 The right children of 27 become the left children of 29. the
number of black nodes above them has not changed
Exercise
 The left children of 27 have one less node above them (29) which
was black.
How do you delete an item from a red-black tree?
But since 27 which was red becomes black, the number of black
nodes remain the same
data Structures
25
data Structures
26
5