CS6301 Programming and Data Structures II Unit 4 Page 1 CS6301 Programming and Data Structures II Unit -4 AVL TREE AVL tree (Adelson-Velskii and Landis' tree, named after the inventors) is a self-balancing binary search tree . It was the first such data structure to be invented. [1] In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. • AVL trees are height-balanced binary search trees • Balance factor of a node › height(left subtree) - height(right subtree) • An AVL tree has balance factor calculated at every node › For every node, heights of left and right subtree can differ by no more than 1 › Store current heights in each node Two types of rotation: x Single Rotation x Double Rotation Single Rotation x LL Rotation x RR Rotation Double Rotation x LR Rotation x RL Rotation www.studentsfocus.com www.studentsfocus.com
26
Embed
Unit -4 - StudentsFocusstudentsfocus.com/wp-content/uploads/anna_univ/CSE/3SEM/CS6301... · CS6301 Programming and Data Structures II Unit 4 Page 1 CS6301 Programming and Data Structures
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CS6301 Programming and Data Structures II Unit 4 Page 1
CS6301 Programming and Data Structures II
Unit -4
AVL TREE
AVL tree (Adelson-Velskii and Landis' tree, named after the inventors) is a self-balancing binary
search tree. It was the first such data structure to be invented.[1] In an AVL tree, the heights of the
two child subtrees of any node differ by at most one; if at any time they differ by more than one,
rebalancing is done to restore this property.
• AVL trees are height-balanced binary search trees
• Balance factor of a node
› height(left subtree) - height(right subtree)
• An AVL tree has balance factor calculated at every node
› For every node, heights of left and right subtree can differ by no more than 1
› Store current heights in each node
Two types of rotation:
x Single Rotation
x Double Rotation
Single Rotation
x LL Rotation
x RR Rotation
Double Rotation
x LR Rotation
x RL Rotation
www.studentsfocus.com
www.studentsfocus.com
CS6301 Programming and Data Structures II Unit 4 Page 2
Single rotate (from the) left, on a left-left rotation
Double rotate (from the) left, on a left-right rotation
This diagram depicts the re-balance operation after an unbalancing left-right insertion.
Example:
1, 2, 3, 4, 5, 6, 7
www.studentsfocus.com
www.studentsfocus.com
CS6301 Programming and Data Structures II Unit 4 Page 3
Example: 50, 25, 10, 5, 7, 3, 30, 20, 8, 15
www.studentsfocus.com
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 4
B-TREES
A m-way tree in which
x The root has at least one key
x Non-root nodes have at least m/2 subtrees (i.e., at least (m - 1)/2 keys)
x All the empty subtrees (i.e., external nodes) are at the same level
Properties of B-Tree
1) All leaves are at same level.
2) A B-Tree is defined by the term minimum degree ‘t’. The value of t depends upon disk block
size.
3) Every node except root must contain at least t-1 keys. Root may contain minimum 1 key.
4) All nodes (including root) may contain at most 2t – 1 keys.
5) Number of children of a node is equal to the number of keys in it plus 1.
6) 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.
7) B-Tree grows and shrinks from root which is unlike Binary Search Tree. Binary Search Trees
grow downward and also shrink from downward.
8) Like other balanced Binary Search Trees, time complexity to search, insert and delete is
O(Logn).
Following is an example B-Tree of minimum degree 3. Note that in practical B-Trees, the value
of minimum degree is much more than 3.
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 5
Search
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 don’t find k in the leaf
node, we return NULL.
Add 19
Add 21
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 6
RED BLACK TREES
Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows
following rules.
1) Every node has a color either red or black.
2) Root of tree is always black.
3) There are no two adjacent red nodes (A red node cannot have a red parent or red child).
4) Every path from root to a NULL node has same number of black nodes.
Insertion:
In AVL tree insertion, we used rotation as a tool to do balancing after insertion caused
imbalance. In Red-Black tree, we use two tools to do balancing.
1) Recoloring
2) Rotation
We try recoloring first, if recoloring doesn’t work, then we go for rotation. Following is detailed
algorithm. The algorithms has mainly two cases depending upon the color of uncle. If uncle is
red, we do recoloring. If uncle is black, we do rotations and/or recoloring.
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.
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 7
2) Do following if color of x’s parent is not BLACK or x is not root.
….a) If x’s uncle is RED (Grand parent must have been black from property 4)
……..(i) Change color of parent and uncle as BLACK.
……..(ii) color of grand parent as RED.
……..(iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x.
….b) If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s
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)
……..ii) Left Right Case (p is left child of g and x is right child of p)
……..iii) Right Right Case (Mirror of case a)
……..iv) Right Left Case (Mirror of case c)
3) If x is root, change color of x as BLACK (Black height of complete tree increases by 1).
Following are operations to be performed in four subcases when uncle is BLACK.
All four cases when Uncle is BLACK
Left Left Case (See g, p and x)
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 8
Left Right Case (See g, p and x)
Right Right Case (See g, p and x)
Right Left Case (See g, p and x)
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 9
Examples of Insertion
Deletion Steps
Following are detailed steps for deletion.
1) Perform standard BST delete. When we perform standard delete operation in BST, we always
end up deleting a node which is either leaf or has only one child (For an internal node, we copy
the successor and then recursively call delete for successor, successor is always a leaf node or a
node with one child). So we only need to handle cases where a node is leaf or has one child. Let
v be the node deleted and u be the child that replaces v.
2) Simple Case: If either u or v is red, we mark the replaced child as black (No change in black
height). Note that both u and v cannot be red as v is parent of u and two consecutive reds are not
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 10
allowed in red-black tree.
3) If Both u and v are Black.
3.1) Color u as double black. Now our task reduces to convert this double black to single black.
Note that If v is leaf, then u is NULL and color of NULL is considered as black. So the deletion
of a black leaf also causes a double black.
3.2) Do following while the current node u is double black or it is not root. Let sibling of node
be s.
….(a): If sibling s is black and at least one of sibling’s children is red, perform rotation(s).
Let the red child of s be r. This case can be divided in four subcases depending upon positions of
s and r.
…………..(i) Left Left Case (s is left child of its parent and r is left child of s or both children of
s are red). This is mirror of right right case shown in below diagram.
…………..(ii) Left Right Case (s is left child of its parent and r is right child). This is mirror of
right left case shown in below diagram.
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 11
…………..(iii) Right Right Case (s is right child of its parent and r is right child of s or both
children of s are red)
…………..(iv) Right Left Case (s is right child of its parent and r is left child of s)
…..(b): If sibling is black and its both children are black, perform recoloring, and recur for
the parent if parent is black.
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 12
In this case, if parent was red, then we didn’t need to recur for prent, we can simply make it
black (red + double black = single black)
…..(c): If sibling is red, perform a rotation to move old sibling up, recolor the old sibling and
parent. The new sibling is always black (See the below diagram). This mainly converts the tree
to black sibling case (by rotation) and leads to case (a) or (b). This case can be divided in two
subcases.
…………..(i) Left Case (s is left child of its parent). This is mirror of right right case shown in
below diagram. We right rotate the parent p.
…………..(iii) Right Case (s is right child of its parent). We left rotate the parent p.
3.3) If u is root, make it single black and return (Black height of complete tree reduces by 1).
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 13
SPLAY TREES
A splay tree is a self-adjusting binary search tree with the additional property that recently
accessed elements are quick to access again. All normal operations on a binary search tree are
combined with one basic operation, called splaying. Splaying the tree for a certain element
rearranges the tree so that the element is placed at the root of the tree. One way to do this is to
first perform a standard binary tree search for the element in question, and then use tree rotations
in a specific fashion to bring the element to the top. Alternatively, a top-down algorithm can
combine the search and the tree reorganization into a single phase.
Rotation:
Zig zig rotation
Zag zag rotation
Zig zag rotation
Zag zig rotation
Zig zig rotation and Zag zag rotation
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 14
Zig zag rotation
Zag zig rotation
BINOMIAL HEAPS
A binomial heap is implemented as a collection of binomial trees (compare with a binary heap,
which has a shape of a single binary tree). A binomial tree is defined recursively:
x A binomial tree of order 0 is a single node
x A binomial tree of order k has a root node whose children are roots of binomial trees of
orders k−1, k−2, ..., 2, 1, 0 (in this order).
A binomial heap is implemented as a set of binomial trees that satisfy the binomial heap
Properties:
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 15
x Each binomial tree in a heap obeys the minimum-heap property: the key of a node is
greater than or equal to the key of its parent.
x There can only be either one or zero binomial trees for each order, including zero order.
The first property ensures that the root of each binomial tree contains the smallest key in the tree,
which applies to the entire heap.
The second property implies that a binomial heap with n nodes consists of at most log n + 1
binomial trees. In fact, the number and orders of these trees are uniquely determined by the
number of nodes n: each binomial tree corresponds to one digit in the binary representation of
number n. For example number 13 is 1101 in binary, , and thus a binomial heap
with 13 nodes will consist of three binomial trees of orders 3, 2, and 0 (see figure below).
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 16
This shows the merger of two binomial heaps. This is accomplished by merging two binomial
trees of the same order one by one. If the resulting merged tree has the same order as one
binomial tree in one of the two heaps, then those two are merged again.
FIBONACCI HEAPS
A Fibonacci heap is a heap data structure consisting of a collection of trees. It has a better
amortized running time than a binomial heap. A Fibonacci heap is a collection of trees satisfying
the minimum-heap property, that is, the key of a child is always greater than or equal to the key
of the parent. This implies that the minimum key is always at the root of one of the trees.
Compared with binomial heaps, the structure of a Fibonacci heap is more flexible. The trees do
not have a prescribed shape and in the extreme case the heap can have every element in a
separate tree. This flexibility allows some operations to be executed in a "lazy" manner,
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 17
postponing the work for later operations. For example merging heaps is done simply by
concatenating the two lists of trees, and operation decrease key sometimes cuts a node from its
parent and forms a new tree.
Figure 1. Example of a Fibonacci heap. It has three trees of degrees 0, 1 and 3. Three vertices are
marked (shown in blue). Therefore the potential of the heap is 9 (3 trees + 2 * marked-vertices).
Fibonacci heap from Figure 1 after first phase of extract minimum. Node with key 1 (the
minimum) was deleted and its children were added as separate trees.
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 18
Fibonacci heap from Figure 1 after extract minimum is completed. First, nodes 3 and 6 are linked
together. Then the result is linked with tree rooted at node 2. Finally, the new minimum is found.
Fibonacci heap from Figure 1 after decreasing key of node 9 to 0. This node as well as its two
marked ancestors are cut from the tree rooted at 1 and placed as new roots.
DISJOINT SETS
Some applications involve grouping n distinct objects into a collection of disjoint sets. Two
important operations are then finding which set a given object belongs to and uniting the two
sets.
A disjoint set data structure maintains a collection S={ S1 , S2 ,…, Sk } of disjoint dynamic sets.
Each set is identified by a representative, which usually is a member in the set.
1 Operations on Sets
Let x be an object. We wish to support the following operations.
MAKE-SET( x) creates a new set whose only member is pointed by x; Note that x is not in the
other sets.
UNION( x,y) unites two dynamic sets containing objects x and y, say Sx and Sy , into a new set
that Sx∪Sy , assuming that Sx ∩ Sy =∅;
FIND-SET( x) returns a pointer to the representative of the set containing x.
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 19
INSERT(a,S) inserts an object a to S, and returns S∪{a}.
DELETE(a,S) deletes an object a from S, and returns S-{a}.
SPLIT(a,S) partitions the objects of S into two sets S1 and S2 such that S1 ={b | b≤a b∈S},
and S2 =S- S1 .
MINIMUM( S) returns the minimum object in S.
2 Applications of Disjoint-set Data Structures
Here we show two application examples.
x Connected components (CCs)
x Minimum Spanning Trees (MSTs)
2.1 Algorithm for Connected Components
CONNECTED-COMPONENTS (G)
1 for each v∈V
2 do MAKE-SET (v)
3 for every edge (u,v)∈E
4 do if FIND-SET (u) ≠ FIND-SET (v)
5 then UNION (u,v)
SAME-COMPONENTS (u,v)
1 if FIND-SET (u) = FIND-SET (v)
2 then return TRUE
3 return FALSE
www.studentsfocus.com
Sri Vidya College of Engineering & Technology, Virudhunagar Course Material (Lecture Note)
CS6301 Programming and Data Structures II Unit 4 Page 20
Figure 1: A graph with four connected components: {a,b,c,d}, {e,f,g}, {h,i}, and {j}.