ADS Lab I
ADS Lab I
ADS Lab I
(UGC-Autonomous)
Approved by AICTE, Affiliated to JNTUH, Accredited by NAAC- ‘A’ Grade
Medbowli, Meerpet, Balapur, Hyderabad, Telangana- 500097
Mob: 9393959597. Email: info@tkrec.ac.in, deanacademics@tkrec.ac.in
ADVANCED DATA STRUCTURES LAB MANUAL
Write a C++ program to perform the following operations:
1.insert an element into a Binary search tree.
2.Delete an element from a Binary search tree.
3.Search for a key element in a Binary search tree
#include<iostream>
#include<conio.h>
#include<stdlib.h>
void insert(int,int );
void delte(int);
EC
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;
R
int main()
TK
int ch,y;
tree[i]=-1;
while(1)
cout
<<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT\nEnter your
Choice:";
switch(ch)
{
case 1:
insert(1,ch);
break;
case 2:
cin >>x;
y=search(1);
if(y!=-1) delte(y);
EC
else cout<<"No Such Element Found in Tree";
break;
case 3:
R
display(1);
cout<<"\n";
TK
cout <<i;
cout <<"\n";
break;
case 4:
cin >> x;
y=search(1);
case 5:
exit(0);
} return 0;
int x;
if(t==1)EC
{
tree[t++]=ch;
return;
R
}
x=search1(s,ch);
TK
if(tree[x]>ch)
tree[2*x]=ch;
else
tree[2*x+1]=ch;
t++;
void delte(int x)
tree[x]=-1;
else if(tree[2*x]==-1)
tree[x]=tree[2*x+1];
tree[2*x+1]=-1;
else if(tree[2*x+1]==-1)
tree[x]=tree[2*x];
tree[2*x]=-1;
} EC
else
tree[x]=tree[2*x];
R
delte(2*x);
}
TK
t--;
int search(int s)
if(t==1)
return -1;
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
void display(int s)
{ EC
if(t==1)
}
TK
if(tree[i]==-1)
return ;
if(t==1)
{
cout <<"No element in tree";
return -1;
if(tree[s]==-1)
return s/2;
search1(2*s,ch);
else search1(2*s+1,ch);
OUTPUT: EC
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your Choice:1
Enter the element to Insert33
R
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
TK
5.EXIT
Enter your Choice:3
33
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your Choice:4
Enter the Element to Search:33
33is in1Position
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your Choice:5
Write a C++ program to perform the following sorting methods:
1.Merge sort 2.Heap sort 3. Quick sort.
#include<iostream>
using namespace std;
void swapping(int &a, int &b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void merge(int *array, int l, int m, int r) {
int i, j, k, nl, nr;
//size of left and right sub-arrays
nl = m-l+1; nr = r-m;
EC
int larr[nl], rarr[nr];
//fill left and right sub-arrays
for(i = 0; i<nl; i++)
larr[i] = array[l+i];
for(j = 0; j<nr; j++)
R
rarr[j] = array[m+1+j];
i = 0; j = 0; k = l;
TK
Output
Enter the number of elements: 6
Enter elements:
14 20 78 98 20 45
Array before Sorting: 14 20 78 98 20 45
Array after Sorting: 14 20 20 45 78 98
#include <iostream>
largest = l;
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, i);
EC
// One by one extract an element from heap
heapify(arr, i, 0);
// Driver program
int main()
//heapify algorithm
// the loop must go reverse you will get after analyzing manually
EC
// (i=n/2 -1) because other nodes/ ele's are leaf nodes
heapify(arr,n,i);
printArray(arr, n);
heapSort(arr, n);
printArray(arr, n);
return 0;
}
//code by Prajwal Chougale
Output
After heapifying array is
70 60 40 20 30 10
Sorted array is
10 20 30 40 60 70
Time complexity : O(N*logN)
Auxiliary space: O(1)
#include <iostream>
EC
using namespace std;
{
R
int pivot = arr[start];
int count = 0;
TK
count++;
swap(arr[pivotIndex], arr[start]);
i++;
j--;
}
R
return pivotIndex;
TK
// base case
return;
quickSort(arr, p + 1, end);
int main()
int arr[] = { 9, 3, 4, 2, 1, 8 };
int n = 6;
quickSort(arr, 0, n - 1);
EC
for (int i = 0; i < n; i++) {
}
R
return 0;
TK
Output
1 2 3 4 8 9
#include<iostream>
class BTreeNode
public:
EC
BTreeNode(int _t, bool _leaf); // Constructor
// this node. The assumption is, the node must be non-full when this
R
// function is called
TK
// child array C[]. The Child y must be full when this function is called
void traverse();
// Make BTree friend of this so that we can access private members of this
// class in BTree functions
};
// A BTree
class BTree
public:
EC
// Constructor (Initializes tree as empty)
BTree(int _t)
void traverse()
BTreeNode* search(int k)
};
// Constructor for BTreeNode class
t = t1;
leaf = leaf1;
n = 0;
R
}
TK
void BTreeNode::traverse()
int i;
if (leaf == false)
C[i]->traverse();
if (leaf == false)
C[i]->traverse();
}
EC
// Function to search key k in subtree rooted with this node
BTreeNode *BTreeNode::search(int k)
{
R
// Find the first key greater than or equal to k
TK
int i = 0;
i++;
if (keys[i] == k)
return this;
if (leaf == true)
return NULL;
return C[i]->search(k);
void BTree::insert(int k)
// If tree is empty
if (root == NULL)
{
EC
// Allocate memory for root
if (root->n == 2*t-1)
s->C[0] = root;
// Split the old root and move 1 key to the new root
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k)
i++;
EC
s->C[i]->insertNonFull(k);
// Change root
root = s;
R
}
TK
root->insertNonFull(k);
// function is called
void BTreeNode::insertNonFull(int k)
{
int i = n-1;
if (leaf == true)
keys[i+1] = keys[i];
R
i--;
TK
keys[i+1] = k;
n = n+1;
if (C[i+1]->n == 2*t-1)
splitChild(i+1, C[i+1]);
i++;
}
R
C[i+1]->insertNonFull(k);
TK
// of y
z->keys[j] = y->keys[j+t];
if (y->leaf == false)
z->C[j] = y->C[j+t];
}
EC
// Reduce the number of keys in y
y->n = t - 1;
R
// Since this node is going to have a new child,
TK
C[j+1] = C[j];
C[i+1] = z;
// new key and move all greater keys one space ahead
keys[j+1] = keys[j];
// Copy the middle key of y to this node
keys[i] = y->keys[t-1];
n = n + 1;
int main()
t.insert(20);
t.insert(5);
R
t.insert(6);
TK
t.insert(12);
t.insert(30);
t.insert(7);
t.insert(17);
t.traverse();
int k = 6;
k = 15;
(t.search(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present";
return 0;
Output:
Traversal of the constructed tree is 5 6 7 10 12 17 20 30
Present
Not Present
// Deleting a key from a B-tree in C++
#include <iostream>
using namespace std;
int *keys;
int t;
EC
class BTreeNode {
BTreeNode **C;
int n;
bool leaf;
R
public:
BTreeNode(int _t, bool _leaf);
TK
void traverse();
class BTree {
BTreeNode *root;
int t;
public:
BTree(int _t) {
root = NULL;
t = _t;
}
void traverse() {
if (root != NULL)
root->traverse();
}
n = 0;
}
// Deletion operation
void BTreeNode::deletion(int k) {
int idx = findKey(k);
if (C[idx]->n < t)
fill(idx);
return;
}
if (C[idx]->n >= t) {
int pred = getPredecessor(idx);
keys[idx] = pred;
C[idx]->deletion(pred);
}
return cur->keys[0];
}
else {
if (idx != n)
merge(idx);
else
merge(idx - 1);
}
return;
}
if (!child->leaf)
child->C[0] = sibling->C[sibling->n];
child->n += 1;
sibling->n -= 1;
return;
} EC
// Borrow from the next
void BTreeNode::borrowFromNext(int idx) {
BTreeNode *child = C[idx];
BTreeNode *sibling = C[idx + 1];
R
child->keys[(child->n)] = keys[idx];
if (!(child->leaf))
TK
child->C[(child->n) + 1] = sibling->C[0];
keys[idx] = sibling->keys[0];
if (!sibling->leaf) {
for (int i = 1; i <= sibling->n; ++i)
sibling->C[i - 1] = sibling->C[i];
}
child->n += 1;
sibling->n -= 1;
return;
}
// Merge
void BTreeNode::merge(int idx) {
BTreeNode *child = C[idx];
BTreeNode *sibling = C[idx + 1];
child->keys[t - 1] = keys[idx];
if (!child->leaf) {
for (int i = 0; i <= sibling->n; ++i)
child->C[i + t] = sibling->C[i];
}
child->n += sibling->n + 1;
n--;
delete (sibling);
R
return;
}
TK
// Insertion operation
void BTree::insertion(int k) {
if (root == NULL) {
root = new BTreeNode(t, true);
root->keys[0] = k;
root->n = 1;
} else {
if (root->n == 2 * t - 1) {
BTreeNode *s = new BTreeNode(t, false);
s->C[0] = root;
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k)
i++;
s->C[i]->insertNonFull(k);
root = s;
} else
root->insertNonFull(k);
}
}
if (leaf == true) {
while (i >= 0 && keys[i] > k) {
keys[i + 1] = keys[i];
i--;
}
keys[i + 1] = k;
n = n + 1;
EC
} else {
while (i >= 0 && keys[i] > k)
i--;
if (C[i + 1]->n == 2 * t - 1) {
splitChild(i + 1, C[i + 1]);
R
if (keys[i + 1] < k)
i++;
TK
}
C[i + 1]->insertNonFull(k);
}
}
// Split child
void BTreeNode::splitChild(int i, BTreeNode *y) {
BTreeNode *z = new BTreeNode(y->t, y->leaf);
z->n = t - 1;
if (y->leaf == false) {
for (int j = 0; j < t; j++)
z->C[j] = y->C[j + t];
}
y->n = t - 1;
for (int j = n; j >= i + 1; j--)
C[j + 1] = C[j];
C[i + 1] = z;
n = n + 1;
}
// Traverse
void BTreeNode::traverse() {
int i;
for (i = 0; i < n; i++) {
EC
if (leaf == false)
C[i]->traverse();
cout << " " << keys[i];
}
if (leaf == false)
R
C[i]->traverse();
}
TK
// Delete Operation
void BTree::deletion(int k) {
if (!root) {
cout << "The tree is empty\n";
return;
}
root->deletion(k);
if (root->n == 0) {
BTreeNode *tmp = root;
if (root->leaf)
root = NULL;
else
root = root->C[0];
delete tmp;
}
return;
}
int main() {
BTree t(3);
t.insertion(8);
t.insertion(9);
t.insertion(10);
t.insertion(11);
t.insertion(15);
t.insertion(16);
t.insertion(17);
t.insertion(18);
t.insertion(20);
t.insertion(23);
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 4
#define MIN 2
Type key;
}BT;
int count;
EC
BT entry[MAX+1];
treenode *branch[MAX+1];
}node;
R
class B
TK
node *root;
public:
};
if((strcmp(a,b))<(0))
return 1;
else
return 0;
if((strcmp(a,b))==(0))
return 1;
else
return 0;
EC
}
if(root==NULL)
return NULL;
else if(SearchNode(target,root,targetpos))
return root;
else
return Search(target,root->branch[*targetpos],targetpos); }
{
if(LT(target,current->entry[1].key))
{*
pos=0;
return 0;
else
EC
for(*pos=current->count;
}
TK
BT medentry;
node *medright;
node *New;
{
New=new node;
New->count=1;
New->entry[1]=medentry;
New->branch[0]=root;
New->branch[1]=medright;
return New;
return root;
EC
}
if(current==NULL)
med=New;
medright=NULL;
return 1;
else
{
if(SearchNode(New.key,current,&pos))
cout<<"Duplicate key\n";
if(MoveDown(New,current->branch[pos],med,medright))
if(current->count<MAX)
InsertIn(*med,*medright,current,pos);
return 0;
}
EC
else
{
R
Split(*med,*medright,current,pos,med,medright);
TK
return 1;
return 0;
int i;
for(i=current->count;i>pos;i--)
current->entry[i+1]=current->entry[i];
current->branch[i+1]=current->branch[i];
current->entry[pos+1]=med;
current->branch[pos+1]=medright;
EC
current->count++;
{
TK
int i;
int median;
if(pos<=MIN)
median=MIN;
//Class Declaration
class LeftistHeap
{
public: EC
LeftistHeap();
LeftistHeap(LeftistHeap &rhs);
~LeftistHeap();
bool isEmpty();
bool isFull();
int &findMin();
void Insert(int &x);
void deleteMin();
R
void deleteMin(int &minItem);
void makeEmpty();
void Merge(LeftistHeap &rhs);
LeftistHeap & operator =(LeftistHeap &rhs);
TK
private:
LeftistNode *root;
LeftistNode *Merge(LeftistNode *h1,
LeftistNode *h2);
LeftistNode *Merge1(LeftistNode *h1,
LeftistNode *h2);
void swapChildren(LeftistNode * t);
void reclaimMemory(LeftistNode * t);
LeftistNode *clone(LeftistNode *t);
};
// Copy constructor.
LeftistHeap::LeftistHeap(LeftistHeap &rhs)
{
root = NULL;
*this = rhs;
}
LeftistNode * h2)
{
if (h1->left == NULL)
h1->left = h2;
else
{
h1->right = Merge(h1->right, h2);
if (h1->left->dist < h1->right->dist)
swapChildren(h1);
h1->dist = h1->right->dist + 1;
}
return h1;
}
bool LeftistHeap::isEmpty()
{
return root == NULL;
}
// Deep copy
LeftistHeap &LeftistHeap::operator =(LeftistHeap & rhs)
{
if (this != &rhs)
{
makeEmpty();
root = clone(rhs.root);
}
return *this;
}
//Driver program
int main()
{
LeftistHeap h;
R
LeftistHeap h1;
LeftistHeap h2;
int x;
int arr[]= {1, 5, 7, 10, 15};
TK
h.Insert(arr[0]);
h.Insert(arr[1]);
h.Insert(arr[2]);
h.Insert(arr[3]);
h.Insert(arr[4]);
h1.Insert(arr1[0]);
h1.Insert(arr1[1]);
h.deleteMin(x);
cout<< x <<endl;
h1.deleteMin(x);
cout<< x <<endl;
h.Merge(h1);
h2 = h;
h2.deleteMin(x);
cout<< x << endl;
return 0;
}
Output
1
22
5
20. */
21. class BinomialHeap
22. {
23. private:
24. node *H;
25. node *Hr;
26. int count;
27. public:
28. node* Initializeheap();
29. int Binomial_link(node*, node*);
30. node* Create_node(int);
31. node* Union(node*, node*);
32. node* Insert(node*, node*);
33. node* Merge(node*, node*);
34. node* Extract_Min(node*);
35. int Revert_list(node*);
36. int Display(node*);
37. node* Search(node*, int);
38. int Decrease_key(node*, int, int);
39. int Delete(node*, int);
40. BinomialHeap()
41. {
42. H = Initializeheap();
43. Hr = Initializeheap();
44. int count = 1;
45. }
46. };
47.
48. /*
49. * Initialize Heap
50. */
51. node* BinomialHeap::Initializeheap()
52. {
53. node* np;
54. np = NULL;
55. return np;
56. }
57. /*
58. * Linking nodes in Binomial Heap
59. */
60. int BinomialHeap::Binomial_link(node* y, node* z)
61. {
62. y->parent = z;
63. y->sibling = z->child;
64. z->child = y;
65. z->degree = z->degree + 1;
66. }
67. /*
68. EC * Create Nodes in Binomial Heap
69. */
70. node* BinomialHeap::Create_node(int k)
71. {
72. node* p = new node;
73. p->n = k;
74. return p;
75. }
76. /*
R
77. * Insert Nodes in Binomial Heap
78. */
79. node* BinomialHeap::Insert(node* H, node* x)
80. {
TK
141. node* y;
142. node* z;
143. node* a;
144. node* b;
145. y = H1;
146. z = H2;
147. if (y != NULL)
148. {
149. if (z != NULL)
150. {
151. if (y->degree <= z->degree)
152. H = y;
153. else if (y->degree > z->degree)
154. H = z;
155. }
156. else
157. H = y;
158. }
159. else
160. H = z;
161. while (y != NULL && z != NULL)
162. {
163. if (y->degree < z->degree)
164. {
165. y = y->sibling;
166. }
167. else if (y->degree == z->degree)
168. {
169. a = y->sibling;
170. y->sibling = z;
171. y = a;
172. }
173. else
174. {
175. b = z->sibling;
176. z->sibling = y;
177. z = b;
178. }
179. }
180. return H;
181. }
182. /*
183. * Display Binomial Heap
184. */
185. int BinomialHeap::Display(node* H)
186. {
187. if (H == NULL)
188. {
189. EC cout<<"The Heap is empty"<<endl;
190. return 0;
191. }
192. cout<<"The root nodes are: "<<endl;
193. node* p;
194. p = H;
195. while (p != NULL)
196. {
197. cout<<p->n;
R
198. if (p->sibling != NULL)
199. cout<<"-->";
200. p = p->sibling;
201. }
TK
202. cout<<endl;
203. }
204. /*
205. * Extract Minimum
206. */
207. node* BinomialHeap::Extract_Min(node* H1)
208. {
209. Hr = NULL;
210. node* t = NULL;
211. node* x = H1;
212. if (x == NULL)
213. {
214. cout<<"Nothing to Extract"<<endl;
215. return x;
216. }
217. int min = x->n;
218. node* p = x;
219. while (p->sibling != NULL)
220. {
221. if ((p->sibling)->n < min)
222. {
223. min = (p->sibling)->n;
224. t = p;
225. x = p->sibling;
226. }
227. p = p->sibling;
228. }
229. if (t == NULL && x->sibling == NULL)
230. H1 = NULL;
231. else if (t == NULL)
232. H1 = x->sibling;
233. else if (t->sibling == NULL)
234. t = NULL;
235. else
236. t->sibling = x->sibling;
237. if (x->child != NULL)
238. {
239. Revert_list(x->child);
240. (x->child)->sibling = NULL;
241. }
242. H = Union(H1, Hr);
243. return x;
244. }
245. /*
246. * Reverse List
247. */
248. int BinomialHeap::Revert_list(node* y)
249. {
250. EC if (y->sibling != NULL)
251. {
252. Revert_list(y->sibling);
253. (y->sibling)->sibling = y;
254. }
255. else
256. {
257. Hr = y;
258. }
R
259. }
260.
261. /*
262. * Search Nodes in Binomial Heap
TK
263. */
264. node* BinomialHeap::Search(node* H, int k)
265. {
266. node* x = H;
267. node* p = NULL;
268. if (x->n == k)
269. {
270. p = x;
271. return p;
272. }
273. if (x->child != NULL && p == NULL)
274. p = Search(x->child, k);
275. if (x->sibling != NULL && p == NULL)
276. p = Search(x->sibling, k);
277. return p;
278. }
279. /*
280. * Decrease key of a node
281. */
282. int BinomialHeap::Decrease_key(node* H, int i, int k)
283. {
284. int temp;
285. node* p;
286. node* y;
287. node* z;
288. p = Search(H, i);
289. if (p == NULL)
290. {
291. cout<<"Invalid choice of key"<<endl;
292. return 0;
293. }
294. if (k > p->n)
295. {
296. cout<<"Error!! New key is greater than current
key"<<endl;
297. return 0;
298. }
299. p->n = k;
300. y = p;
301. z = p->parent;
302. while (z != NULL && y->n < z->n)
303. {
304. temp = y->n;
305. y->n = z->n;
306. z->n = temp;
307. y = z;
308. z = z->parent;
309. }
310. EC cout<<"Key reduced successfully"<<endl;
311. }
312. /*
313. * Delete Nodes in Binomial Heap
314. */
315. int BinomialHeap::Delete(node* H, int k)
316. {
317. node* np;
318. if (H == NULL)
R
319. {
320. cout<<"\nHEAP EMPTY!!!!!";
321. return 0;
322. }
TK
383. case 6:
384. exit(1);
385. default:
386. cout<<"Wrong Choice";
387. }
388. }
389. return 0;
390. }
Out put:
$ g++ binomialheap.cpp
$ a.out
----------------------------
Operations on Binomial heap
----------------------------
1)Insert Element in the heap
2)Extract Minimum key node
3)Decrease key of a node
4)Delete a node
5)Display Heap
6)Exit
Enter Your Choice: 1
Enter the element to be inserted: 9
----------------------------
Operations on Binomial heap
----------------------------
1)Insert Element in the heap
2)Extract Minimum key node
3)Decrease key of a node
4)Delete a node
5)Display Heap
6)Exit
Enter Your Choice: 1
Enter the element to be inserted: 8
----------------------------
Operations on Binomial heap
----------------------------
1)Insert Element in the heap
2)Extract Minimum key node
3)Decrease key of a node
4)Delete a node
5)Display Heap
6)Exit
Enter Your Choice: 1
Enter the element to be inserted: 7
----------------------------
Operations on Binomial heap
----------------------------
1)Insert Element in the heap
EC
2)Extract Minimum key node
3)Decrease key of a node
4)Delete a node
5)Display Heap
6)Exit
Enter Your Choice: 1
Enter the element to be inserted: 6
----------------------------
R
Operations on Binomial heap
----------------------------
1)Insert Element in the heap
2)Extract Minimum key node
TK
6)Exit
Enter Your Choice: 6
------------------
(program exited with code: 1)
Press return to continue
struct node {
struct node *left;
int data;
int height;
struct node *right;
};
class AVL
{
private:
public:
struct node * root;
AVL(){
this->root = NULL;
return n->left->height;
}
else if(n->left== NULL && n->right ){
return -n->right->height;
}
}
p->left = tp->right;
tp->right = p;
return tp;
}
return tp;
}
return tp2;
}
EC
struct node * lrrotation(struct node *n){
struct node *p;
struct node *tp;
struct node *tp2;
p = n;
tp = p->left;
tp2 =p->left->right;
R
p -> left = tp2->right;
tp ->right = tp2->left;
tp2 ->right = p;
tp2->left = tp;
TK
return tp2;
}
if(r==NULL){
struct node *n;
n = new struct node;
n->data = data;
r = n;
r->left = r->right = NULL;
r->height = 1;
return r;
}
else{
if(data < r->data)
r->left = insert(r->left,data);
else
r->right = insert(r->right,data);
}
r->height = calheight(r);
return r;
void levelorder_newline(){
if (this->root == NULL){
cout<<"\n"<<"Empty tree"<<"\n";
return;
}
levelorder_newline(this->root);
}
EC
void levelorder_newline(struct node *v){
queue <struct node *> q;
struct node *cur;
q.push(v);
q.push(NULL);
while(!q.empty()){
cur = q.front();
R
q.pop();
if(cur == NULL && q.size()!=0){
cout<<"\n";
TK
q.push(NULL);
continue;
}
if(cur!=NULL){
cout<<" "<<cur->data;
if (cur->left!=NULL){
q.push(cur->left);
}
if (cur->right!=NULL){
q.push(cur->right);
}
}
}
}
return p;
R
}
p = p->right;
return p;
}
return p;
}
~AVL(){
}
};
int main(){
AVL b;
int c,x;
do{
cout<<"\n1.Display levelorder on newline";
cout<<"\n2.Insert";
cout<<"\n3.Delete\n";
cout<<"\n0.Exit\n";
cout<<"\nChoice: ";
cin>>c;
switch (c)
{
case 1:
b.levelorder_newline();
// to print the tree in level order
break;
case 2:
cout<<"\nEnter no. ";
cin>>x;
b.root = b.insert(b.root,x);
break;
case 3:
cout<<"\nWhat to delete? ";
cin>>x;
b.root = b.deleteNode(b.root,x);
break;
EC
case 0:
break;
}
} while(c!=0);
}
R
Output:
TK
struct node
{
int key;
node *parent;
char color;
node *left;
node *right;
};
class RBtree
{
node *root;
node *q;
public :
RBtree()
{
q=NULL;
EC root=NULL;
}
void insert();
void insertfix(node *);
void leftrotate(node *);
void rightrotate(node *);
void del();
node* successor(node *);
R
void delfix(node *);
void disp();
void display( node *);
void search();
TK
};
void RBtree::insert()
{
int z,i=0;
cout<<"\nEnter key of the node to be inserted: ";
cin>>z;
node *p,*q;
node *t=new node;
t->key=z;
t->left=NULL;
t->right=NULL;
t->color='r';
p=root;
q=NULL;
if(root==NULL)
{
root=t;
t->parent=NULL;
}
else
{
while(p!=NULL)
{
q=p;
if(p->key<t->key)
p=p->right;
else
p=p->left;
}
t->parent=q;
if(q->key<t->key)
q->right=t;
else
q->left=t;
}
insertfix(t);
}
void RBtree::insertfix(node *t)
{
node *u;
if(root==t)
{
t->color='b';
return;
} EC
while(t->parent!=NULL&&t->parent->color=='r')
{
node *g=t->parent->parent;
if(g->left==t->parent)
{
if(g->right!=NULL)
{
R
u=g->right;
if(u->color=='r')
{
t->parent->color='b';
TK
u->color='b';
g->color='r';
t=g;
}
}
else
{
if(t->parent->right==t)
{
t=t->parent;
leftrotate(t);
}
t->parent->color='b';
g->color='r';
rightrotate(g);
}
}
else
{
if(g->left!=NULL)
{
u=g->left;
if(u->color=='r')
{
t->parent->color='b';
u->color='b';
g->color='r';
t=g;
}
}
else
{
if(t->parent->left==t)
{
t=t->parent;
rightrotate(t);
}
t->parent->color='b';
g->color='r';
leftrotate(g);
}
}
root->color='b';
}
} EC
void RBtree::del()
{
if(root==NULL)
{
cout<<"\nEmpty Tree." ;
return ;
R
}
int x;
cout<<"\nEnter the key of the node to be deleted: ";
cin>>x;
TK
node *p;
p=root;
node *y=NULL;
node *q=NULL;
int found=0;
while(p!=NULL&&found==0)
{
if(p->key==x)
found=1;
if(found==0)
{
if(p->key<x)
p=p->right;
else
p=p->left;
}
}
if(found==0)
{
cout<<"\nElement Not Found.";
return ;
}
else
{
cout<<"\nDeleted Element: "<<p->key;
cout<<"\nColour: ";
if(p->color=='b')
cout<<"Black\n";
else
cout<<"Red\n";
if(p->parent!=NULL)
cout<<"\nParent: "<<p->parent->key;
else
cout<<"\nThere is no parent of the node. ";
if(p->right!=NULL)
cout<<"\nRight Child: "<<p->right->key;
else
cout<<"\nThere is no right child of the node. ";
if(p->left!=NULL)
cout<<"\nLeft Child: "<<p->left->key;
else
cout<<"\nThere is no left child of the node. ";
cout<<"\nNode Deleted.";
if(p->left==NULL||p->right==NULL)
ECy=p;
else
y=successor(p);
if(y->left!=NULL)
q=y->left;
else
{
R
if(y->right!=NULL)
q=y->right;
else
q=NULL;
TK
}
if(q!=NULL)
q->parent=y->parent;
if(y->parent==NULL)
root=q;
else
{
if(y==y->parent->left)
y->parent->left=q;
else
y->parent->right=q;
}
if(y!=p)
{
p->color=y->color;
p->key=y->key;
}
if(y->color=='b')
delfix(q);
}
}
}
}
else
{
s=p->parent->left;
if(s->color=='r')
{
s->color='b';
p->parent->color='r';
rightrotate(p->parent);
s=p->parent->left;
}
if(s->left->color=='b'&&s->right->color=='b')
{
s->color='r';
p=p->parent;
}
else
{
if(s->left->color=='b')
{
s->right->color='b';
s->color='r';
leftrotate(s);
s=p->parent->left;
}
s->color=p->parent->color;
p->parent->color='b';
s->left->color='b';
rightrotate(p->parent);
p=root;
}
}
p->color='b';
root->color='b';
}
}
else
{
if(p==p->parent->left)
p->parent->left=y;
else
p->parent->right=y;
}
y->left=p;
p->parent=y;
}
}
void RBtree::rightrotate(node *p)
{
if(p->left==NULL)
return ;
else
{
node *y=p->left;
if(y->right!=NULL)
{
p->left=y->right;
y->right->parent=p;
}
else
p->left=NULL;
if(p->parent!=NULL)
y->parent=p->parent;
if(p->parent==NULL)
root=y;
else
{
if(p==p->parent->left)
p->parent->left=y;
else
p->parent->right=y;
}
y->right=p;
p->parent=y;
}
}
return y;
}
void RBtree::disp()
{
display(root);
}
void RBtree::display(node *p)
{
if(root==NULL)
{
cout<<"\nEmpty Tree.";
return ;
}
if(p!=NULL)
{
cout<<"\n\t NODE: ";
cout<<"\n Key: "<<p->key;
cout<<"\n Colour: ";
if(p->color=='b')
cout<<"Black";
else
cout<<"Red";
if(p->parent!=NULL)
cout<<"\n Parent: "<<p->parent->key;
else
cout<<"\n There is no parent of the node. ";
if(p->right!=NULL)
cout<<"\n Right Child: "<<p->right->key;
else
cout<<"\n There is no right child of the node. ";
if(p->left!=NULL)
cout<<"\n Left Child: "<<p->left->key;
else
cout<<"\n There is no left child of the node. ";
cout<<endl;
if(p->left)
{
cout<<"\n\nLeft:\n";
display(p->left);
}
/*else
cout<<"\nNo Left Child.\n";*/
if(p->right)
{ EC
cout<<"\n\nRight:\n";
display(p->right);
}
/*else
cout<<"\nNo Right Child.\n"*/
}
}
R
void RBtree::search()
{
if(root==NULL)
{
TK
cout<<"\nEmpty Tree\n" ;
return ;
}
int x;
cout<<"\n Enter key of the node to be searched: ";
cin>>x;
node *p=root;
int found=0;
while(p!=NULL&& found==0)
{
if(p->key==x)
found=1;
if(found==0)
{
if(p->key<x)
p=p->right;
else
p=p->left;
}
}
if(found==0)
cout<<"\nElement Not Found.";
else
{
cout<<"\n\t FOUND NODE: ";
cout<<"\n Key: "<<p->key;
cout<<"\n Colour: ";
if(p->color=='b')
cout<<"Black";
else
cout<<"Red";
if(p->parent!=NULL)
cout<<"\n Parent: "<<p->parent->key;
else
cout<<"\n There is no parent of the node. ";
if(p->right!=NULL)
cout<<"\n Right Child: "<<p->right->key;
else
cout<<"\n There is no right child of the node. ";
if(p->left!=NULL)
cout<<"\n Left Child: "<<p->left->key;
else
cout<<"\n There is no left child of the node. ";
cout<<endl;
}
}
int main()
EC
{
int ch,y=0;
RBtree obj;
do
R
{
cout<<"\n\t RED BLACK TREE " ;
cout<<"\n 1. Insert in the tree ";
cout<<"\n 2. Delete a node from the tree";
TK
}while(y!=1);
return 1;
}
Out put:
EC
R
TK
#include<iostream>
#include<conio.h>
#include<stdlib.h>
# define max 10
int data;
struct list *next;
} node_type;
node_type *ptr[max],*root[max],*temp[max];
class Dictionary
public:
int index;
Dictionary();
void insert(int);
void search(int);
EC
void delete_ele(int);
};
Dictionary::Dictionary()
R
{
index=-1;
TK
root[i]=NULL;
ptr[i]=NULL;
temp[i]=NULL;
index=int(key%max);
ptr[index]=(node_type*)malloc(sizeof(node_type));
ptr[index]->data=key;
if(root[index]==NULL)
root[index]=ptr[index];
root[index]->next=NULL;
temp[index]=ptr[index];
else
{ EC
temp[index]=root[index];
while(temp[index]->next!=NULL)
temp[index]=temp[index]->next;
R
temp[index]->next=ptr[index];
}
TK
int flag=0;
index=int(key%max);
temp[index]=root[index];
while(temp[index]!=NULL)
if(temp[index]->data==key)
{
cout<<"\nSearch key is found!!";
flag=1;
break;
else temp[index]=temp[index]->next;
if (flag==0)
index=int(key%max);
temp[index]=root[index];
R
while(temp[index]->data!=key && temp[index]!=NULL)
{
TK
ptr[index]=temp[index];
temp[index]=temp[index]->next;
ptr[index]->next=temp[index]->next;
temp[index]->data=-1;
temp[index]=NULL;
free(temp[index]);
main()
{
int val,ch,n,num;
char c;
Dictionary d;
do
cout<<"\nMENU:\n1.Create";
cin>>ch;
EC
switch(ch)
case 1:
R
cout<<"\nEnter the number of elements to be inserted:";
cin>>n;
TK
cin>>num;
d.insert(num);
break;
case 2:
cin>>n;
d.search(n);
case 3:
cin>>n;
d.delete_ele(n);
break;
default:
cout<<"\nInvalid Choice.";
cout<<"\nEnter y to Continue:";
EC
cin>>c;
while(c=='y');
R
getch();
}
TK
OUTPUT:
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:1
Enter y to Continue:y
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:2
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:
79. }
80. }
81. }
82.
83. // Driver program to test above function
84. int main()
85. {
86. char *txt = "ABABDABACDABABCABAB";
87. char *pat = "ABABCABAB";
88. KMPSearch(pat, txt);
89. return 0;
90. }
Output
1. # include <limits.h>
2. # include <string.h>
3. # include <stdio.h>
4.
5. # define NO_OF_CHARS 256
6.
7. // A utility function to get maximum of two integers
8. int max(int a, int b)
9. {
10. return (a > b) ? a : b;
11. }
12.
13. // The preprocessing function for Boyer Moore's bad
character heuristic
14. void badCharHeuristic(char *str, int size, int
badchar[NO_OF_CHARS])
15. {
16. int i;
17.
18. // Initialize all occurrences as -1
19. for (i = 0; i < NO_OF_CHARS; i++)
20. badchar[i] = -1;
21.
22. // Fill the actual value of last occurrence of a
character
23. for (i = 0; i < size; i++)
24. badchar[(int) str[i]] = i;
25. }
26.
27. void search(char *txt, char *pat)
28. {
29. int m = strlen(pat);
30. int n = strlen(txt);
31.
32. int badchar[NO_OF_CHARS];
33.
34. badCharHeuristic(pat, m, badchar);
35. EC
36. int s = 0; // s is shift of the pattern with respect to
text
37. while (s <= (n - m))
38. {
39. int j = m - 1;
40.
41. while (j >= 0 && pat[j] == txt[s + j])
42. j--;
R
43.
44. if (j < 0)
45. {
46. printf("\n pattern occurs at shift = %d", s);
TK
47.
48. s += (s + m < n) ? m - badchar[txt[s + m]] : 1;
49.
50. }
51.
52. else
53. s += max(1, j - badchar[txt[s + j]]);
54. }
55. }
56.
57. /* Driver program to test above funtion */
58. int main()
59. {
60. char txt[] = "ABAAABCD";
61. char pat[] = "ABC";
62. search(txt, pat);
63. return 0;
64. }
Output:
$ g++ Boyer-Moore.cpp
$ a.out
EC
R
TK