Define Tree. List Its Features.: Lecture Notes
Define Tree. List Its Features.: Lecture Notes
asia
Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Find operation returns a pointer to the node in tree T that has key X, or NULL if
there is no such node.
o To insert X into tree T, proceed down the tree as with a Find. If X is found, do
nothing. Otherwise, insert X at the last spot on the path traversed.
o For deletion, once the node to be deleted is found, the possibilities to be examined
are leaf node, node with one child and node with two child.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o All the tree operations can be performed in O(log N) time, except insertion.
o Inserting a node could violate the AVL tree property. The four cases are:
1) An insertion into the left subtree of the left child
2) An insertion into the right subtree of the left child
3) An insertion into the left subtree of the right child
4) An insertion into the right subtree of the right child
o If so, then it is restored with a simple transformation to the tree, known as a rotation.
o Cases 1 and 4 are fixed by a single rotation of the tree.
o Cases 2 and 3 are handled by double rotation.
o A rotation involves only a few pointer changes, and changes the structure of the tree
while preserving the search tree property.
Single Rotation
Double Rotation
o The single rotation does not fix the problem if insertion is done either onto right
subtree of the left child or onto left subtree of the right child.
o When insertion is done to right of A (case 3), imbalance is fixed using double rotation
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o When insertion is done to left of D (case 2), imbalance is fixed using double rotation
Illustrate insertion of keys 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8 onto an empty AVL
tree. Show the various rotation involved.
o Insert key 1 into empty AVL tree.
o Insert key 2 to the right of 1, since 2 > 1.
o Problem occurs when inserting key 3, because AVL property is violated at the root.
Single rotation is performed to fix the problem.
o Inserting 6 causes a balance problem for the root, since its left subtree is of height 0,
and its right subtree would be height 2. Therefore a single rotation is done.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Next key 7 is inserted that results in imbalance at node 5, thereby another rotation.
o Inserting 13 requires a double rotation. In this case, k3 is the node 6, k1 is node 14 and
k2 is the node 7. Subtree A is the tree rooted at node 5, subtree B is the empty, subtree
C is the tree at node 13 and subtree D is the node with 15.
o Inserting 12 creates an imbalance at the root. A single rotation will restore the tree.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Inserting 10, forces a single rotation, and the same is true for insertion of 9.
o Finally, 8 is inserted to produce the final tree.
}
else
if(X < T->Element)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
{
T->Left = Insert(X, T->Left);
if(Height(T->Left) - Height(T->Right) == 2)
if(X < T->Left->Element)
T = SingleRotateWithLeft(T);
else
T = DoubleRotateWithLeft(T);
}
else if(X > T->Element)
{
T->Right = Insert(X, T->Right);
if(Height(T->Right) - Height(T->Left) == 2)
if(X > T->Right->Element)
T = SingleRotateWithRight(T);
else
T = DoubleRotateWithRight(T);
}
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
T->Height=Max(Height(T->Left), Height(T->Right))+1;
return T;
}
// Single Rotation
static Position SingleRotateWithLeft(Position K2)
{
Position K1;
K1 = K2->Left;
K2->Left = K1->Right;
K1->Right = K2;
K2->Height=Max(Height(K2->Left),Height(K2->Right))+1;
K1->Height=Max(Height(K1->Left), K2->Height) + 1;
return K1;
}
// Double Rotation
static Position DoubleRotateWithLeft(Position K3)
{
K3->Left = SingleRotateWithRight(K3->Left);
return SingleRotateWithLeft(K3);
}
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Splaying
o The strategy is to rotate bottom up along the access path.
o Let X be a (non-root) node on the access path.
o If the parent of X is root of the tree, then rotate X and the root. This is the last rotation
along access path.
o Otherwise, X has both a parent (P) and a grandparent (G), with two cases.
o If X is a right child and P is a left child (or vice versa). If so perform a double
rotation. This is known as zig-zag case.
o Otherwise, X and P are either both left or right children. In that case,
transform tree on the left to tree on the right. This is known as zig-zig case.
Example
o Nodes with keys 1, 2, … 7 are inserted into initial empty tree.
o Splaying at the node with key 1 is shown below. Node with key 1 is pushed to the
root, through a series of zig-zig rotations.
o Access of node with key 1 takes N units, access on node with key 2 takes only N/2
units instead of another N units.
o Access on the node with item 2 will bring nodes to within N/4 of the root, and this is
repeated until the depth becomes roughly log N.
o Splaying not only moves the accessed node to the root, but also has the effect of
roughly halving the depth of most nodes on the access path.
o When access paths are long, leading to a longer-than-normal search time, the rotations
CS6301 Programming & Data Structures II Unit IV Page 9
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
B-tree of order 4
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Initial 2-3 B-tree is shown below (M = L = 3). Non-leaf nodes are represented as
ellipses, leaves are shown as boxes, which contain the keys.
o Next insert 1.
o Since 1 < 22, traverse left subtree. Since 1 < 16, traverse left subtree.
o It leads to a leaf which already has 3 elements (max L = 3). Insertion at that
leaf violates the property of 2-3 B-Tree.
o Therefore it is solved by splitting leaf into two nodes each with two data items
and adjusting the pointers in the parent as shown.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Therefore the root is split into two nodes and a new root is created. Thus
height of the tree increases.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o When the parent is split, update values of the keys and also the parent’s parent, thus
incurring additional disk writes.
o Splitting nodes is time consuming, but it is a relatively rare occurrence for large B-
Trees.
Explain deletion in B-Trees using an example.
o Deletion can be performed b y finding the data item to be deleted and then removing it
o If the leaf it was in had only L/2 data items, then after deletion the leaf has less than
L/2 data items. This violates the B-Tree property.
o It is solved by adopting a neighboring data item, if the neighbor is not itself at its
minimum.
o Otherwise, combine with the neighbor to form a full leaf. This leads to a child loss for
the parent.
o If the parent falls below the minimum, then proceed as above, until it reaches the root.
o If the root loses its second child, then the root is also deleted and the tree becomes one
level shallower.
o Thus B-Tree could lose height as a result of deletion.
Example
o Consider the above B-Tree of order 5, wherein each leaf node should have at least
three data items.
o To remove data 99, perform Find operation for 99.
o It leads to a leaf having only three data items, i.e., minimum number of
elements.
o Deleting 99 leads to the leaf having only two children. This violates the B-
Tree property.
o Since the leaf has only two items and its neighbor is already at its minimum of
three, combine the items into a new leaf of ¿ve items.
o But now, its parent loses a child and has only two children, which is violation
of B-Tree property.
o This is solved by adopting from a neighbor, because the neighbor has four
children. Thus both have three children as shown.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Name / representative of the set is the root node for that set.
o Implicitly disjoint set is represented as an array.
o Each entry s[i] in the array represents parent for element i
o If i is a root, then s[i] = í1
-1 -1 -1 -1 -1 -1 -1 -1
0 1 2 3 4 5 6 7
Example
o union(4, 5) results in merger of sets 4 and 5 as a single tree with root as 4.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Pseudocode
// union routine
void unionSets(int root1, int root2)
{
s[root2] = root1;
}
// find routine
int find(int x)
{
if(s[x] < 0)
return x;
else
}
return
find(s[x])
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Union (3,4) results in the same forest for both by size and height.
Pseudocode
CS6301 Programming & Data Structures II Unit IV Page 17
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
// union-by-height
void unionSets(int root1, int root2)
{
if(s[root2] < s[root1]) // root2 is deeper
s[root1] = root2;
else
{
if(s[root1] == s[root2])
--s[root1];
s[root2] = root1;
}
}
What is path compression on disjoint sets? Explain find operation using path compression.
o Worst case of all union/find algorithm results in O(M log N).
o Running time is better by improvising the find operation, known as path compression.
o Path compression for node x results in every node on the path from x to the root has
its parent changed to root.
o After the root of the set is found recursively, x’s parent link references it. This occurs
recursively to every node on the path to the root. The pseudocode is:
// find with path compression
int find(int x)
{
if(s[x] < 0)
return x;
else
return s[x] = find(s[x]);
}
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Briefly explain insert and delete operations on binary heap with an example.
insert
o To insert an element X into the heap, we create a hole in the next available location.
o If X can be placed in the hole without violating heap order, insertion is done.
o Otherwise percolate up the hole and slide the parent down. Repeat the process until X
can be placed in the hole.
o For example, to insert 14, a hole is created.
o Since 31 > 14, 31 is slide down and hole is moved up o
Since 21 > 14, 21 is slide down and hole is moved up o
Eventually 14 is placed in the hole
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
deleteMin
o Minimum element of the tree, i.e., root is removed. This creates a hole at the root. The
heap becomes one element smaller.
o Slide up smaller of hole's children as root and the hole is percolate down. Repeat the
process until hole becomes a leaf element.
o Last element is placed in the hole, and hole is eventually removed from the tree.
o For example, deleteMin, removes the root 13. A hole is created. o
Since 14 < 16, 14 becomes the root and hole slides down o
Since 19 < 21, 19 moves up and hole slides down
o Since 26 < 65, 26 moves up and hole becomes the leaf.
o Last element 31 is placed in the hole.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Merged Heap H3
o Heaps H1, H2 and H3 have trees of height 2. Trees with root 12 and 14 (first and
second minimum) are merged. This results in a tree of height 3 and placed in H3.
o Tree with root 23 in H2 of height 2 is placed in H3. Merge process is complete.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Insert 21
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o In the newly formed heap, there are trees of the same rank, i.e., height. Hence trees of
same rank are consolidated and min pointer is updated, until all are of distinct rank.
o Trees with root 23 and 17 are of same rank, i.e., 0. Hence they are consolidated as a
single tree. Now there is only one tree of rank 0.
o Trees with root 7 and 17 are of rank 1. Hence they are consolidated.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Trees with root 7 and 24 are of rank 2. Hence they are consolidated.
o Next trees with root 18 and 41 are of rank 1. Hence they are consolidated.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
o Now all trees have distinct rank. The consolidation process is over.
o For example, decrease the value of node from 46 to 29. The heap-order property is
maintained. Hence the process is over.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
. Lecture Notes
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Lecture Notes
Give value for variables maintained for the given fibonacci heap.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
jntuworldupdates.org Specworld.in