Professional Documents
Culture Documents
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
struct tree {
int info;
struct tree *left;
struct tree *right;
};
int main(void) {
struct tree *root;
int choice, item,item_no;
root = NULL;
do {
do {
printf("\n \t 1. Insert in Binary Tree ");
printf("\n\t 2. Delete from Binary Tree ");
printf("\n\t 3. Inorder traversal of Binary tree");
printf("\n\t 4. Postorder traversal of Binary
tree");
1|Page
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
inorder(root); break;
case 4:
printf("\n Postorder traversal of binary tree is : ");
postorder(root); break;
case 5:
printf("\n Preorder traversal of binary tree is : ");
preorder(root); break;
2|Page
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
case 6:
printf("\n Search operation in binary tree ");
root=search(root); break;
default:
printf("\n End of program ");
}
}while(choice !=7);
return(0);
}
void inorder(struct tree *root) {
if(root != NULL) {
inorder(root->left);
printf(" %d",root->info);
inorder(root->right);
}
}
void postorder(struct tree *root) {
if(root != NULL) {
postorder(root->left);
postorder(root->right);
printf(" %d",root->info);
}
}
void preorder(struct tree *root) {
if(root != NULL) {
printf(" %d",root->info);
preorder(root->left);
preorder(root->right);
}
}
3|Page
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
4|Page
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
if(ptr->left == ptr->right)
/*i.e., a leaf node*/ {
free(ptr);
return(NULL);
}
else if(ptr->left==NULL)
/* a right subtree */ {
p1=ptr->right;
free(ptr);
return p1;
}
else if(ptr->right==NULL)
/* a left subtree */ {
p1=ptr->left;
free(ptr);
return p1;
}
else {
p1=ptr->right;
p2=ptr->right;
while(p1->left != NULL)
p1=p1->left;
p1->left=ptr->left;
free(ptr);
return p2;
}
}
}
}
return(ptr);
}
5|Page
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
while(ptr) {
parent = ptr;
l_child = ptr->left;
r_child = ptr->right;
if(no>ptr->info)
ptr=ptr->right;
else if(no<ptr->info)
ptr=ptr->left;
else
break;
}
if(ptr->left == NULL)
printf("\n Element %d which was searched is found and its
left child is NULL and right child is %d",no, r_child->info);
if(ptr->left == NULL)
printf("\n Element %d which was searched is found and its
left child is %d and right child is NULL",no, l_child->info);
}
else
printf("\n Element %d does not exist in the binary tree",no);
return(root);
}
6|Page
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
i = 0; j = 0;
k = l;
7|Page
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
8|Page
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);
9|Page
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
10 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
int main()
{
int arr[] = {10, 20, 15, 17, 9, 21};
int n = sizeof(arr) / sizeof(arr[0]);
printf("\n\n");
heapSort(arr, n);
return 0;
}
11 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
13 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
class BTreeNode
{
int *keys;
int t;
BTreeNode **C;
int n;
bool leaf;
public:
BTreeNode(int _t, bool _leaf);
void traverse();
BTreeNode *search(int k);
int findKey(int k);
void insertNonFull(int k);
void splitChild(int i, BTreeNode *y);
void remove(int k);
void removeFromLeaf(int idx);
void removeFromNonLeaf(int idx);
int getPred(int idx);
int getSucc(int idx);
void fill(int idx);
void borrowFromPrev(int idx);
void borrowFromNext(int idx);
void merge(int idx);
14 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
class BTree
{
BTreeNode *root;
int t;
public:
BTree(int _t) {
root = NULL;
t = _t;
}
void traverse() {
if (root != NULL) root->traverse();
}
BTreeNode* search(int k) {
return (root == NULL)? NULL : root->search(k);
}
void insert(int k);
void remove(int k);
};
BTreeNode::BTreeNode(int t1, bool leaf1) {
t = t1; leaf = leaf1;
keys = new int[2*t-1];
C = new BTreeNode *[2*t];
n = 0;
}
int BTreeNode::findKey(int k) {
int idx=0;
while (idx<n && keys[idx] < k)
++idx;
return idx;
}
15 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
16 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
17 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
if (idx != n)
merge(idx);
else
merge(idx-1);
}
return;
}
void BTreeNode::borrowFromPrev(int idx){
BTreeNode *child=C[idx];
BTreeNode *sibling=C[idx-1];
for (int i=child->n-1; i>=0; --i)
child->keys[i+1] = child->keys[i];
if (!child->leaf) {
for(int i=child->n; i>=0; --i)
child->C[i+1] = child->C[i];
}
child->keys[0] = keys[idx-1];
if(!child->leaf)
child->C[0] = sibling->C[sibling->n];
keys[idx-1] = sibling->keys[sibling->n-1];
child->n += 1;
sibling->n -= 1;
return;
}
void BTreeNode::borrowFromNext(int idx) {
BTreeNode *child=C[idx];
BTreeNode *sibling=C[idx+1];
child->keys[(child->n)] = keys[idx];
if (!(child->leaf))
child->C[(child->n)+1] = sibling->C[0];
18 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
keys[idx] = sibling->keys[0];
for (int i=1; i<sibling->n; ++i)
sibling->keys[i-1] = sibling->keys[i];
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;
}
void BTreeNode::merge(int idx) {
BTreeNode *child = C[idx];
BTreeNode *sibling = C[idx+1];
child->keys[t-1] = keys[idx];
for (int i=0; i<sibling->n; ++i)
child->keys[i+t] = sibling->keys[i];
if (!child->leaf) {
for(int i=0; i<=sibling->n; ++i)
child->C[i+t] = sibling->C[i];
}
for (int i=idx+1; i<n; ++i)
keys[i-1] = keys[i];
for (int i=idx+2; i<=n; ++i)
C[i-1] = C[i];
child->n += sibling->n+1;
n--;
delete(sibling);
return;
}
19 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
20 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
else {
while (i >= 0 && keys[i] > k)
i--;
if (C[i+1]->n == 2*t-1) {
splitChild(i+1, C[i+1]);
if (keys[i+1] < k)
i++;
}
C[i+1]->insertNonFull(k);
}
}
void BTreeNode::splitChild(int i, BTreeNode *y) {
BTreeNode *z = new BTreeNode(y->t, y->leaf);
z->n = t - 1;
for (int j = 0; j < t-1; j++)
z->keys[j] = y->keys[j+t];
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;
for (int j = n-1; j >= i; j--)
keys[j+1] = keys[j];
keys[i] = y->keys[t-1];
n = n + 1;
}
21 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
void BTreeNode::traverse() {
int i;
for (i = 0; i < n; i++) {
if (leaf == false)
C[i]->traverse();
cout << " " << keys[i];
}
if (leaf == false) C[i]->traverse();
}
BTreeNode *BTreeNode::search(int k) {
int i = 0;
while (i < n && k > keys[i])
i++;
if (keys[i] == k) return this;
if (leaf == true) return NULL;
return C[i]->search(k);
}
void BTree::remove(int k) {
if (!root) { cout << "The tree is empty\n"; return; }
root->remove(k);
if (root->n==0) {
BTreeNode *tmp = root;
if (root->leaf)
root = NULL;
else
root = root->C[0];
delete tmp;
}
return;
}
22 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
int main()
{
BTree t(3);
t.insert(1); t.insert(3); t.insert(7); t.insert(10);
t.insert(11); t.insert(13); t.insert(14); t.insert(15);
t.insert(18); t.insert(16); t.insert(19); t.insert(24);
t.insert(25); t.insert(26); t.insert(21); t.insert(4);
t.insert(5); t.insert(20); t.insert(22); t.insert(2);
t.insert(17); t.insert(12); t.insert(6);
cout << "Traversal of tree constructed is\n";
t.traverse(); cout << endl; t.remove(6);
cout << "Traversal of tree after removing 6\n";
t.traverse(); cout << endl; t.remove(13);
cout << "Traversal of tree after removing 13\n";
t.traverse(); cout << endl; t.remove(7);
cout << "Traversal of tree after removing 7\n";
t.traverse(); cout << endl; t.remove(4);
cout << "Traversal of tree after removing 4\n";
t.traverse(); cout << endl; t.remove(2);
cout << "Traversal of tree after removing 2\n";
t.traverse(); cout << endl; t.remove(16);
cout << "Traversal of tree after removing 16\n";
t.traverse(); cout << endl;
return 0;
}
23 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
Output:
Traversal of tree constructed is
1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26
Traversal of tree after removing 6
1 2 3 4 5 7 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26
Traversal of tree after removing 13
1 2 3 4 5 7 10 11 12 14 15 16 17 18 19 20 21 22 24 25 26
Traversal of tree after removing 7
1 2 3 4 5 10 11 12 14 15 16 17 18 19 20 21 22 24 25 26
Traversal of tree after removing 4
1 2 3 5 10 11 12 14 15 16 17 18 19 20 21 22 24 25 26
Traversal of tree after removing 2
1 3 5 10 11 12 14 15 16 17 18 19 20 21 22 24 25 26
Traversal of tree after removing 16
1 3 5 10 11 12 14 15 17 18 19 20 21 22 24 25 26
24 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
class MinHeap {
int *harr;
int capacity;
int heap_size;
public:
MinHeap(int capacity);
void MinHeapify(int );
int parent(int i)
{ return (i-1)/2; }
int left(int i)
{ return (2*i + 1); }
int right(int i)
{ return (2*i + 2); }
int extractMin();
void decreaseKey(int i, int new_val);
int getMin()
{ return harr[0]; }
void deleteKey(int i);
void insertKey(int k);
};
MinHeap::MinHeap(int cap) {
heap_size = 0; capacity = cap; harr = new int[cap];
}
25 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
void MinHeap::insertKey(int k) {
if (heap_size == capacity) {
cout << "\nOverflow: Could not insertKey\n"; return;
}
heap_size++;
int i = heap_size - 1; harr[i] = k;
while (i != 0 && harr[parent(i)] > harr[i]) {
swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
}
void MinHeap::decreaseKey(int i, int new_val){
harr[i] = new_val;
while (i != 0 && harr[parent(i)] > harr[i]) {
swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
}
int MinHeap::extractMin()
{
if (heap_size <= 0) return INT_MAX;
if (heap_size == 1) {
heap_size--; return harr[0];
}
int root = harr[0];
harr[0] = harr[heap_size-1];
heap_size--;
MinHeapify(0);
return root;
}
26 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
void MinHeap::deleteKey(int i) {
decreaseKey(i, INT_MIN); extractMin();
}
void MinHeap::MinHeapify(int i) {
int l = left(i); int r = right(i);
int smallest = i;
if (l < heap_size && harr[l] < harr[i])
smallest = l;
if (r < heap_size && harr[r] < harr[smallest])
smallest = r;
if (smallest != i) {
swap(&harr[i], &harr[smallest]);
MinHeapify(smallest);
}
}
void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; }
int main()
{
MinHeap h(11);
h.insertKey(3); h.insertKey(2); h.deleteKey(1);
h.insertKey(15); h.insertKey(5);
h.insertKey(4); h.insertKey(45);
cout << h.extractMin() << " ";
cout << h.getMin() << " ";
h.decreaseKey(2, 1);
cout << h.getMin();
return 0;
}
Output:
2 4 1
27 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
28 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
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);
};
LeftistHeap::LeftistHeap() { root = NULL; }
LeftistHeap::LeftistHeap(LeftistHeap &rhs) {
root = NULL;
*this = rhs;
}
LeftistHeap::~LeftistHeap() { makeEmpty( ); }
void LeftistHeap::Merge(LeftistHeap &rhs) {
if (this == &rhs) return;
root = Merge(root, rhs.root);
rhs.root = NULL;
}
LeftistNode *LeftistHeap::Merge(LeftistNode * h1,LeftistNode * h2){
if (h1 == NULL) return h2;
if (h2 == NULL) return h1;
if (h1->element < h2->element)
return Merge1(h1, h2);
else
return Merge1(h2, h1);
}
29 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
30 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
bool LeftistHeap::isEmpty()
{ return root == NULL; }
bool LeftistHeap::isFull()
{ return false; }
void LeftistHeap::makeEmpty() {
reclaimMemory(root); root = NULL;
}
LeftistHeap &LeftistHeap::operator =(LeftistHeap & rhs){
if (this != &rhs) {
makeEmpty(); root = clone(rhs.root);
}
return *this;
}
void LeftistHeap::reclaimMemory(LeftistNode * t) {
if (t != NULL) {
reclaimMemory(t->left); reclaimMemory(t->right);
delete t;
}
}
LeftistNode *LeftistHeap::clone(LeftistNode * t) {
if (t == NULL) return NULL;
else
return new LeftistNode(t->element, clone(t->left),
clone(t->right), t->dist);
}
int main(){
LeftistHeap h;
LeftistHeap h1;
LeftistHeap h2;
int x;
31 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
32 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
33 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
34 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
35 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
36 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
Output:
The heap is:
50 10 30 40 20
After deleing 10, the heap is:
20 30 40 50
37 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
38 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
39 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
case 0 :
root -> balfact = -1 ;
break ;
case -1 :
node1 = root -> right ;
if ( node1 -> balfact == -1 )
{
cout << "\nLeft rotation." ;
root -> right = node1 -> left ;
node1 -> left = root ;
root -> balfact = 0 ;
root = node1 ;
}
else
{
cout << "\nDouble rotation, right then
left." ;
node2 = node1 -> left;
node1-> left = node2 -> right;
node2-> right = node1;
root -> right = node2 -> left ;
node2 -> left = root ;
if ( node2 -> balfact == -1 )
root -> balfact = 1 ;
else
root -> balfact = 0 ;
if ( node2 -> balfact == 1 )
node1 -> balfact = -1 ;
else
node1 -> balfact = 0 ;
root = node2 ;
}
root -> balfact = 0 ;
*h = FALSE ;
}
}
}
return ( root ) ;
}
void avltree :: display ( AVLNode* root ){
if ( root != NULL ){
display ( root -> left ) ; cout << root -> data << "\t" ;
display ( root -> right ) ;
}
}
AVLNode* avltree :: deldata (AVLNode *root,int data,int *h )
{
AVLNode *node ;
if ( root -> data == 13 )
cout << root -> data ;
40 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
if ( root == NULL ){
cout << "\nNo such data." ;
return ( root ) ;
}
else {
if ( data < root -> data ){
root->left = deldata(root->left, data, h);
if ( *h )
root = balright ( root, h ) ;
}
else {
if ( data > root -> data ) {
root->right=deldata(root->right,data,h);
if ( *h )
root = balleft ( root, h ) ;
}
else {
node = root ;
if ( node -> right == NULL ) {
root = node -> left ;
*h = TRUE ; delete ( node ) ;
}
else {
if ( node -> left == NULL ) {
root = node -> right ;
*h = TRUE ; delete ( node ) ;
}
else {
node->right=del(node->right,node,h);
if ( *h )
root = balleft(root, h);
}
}
}
}
}
return ( root ) ;
}
AVLNode* avltree :: del (AVLNode *succ,AVLNode *node, int *h) {
AVLNode *temp = succ ;
if ( succ -> left != NULL ) {
succ -> left = del ( succ -> left, node, h ) ;
if ( *h ) succ = balright ( succ, h ) ;
}
else {
temp = succ ; node -> data = succ -> data ;
succ = succ -> right ; delete ( temp ) ; *h = TRUE ;
}
return ( succ ) ;
}
41 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
42 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
43 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
avltree :: ~avltree( )
{
deltree ( root ) ;
}
void avltree :: deltree ( AVLNode *root )
{
if ( root != NULL )
{
deltree ( root -> left ) ;
deltree ( root -> right ) ;
}
delete ( root ) ;
}
void main( )
{
avltree at ;
AVLNode *avl = NULL ;
int h ;
clrscr();
44 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
45 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
46 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
else {
parent->right = nParent;
}
}
nParent->parent = parent;
parent = nParent;
}
bool hasRedChild() {
return (left != NULL and left->color == RED) or
(right != NULL and right->color == RED);
}
};
class RBTree {
Node *root;
void 7(Node *x) {
Node *nParent = x->right;
if (x == root)
root = nParent;
x->moveDown(nParent);
x->right = nParent->left;
if (nParent->left != NULL)
nParent->left->parent = x;
nParent->left = x;
}
void rightRotate(Node *x) {
Node *nParent = x->left;
if (x == root)
root = nParent;
x->moveDown(nParent);
47 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
x->left = nParent->right;
if (nParent->right != NULL)
nParent->right->parent = x;
nParent->right = x;
}
void swapColors(Node *x1, Node *x2) {
COLOR temp;
temp = x1->color;
x1->color = x2->color;
x2->color = temp;
}
void swapValues(Node *u, Node *v) {
int temp;
temp = u->val;
u->val = v->val;
v->val = temp;
}
void fixRedRed(Node *x) {
if (x == root) {
x->color = BLACK;
return;
}
Node *parent = x->parent, *grandparent = parent->parent,
*uncle = x->uncle();
if (parent->color != BLACK) {
if (uncle != NULL && uncle->color == RED) {
parent->color = BLACK;
uncle->color = BLACK;
grandparent->color = RED;
fixRedRed(grandparent);
48 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
} else {
if (parent->isOnLeft()) {
if (x->isOnLeft()) {
swapColors(parent, grandparent);
} else {
leftRotate(parent);
swapColors(x, grandparent);
}
rightRotate(grandparent);
} else {
if (x->isOnLeft()) {
rightRotate(parent);
swapColors(x, grandparent);
} else {
swapColors(parent, grandparent);
}
leftRotate(grandparent);
}
}
}
}
Node *successor(Node *x) {
Node *temp = x;
while (temp->left != NULL)
temp = temp->left;
return temp;
}
Node *BSTreplace(Node *x) {
if (x->left != NULL and x->right != NULL)
return successor(x->right);
49 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
50 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
51 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
} else {
if (sibling->color == RED) {
parent->color = RED;
sibling->color = BLACK;
if (sibling->isOnLeft()) {
rightRotate(parent);
} else {
leftRotate(parent);
}
fixDoubleBlack(x);
} else {
if (sibling->hasRedChild()) {
if (sibling->left != NULL and sibling->left->color==RED)
{
if (sibling->isOnLeft()) {
sibling->left->color = sibling->color;
sibling->color = parent->color;
rightRotate(parent);
} else {
sibling->left->color = parent->color;
rightRotate(sibling);
leftRotate(parent);
}
} else {
if (sibling->isOnLeft()) {
sibling->right->color = parent->color;
leftRotate(sibling);
rightRotate(parent);
} else {
sibling->right->color = sibling->color;
52 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
sibling->color = parent->color;
leftRotate(parent);
}
}
parent->color = BLACK;
} else {
sibling->color = RED;
if (parent->color == BLACK)
fixDoubleBlack(parent);
else parent->color = BLACK;
}
}
}
}
void levelOrder(Node *x) {
if (x == NULL)
return;
queue<Node *> q;
Node *curr;
q.push(x);
while (!q.empty()) {
// while q is not empty
// dequeue
curr = q.front();
q.pop();
53 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
if (curr->left != NULL)
q.push(curr->left);
if (curr->right != NULL)
q.push(curr->right);
}
}
public:
// constructor
// initialize root
RBTree() { root = NULL; }
54 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
if (temp->left == NULL)
break;
else
temp = temp->left;
} else if (n == temp->val) {
break;
} else {
if (temp->right == NULL)
break;
else
temp = temp->right;
}
}
return temp;
}
if (temp->val == n) {
// return if value already exists
55 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
return;
}
if (n < temp->val)
temp->left = newNode;
else
temp->right = newNode;
if (v->val != n) {
cout << "No node found to delete with value:" << n << endl;
return;
56 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
deleteNode(v);
}
int main() {
RBTree tree;
tree.insert(7);
57 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
tree.deleteByVal(18); tree.deleteByVal(11);
tree.deleteByVal(3); tree.deleteByVal(10);
tree.deleteByVal(22);
tree.printInOrder();
tree.printLevelOrder();
return 0;
}
58 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
Output:
Inorder:
2 3 6 7 8 10 11 13 18 22 26
Level order:
10 7 18 3 8 11 22 2 6 13 26
59 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
#include <iostream>
using namespace std;
const int MAX=10;
class dictionary;
class node
{
string key,value;
node *next;
public:
friend class dictionary;
node()
{ next=NULL; }
node(string key,string value)
{
this->key=key;
this->value=value;
next=NULL;
}
};
class dictionary
{
node *head[MAX];
public:
dictionary(){
for(int i=0;i<MAX;i++)
head[i]=NULL;
60 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
}
int hashf(string word);
void insert(string,string);
void find(string word);
bool deleteWord(string word);
void display();
};
if(start->key==word)
{
flag=1;
break;
}
61 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
start=start->next;
}
if(flag==1)
cout<<"Word Is present.";
else
cout<<"Word Is not present.";
}
if(head[index]==NULL)
{
head[index]=p;
}
else
{
node *start=head[index];
while(start->next!=NULL)
start=start->next;
start->next=p;
}
62 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
{
int index=hashf(word);
node *tmp=head[index];
node *par=head[index];
if(tmp==NULL) //if no word is present at that index
{
return false;
}
if(tmp->key==word && tmp->next==NULL)
//only one word is present
{
head[index]=NULL;
delete tmp;
return true;
}
//tmp=tmp->next;
while(tmp->key!=word && tmp->next!=NULL)
{
par=tmp;
tmp=tmp->next;
}
if(tmp->key==word&&tmp->next!=NULL)
{
if(par->key==tmp->key)
{ head[index]=tmp->next; }
else
{
par->next=tmp->next;
tmp->next=NULL;
}
delete tmp;
63 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
return true;
}
else //delete at end
{
par->next=NULL;
tmp->next=NULL;
delete tmp;
return true;
}
return false;
}
int main() {
dictionary oxford;
64 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
int choice;
string word,meaning;
do
{
cout<<"\n**** OXFORD DICTIONARY ****\n"
<<"1.Insert Word\n"
<<"2.Find Word\n"
<<"3.Delete Word\n"
<<"4.Display\n"
<<"Enter Your Choice :";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter Word: ";
cin>>word;
cout<<"Enter Meaning: ";
cin>>meaning;
oxford.insert(word,meaning); break;
case 2:
cout<<"Enter Word to Search: ";
cin>>word;
oxford.find(word); break;
case 3:
cout<<"Enter Word to Delete: ";
cin>>word;
if(oxford.deleteWord(word))
cout<<" Word is deleted.";
65 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
else
{
cout<<"\nFailed to delete "<<word;
}
break;
case 4:
cout<<"***Oxford Dictionary***";
oxford.display();
break;
default:
cout<<"\nWrong Choice.";
}
}while(choice!=0);
return 0;
}
66 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
OUTPUT:
**** OXFORD DICTIONARY ****
1.Insert Word
2.Find Word
3.Delete Word
4.Display
Enter Your Choice :1
Enter Word: a
Enter Meaning: ant
67 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
Word Is present.
**** OXFORD DICTIONARY ****
1.Insert Word
2.Find Word
3.Delete Word
4.Display
Enter Your Choice :2
Enter Word to Search: c
Word Is not present.
**** OXFORD DICTIONARY ****
1.Insert Word
2.Find Word
3.Delete Word
4.Display
Enter Your Choice :4
***Oxford Dictionary***
Index Key Value
:7 a ant
:8 b bat
68 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
1.Insert Word
2.Find Word
3.Delete Word
4.Display
Enter Your Choice :3
Enter Word to Delete: c
Failed to delete c
**** OXFORD DICTIONARY ****
1.Insert Word
2.Find Word
3.Delete Word
4.Display
Enter Your Choice :3
Enter Word to Delete: b
Word is deleted.
**** OXFORD DICTIONARY ****
1.Insert Word
2.Find Word
3.Delete Word
4.Display
Enter Your Choice :
69 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
computeLPSArray(pat, M, lps);
int i = 0; int j = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++; i++;
}
if (j == M) {
printf("Found pattern at index %d ", i - j);
j = lps[j - 1];
}
else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
70 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
int main() {
char txt[] = "ABABDABACDABABCABAB";
char pat[] = "ABABCABAB";
KMPSearch(pat, txt); return 0;
}
Output:
71 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
Output:
Pattern found at index 0
72 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
# include <limits.h>
# include <string.h>
# include <stdio.h>
# define NO_OF_CHARS 256
int max (int a, int b) { return (a > b)? a: b; }
73 | P a g e
M Tech CS (PTPG) Sem – I Advanced Data Structures Lab Record
if (j < 0) {
printf("\n pattern occurs at shift = %d", s);
s += (s+m < n)? m-badchar[txt[s+m]] : 1;
}
else
s += max(1, j - badchar[txt[s+j]]);
}
}
int main()
{
char txt[] = "ABAAABCD";
char pat[] = "ABC";
search(txt, pat);
return 0;
}
Output:
pattern occurs at shift = 4
74 | P a g e