Cs-509: Advanced Data Structures and Algorithms: Assignment Number 3
Cs-509: Advanced Data Structures and Algorithms: Assignment Number 3
18203007
1. Define a 2-d tree. Give algorithms for insert & delete operations in 2-d tree.
Answer. A 2-d Tree(also called as 2-Dimensional Tree) is a binary search tree where data
in each node is a 2-Dimensional point in space. In short, it is a space partitioning data
structure for organising points in a K-Dimensional space.
A non-leaf node in 2-d tree divides the space into two parts, called as half-spaces.
Points to the left of this space are represented by the left subtree of that node and points
to the right of the space are represented by the right subtree.
For example, The root would have an x-aligned plane, the root’s children would both have
y-aligned planes, the root’s grandchildren would all have x-aligned planes, and the root’s
great-grandchildren would all have y-aligned planes and so on.
Code (Insertion):-
const int k = 2;
return root;
}
return true;
}
int main()
{
struct Node *root = NULL;
int points[][k] = {{3, 6}, {17, 15}, {13, 15}, {6, 12},
{9, 1}, {2, 7}, {10, 19}};
int n = sizeof(points)/sizeof(points[0]);
return 0;
}
Algorithm for deletion operation:-
Like Binary Search Tree Delete, we recursively traverse down and search for the point to
be deleted. Below are steps are followed for every node visited.
1) If current node contains the point to be deleted
1. If node to be deleted is a leaf node, simply delete it (Same as BST Delete)
2. If node to be deleted has right child as not NULL (Different from BST)
• Find minimum of current node’s dimension in right subtree.
• Replace the node with above found minimum and recursively delete minimum in
right subtree.
3. Else If node to be deleted has left child as not NULL (Different from BST)
• Find minimum of current node’s dimension in left subtree.
• Replace the node with above found minimum and recursively delete minimum in
left subtree.
• Make new left subtree as right child of current node.
2) If current doesn’t contain the point to be deleted
• If node to be deleted is smaller than current node on current dimension, recur for
left subtree.
• Else recur for right subtree.
2. Explain insertion & deletion algorithms for Min-Max heaps with examples.
Answer. INSERTION:-
1. Append the required key to (the end of) the array representing the min-max heap.
This will likely break the min-max heap properties, therefore we need to adjust the
heap.
• If it is found to be less (greater) than the parent, then it is surely less (greater)
than all other nodes on max (min) levels that are on the path to the root of heap.
Now, just check for nodes on min (max) levels.
• The path from the new node to the root (considering only min (max) levels)
should be in a descending (ascending) order and it was before the insertion. So,
we need to make a binary insertion of the new node into this sequence.
Technically it is simpler to swap the new node with its parent while the parent is
greater (less).
This process is implemented by calling the push-up algorithm described below on the
index of the newly-appended key.
//PUSH-UP FUNCTION
function PUSH-UP(h, i):
if i is not the root then:
if i is on a min level then:
if h[i] > h[parent(i)] then:
swap h[i] and h[parent(i)]
PUSH-UP-MAX(h, parent(i))
else:
PUSH-UP-MIN(h, i)
endif
else:
if h[i] < h[parent(i)] then:
swap h[i] and h[parent(i)]
PUSH-UP-MIN(h, parent(i))
else:
PUSH-UP-MAX(h, i)
endif
endif
endif
//PUSH-UP-MIN
function PUSH-UP-MIN(h, i):
if i has a grandparent and h[i] < h[grandparent(i)] then:
swap h[i] and h[grandparent(i)]
PUSH-UP-MIN(h, grandparent(i))
endif
//PUSH-UP-MAX
function PUSH-UP-MAX(h, i):
if i has a grandparent and h[i] > h[grandparent(i)] then:
swap h[i] and h[grandparent(i)]
PUSH-UP-MAX(h, grandparent(i))
endif
Example:-
Say we have the following min-max heap and want to insert a new node with value 6.
Initially, node 6 is inserted as a right child of the node 11. 6 is less than 11, therefore it is
less than all the nodes on the max levels (41), and we need to check only the min levels (8
and 11). We should swap the nodes 6 and 11 and then swap 6 and 8. So, 6 gets moved to
the root position of the heap, the former root 8 gets moved down to replace 11, and 11
becomes a right child of 8.
Consider adding the new node 81 instead of 6. Initially, the node is inserted as a right child
of the node 11. 81 is greater than 11, therefore it is greater than any node on any of the
min levels (8 and 11). Now, we only need to check the nodes on the max levels (41) and
make one swap.
DELETION:-
3. push-down is then called on the root index to restore the heap property
//PUSH-DOWN
function PUSH-DOWN(h, i):
if i is on a min level then:
PUSH-DOWN-MIN(h, i)
else:
PUSH-DOWN-MAX(h, i)
endif
//PUSH-DOWN-MIN
function PUSH-DOWN-MIN(h, i):
if i has children then:
m := index of the smallest child or grandchild of i
if m is a grandchild of i then:
if h[m] < h[i] then:
swap h[m] and h[i]
if h[m] > h[parent(m)] then:
swap h[m] and h[parent(m)]
endif
PUSH-DOWN-MIN(h, m)
endif
else if h[m] < h[i] then:
swap h[m] and h[i]
endif
endif
//PUSH-DOWN-MAX
function PUSH-DOWN-MAX(h, i):
if i has children then:
m := index of the largest child or grandchild of i
if m is a grandchild of i then:
if h[m] > h[i] then:
swap h[m] and h[i]
if h[m] < h[parent(m)] then:
swap h[m] and h[parent(m)]
endif
PUSH-DOWN-MAX(h, m)
endif
else if h[m] > h[i] then:
swap h[m] and h[i]
endif
endif
Answer. The main application of Binary Heap is to implement priority queue. Binomial
Heap is an extension of Binary Heap that provides faster union or merge operation
together with other operations provided by Binary Heap.
A Binomial Tree of order 0 has 1 node. A Binomial Tree of order k can be constructed by
taking two binomial trees of order k-1 and making one as leftmost child or other.
b) It has depth as k.
d) The root has degree k and children of root are themselves Binomial Trees with order
k-1, k-2,.. 0 from left to right.
Binomial Heap:
A Binomial Heap is a set of Binomial Trees where each Binomial Tree follows Min Heap
property. And there can be at most one Binomial Tree of any degree.
4. Working modulo q = 11, how many spurious hits does the Rabin-Karp matcher
encounter in the text T = 3141592653589793 when looking for the pattern P = 26?
15 mod 11 = 4
59 mod 11 = 4
92 mod 11 = 4
6. Draw the Kd tree and matching graph when the following points are inserted : (2,
3), (4, 2), (4, 4), (3, 3), (1, 5).
Answer. Kd tree:-
Matching graph:-