SlideShare a Scribd company logo
//String Length
#include <stdio.h>
void main()
{
char string[50];
int i, length = 0;
printf("Enter a string n");
gets(string);
for (i = 0; string[i] != '0'; i++)
{
length++;
}
printf("The length of %s = %dn", string, length);
}
// String Concatenation
#include<stdio.h>
void main(void)
{
char str1[25],str2[25];
int i=0, j=0;
printf("nEnter First String:");
gets(str1);
printf("nEnter Second String:");
gets(str2);
while(str1[i]!='0')
i++;
while(str2[j]!='0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='0';
printf("nConcatenated String is %s",str1);
}
//Pattern Matching
# include<stdio.h>
void main()
{
char str[80], search[10];
int count1 = 0, count2 = 0, i, j, flag;
printf("Enter a string:");
gets(str);
printf("Enter search substring:");
gets(search);
while (str[count1] != '0')
count1++;
while (search[count2] != '0')
count2++;
for (i = 0; i <= count1 - count2; i++)
{
for (j = i; j < i + count2; j++)
{
flag = 1;
if (str[j] != search[j - i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1)
printf("SEARCH SUCCESSFUL!");
else
printf("SEARCH UNSUCCESSFUL!");
}
// Program to Implement B+ Tree
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
struct BTreeNode
{
int *data;
BTreeNode **child_ptr;
bool leaf;
int n;
}*root = NULL, *np = NULL, *x = NULL;
BTreeNode * init()
{
int i;
np = new BTreeNode;
np->data = new int[5];
np->child_ptr = new BTreeNode *[6];
np->leaf = true;
np->n = 0;
for (i = 0; i < 6; i++)
{
np->child_ptr[i] = NULL;
}
return np;
}
void traverse(BTreeNode *p)
{
cout<<endl;
int i;
for (i = 0; i < p->n; i++)
{
if (p->leaf == false)
{
traverse(p->child_ptr[i]);
}
cout << " " << p->data[i];
}
if (p->leaf == false)
{
traverse(p->child_ptr[i]);
}
cout<<endl;
}
void sort(int *p, int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = i; j <= n; j++)
{
if (p[i] > p[j])
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
}
int split_child(BTreeNode *x, int i)
{
int j, mid;
BTreeNode *np1, *np3, *y;
np3 = init();
np3->leaf = true;
if (i == -1)
{
mid = x->data[2];
x->data[2] = 0;
x->n--;
np1 = init();
np1->leaf = false;
x->leaf = true;
for (j = 3; j < 5; j++)
{
np3->data[j - 3] = x->data[j];
np3->child_ptr[j - 3] = x->child_ptr[j];
np3->n++;
x->data[j] = 0;
x->n--;
}
for(j = 0; j < 6; j++)
{
x->child_ptr[j] = NULL;
}
np1->data[0] = mid;
np1->child_ptr[np1->n] = x;
np1->child_ptr[np1->n + 1] = np3;
np1->n++;
root = np1;
}
else
{
y = x->child_ptr[i];
mid = y->data[2];
y->data[2] = 0;
y->n--;
for (j = 3; j < 5; j++)
{
np3->data[j - 3] = y->data[j];
np3->n++;
y->data[j] = 0;
y->n--;
}
x->child_ptr[i + 1] = y;
x->child_ptr[i + 1] = np3;
}
return mid;
}
void insert(int a)
{
int i, temp;
x = root;
if (x == NULL)
{
root = init();
x = root;
}
else
{
if (x->leaf == true && x->n == 5)
{
temp = split_child(x, -1);
x = root;
for (i = 0; i < (x->n); i++)
{
if ((a > x->data[i]) && (a < x->data[i + 1]))
{
i++;
break;
}
else if (a < x->data[0])
{
break;
}
else
{
continue;
}
}
x = x->child_ptr[i];
}
else
{
while (x->leaf == false)
{
for (i = 0; i < (x->n); i++)
{
if ((a > x->data[i]) && (a < x->data[i + 1]))
{
i++;
break;
}
else if (a < x->data[0])
{
break;
}
else
{
continue;
}
}
if ((x->child_ptr[i])->n == 5)
{
temp = split_child(x, i);
x->data[x->n] = temp;
x->n++;
continue;
}
else
{
x = x->child_ptr[i];
}
}
}
}
x->data[x->n] = a;
sort(x->data, x->n);
x->n++;
}
int main()
{
int i, n, t;
cout<<"enter the no of elements to be insertedn";
cin>>n;
for(i = 0; i < n; i++)
{
cout<<"enter the elementn";
cin>>t;
insert(t);
}
cout<<"traversal of constructed treen";
traverse(root);
getch();
}
//Threaded Binary Tree
#include <iostream>
#include <cstdlib>
#define MAX_VALUE 65536
using namespace std;
/* Class Node */
class Node
{
public:
int key;
Node *left, *right;
bool leftThread, rightThread;
};
/* Class ThreadedBinarySearchTree */
class ThreadedBinarySearchTree
{
private:
Node *root;
public:
/* Constructor */
ThreadedBinarySearchTree()
{
root = new Node();
root->right = root->left = root;
root->leftThread = true;
root->key = MAX_VALUE;
}
/* Function to clear tree */
void makeEmpty()
{
root = new Node();
root->right = root->left = root;
root->leftThread = true;
root->key = MAX_VALUE;
}
/* Function to insert a key */
void insert(int key)
{
Node *p = root;
for (;;)
{
if (p->key < key)
{
if (p->rightThread)
break;
p = p->right;
}
else if (p->key > key)
{
if (p->leftThread)
break;
p = p->left;
}
else
{
/* redundant key */
return;
}
}
Node *tmp = new Node();
tmp->key = key;
tmp->rightThread = tmp->leftThread = true;
if (p->key < key)
{
/* insert to right side */
tmp->right = p->right;
tmp->left = p;
p->right = tmp;
p->rightThread = false;
}
else
{
tmp->right = p;
tmp->left = p->left;
p->left = tmp;
p->leftThread = false;
}
}
/* Function to search for an element */
bool search(int key)
{
Node *tmp = root->left;
for (;;)
{
if (tmp->key < key)
{
if (tmp->rightThread)
return false;
tmp = tmp->right;
}
else if (tmp->key > key)
{
if (tmp->leftThread)
return false;
tmp = tmp->left;
}
else
{
return true;
}
}
}
/* Fuction to delete an element */
void Delete(int key)
{
Node *dest = root->left, *p = root;
for (;;)
{
if (dest->key < key)
{
/* not found */
if (dest->rightThread)
return;
p = dest;
dest = dest->right;
}
else if (dest->key > key)
{
/* not found */
if (dest->leftThread)
return;
p = dest;
dest = dest->left;
}
else
{
/* found */
break;
}
}
Node *target = dest;
if (!dest->rightThread && !dest->leftThread)
{
/* dest has two children*/
p = dest;
/* find largest node at left child */
target = dest->left;
while (!target->rightThread)
{
p = target;
target = target->right;
}
/* using replace mode*/
dest->key = target->key;
}
if (p->key >= target->key)
{
if (target->rightThread && target->leftThread)
{
p->left = target->left;
p->leftThread = true;
}
else if (target->rightThread)
{
Node *largest = target->left;
while (!largest->rightThread)
{
largest = largest->right;
}
largest->right = p;
p->left = target->left;
}
else
{
Node *smallest = target->right;
while (!smallest->leftThread)
{
smallest = smallest->left;
}
smallest->left = target->left;
p->left = target->right;
}
}
else
{
if (target->rightThread && target->leftThread)
{
p->right = target->right;
p->rightThread = true;
}
else if (target->rightThread)
{
Node *largest = target->left;
while (!largest->rightThread)
{
largest = largest->right;
}
largest->right = target->right;
p->right = target->left;
}
else
{
Node *smallest = target->right;
while (!smallest->leftThread)
{
smallest = smallest->left;
}
smallest->left = p;
p->right = target->right;
}
}
}
/* Function to print tree */
void printTree()
{
Node *tmp = root, *p;
for (;;)
{
p = tmp;
tmp = tmp->right;
if (!p->rightThread)
{
while (!tmp->leftThread)
{
tmp = tmp->left;
}
}
if (tmp == root)
break;
cout<<tmp->key<<" ";
}
cout<<endl;
}
};
int main()
{
ThreadedBinarySearchTree tbst;
cout<<"ThreadedBinarySearchTree Testn";
char ch;
int choice, val;
/* Perform tree operations */
do
{
cout<<"nThreadedBinarySearchTree Operationsn";
cout<<"1. Insert "<<endl;
cout<<"2. Delete"<<endl;
cout<<"3. Search"<<endl;
cout<<"4. Clear"<<endl;
cout<<"Enter Your Choice: ";
cin>>choice;
switch (choice)
{
case 1 :
cout<<"Enter integer element to insert: ";
cin>>val;
tbst.insert(val);
break;
case 2 :
cout<<"Enter integer element to delete: ";
cin>>val;
tbst.Delete(val);
break;
case 3 :
cout<<"Enter integer element to search: ";
cin>>val;
if (tbst.search(val) == true)
cout<<"Element "<<val<<" found in the tree"<<endl;
else
cout<<"Element "<<val<<" not found in the tree"<<endl;
break;
case 4 :
cout<<"nTree Clearedn";
tbst.makeEmpty();
break;
default :
cout<<"Wrong Entry n ";
break;
}
/* Display tree */
cout<<"nTree = ";
tbst.printTree();
cout<<"nDo you want to continue (Type y or n): ";
cin>>ch;
}
while (ch == 'Y'|| ch == 'y');
return 0;
}
//To Copy one string into other
#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
gets(s1);
printf("Enter string s2: ");
gets(s2);
for(i = 0; s1[i] != '0'; ++i)
{
s2[i] = s1[i];
}
s2[i] = '0';
printf("String s1: %s", s1);
printf("nString s2: %s", s2);
return 0;
}
// Read from binary file
#include <stdio.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:program.bin","rb")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
for(n = 1; n < 5; ++n)
{
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %dtn2: %dtn3: %d", num.n1, num.n2, num.n3);
}
fclose(fptr);
return 0;
}
//To count number of words
#include<stdio.h>
void main()
{
FILE *p;
char ch;
int w=1;
clrscr();
p=fopen("source","r");
if(p==NULL)
{
printf("file not found");
}
else
{
ch=fgetc(p);
while(ch!=EOF)
{
printf("%c",ch);
if(ch==' '||ch=='n')
{
w++;
}
ch=fgetc(p);
}
printf("nWords in a file are=%d",w);
}
fclose(p);
getch();
}
//writing into a binary file
#include <stdio.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:program.bin","wb")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
for(n = 1; n < 5; ++n)
{
num.n1 = n;
num.n2 = 5n;
num.n3 = 5n + 1;
fwrite(&num, sizeof(struct threeNum), 1, fptr);
}
fclose(fptr);
return 0;

More Related Content

DOCX
C++ adt c++ implementations
DOC
Ds 2 cycle
PDF
6. Generics. Collections. Streams
PPTX
Bank management system project in c++ with graphics
DOCX
Binomial heap
PDF
Data structures lab
PDF
Groovy kind of test
PDF
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
C++ adt c++ implementations
Ds 2 cycle
6. Generics. Collections. Streams
Bank management system project in c++ with graphics
Binomial heap
Data structures lab
Groovy kind of test
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)

What's hot (20)

PDF
Rデバッグあれこれ
PDF
Rのスコープとフレームと環境と
TXT
Hsn code not show
PDF
SWP - A Generic Language Parser
PDF
Pdxpugday2010 pg90
PDF
Javascript
PPTX
DOCX
Data structures
TXT
Ass2 1 (2)
PDF
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
PPTX
Unit 3
PPTX
Super Advanced Python –act1
PDF
c programming
PDF
Implementing virtual machines in go & c 2018 redux
PDF
Building Real Time Systems on MongoDB Using the Oplog at Stripe
PDF
はじめてのGroovy
PDF
Python Programming: Data Structure
PPTX
Python 내장 함수
PDF
Python dictionary : past, present, future
KEY
Programming Haskell Chapter8
Rデバッグあれこれ
Rのスコープとフレームと環境と
Hsn code not show
SWP - A Generic Language Parser
Pdxpugday2010 pg90
Javascript
Data structures
Ass2 1 (2)
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
Unit 3
Super Advanced Python –act1
c programming
Implementing virtual machines in go & c 2018 redux
Building Real Time Systems on MongoDB Using the Oplog at Stripe
はじめてのGroovy
Python Programming: Data Structure
Python 내장 함수
Python dictionary : past, present, future
Programming Haskell Chapter8
Ad

Similar to program on string in java Lab file 2 (3-year) (20)

PDF
Data StructuresPlease I need help completing this c++ program..pdf
PPTX
Binary search tree.pptx
PDF
My C proggram is having trouble in the switch in main. Also the a co.pdf
PDF
This is a c++ binary search program I worked so far but still cant g.pdf
DOCX
Data structure lab on practical computer science.docx
DOCX
Data Structure lab on a practical in computer science
PDF
I have a Programming is Binary Tree Search (BTS) for strings with po.pdf
PDF
Write a C++ program that implements a binary search tree (BST) to man.pdf
PDF
in this assignment you are asked to write a simple driver program an.pdf
PDF
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
PDF
MAINCPP include ltiostreamgt include ltstringgt u.pdf
DOCX
Binary Tree in C++ coding in the data structure
PDF
Write a C program that reads the words the user types at the command.pdf
PDF
This project will implement a simple usernamepassword lookup system.pdf
PPTX
Binary Search Tree
DOCX
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
PDF
Write a program that accepts an arithmetic expression of unsigned in.pdf
DOCX
PDF
Complete DB code following the instructions Implement the D.pdf
PDF
C programming. Answer question only in C code In the eighth part, yo.pdf
Data StructuresPlease I need help completing this c++ program..pdf
Binary search tree.pptx
My C proggram is having trouble in the switch in main. Also the a co.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
Data structure lab on practical computer science.docx
Data Structure lab on a practical in computer science
I have a Programming is Binary Tree Search (BTS) for strings with po.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
in this assignment you are asked to write a simple driver program an.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
Binary Tree in C++ coding in the data structure
Write a C program that reads the words the user types at the command.pdf
This project will implement a simple usernamepassword lookup system.pdf
Binary Search Tree
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
Write a program that accepts an arithmetic expression of unsigned in.pdf
Complete DB code following the instructions Implement the D.pdf
C programming. Answer question only in C code In the eighth part, yo.pdf
Ad

More from Ankit Gupta (20)

PPT
Biometricstechnology in iot and machine learning
PDF
Week2 cloud computing week2
PDF
Week 8 lecture material
PDF
Week 4 lecture material cc (1)
PDF
Week 3 lecture material cc
PDF
Week 1 lecture material cc
PDF
Mod05lec25(resource mgmt ii)
PDF
Mod05lec24(resource mgmt i)
PDF
Mod05lec23(map reduce tutorial)
PDF
Mod05lec22(cloudonomics tutorial)
PDF
Mod05lec21(sla tutorial)
PDF
Lecture29 cc-security4
PDF
Lecture28 cc-security3
PDF
Lecture27 cc-security2
PDF
Lecture26 cc-security1
PDF
Lecture 30 cloud mktplace
PDF
Week 7 lecture material
PDF
Gurukul Cse cbcs-2015-16
PDF
Microprocessor full hand made notes
PPTX
Transfer Leaning Using Pytorch synopsis Minor project pptx
Biometricstechnology in iot and machine learning
Week2 cloud computing week2
Week 8 lecture material
Week 4 lecture material cc (1)
Week 3 lecture material cc
Week 1 lecture material cc
Mod05lec25(resource mgmt ii)
Mod05lec24(resource mgmt i)
Mod05lec23(map reduce tutorial)
Mod05lec22(cloudonomics tutorial)
Mod05lec21(sla tutorial)
Lecture29 cc-security4
Lecture28 cc-security3
Lecture27 cc-security2
Lecture26 cc-security1
Lecture 30 cloud mktplace
Week 7 lecture material
Gurukul Cse cbcs-2015-16
Microprocessor full hand made notes
Transfer Leaning Using Pytorch synopsis Minor project pptx

Recently uploaded (20)

PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Queuing formulas to evaluate throughputs and servers
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
composite construction of structures.pdf
PPTX
Practice Questions on recent development part 1.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Foundation to blockchain - A guide to Blockchain Tech
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Lesson 3_Tessellation.pptx finite Mathematics
Model Code of Practice - Construction Work - 21102022 .pdf
573137875-Attendance-Management-System-original
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Queuing formulas to evaluate throughputs and servers
Strings in CPP - Strings in C++ are sequences of characters used to store and...
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
composite construction of structures.pdf
Practice Questions on recent development part 1.pptx
Internet of Things (IOT) - A guide to understanding
Structs to JSON How Go Powers REST APIs.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf

program on string in java Lab file 2 (3-year)

  • 1. //String Length #include <stdio.h> void main() { char string[50]; int i, length = 0; printf("Enter a string n"); gets(string); for (i = 0; string[i] != '0'; i++) { length++; } printf("The length of %s = %dn", string, length); }
  • 2. // String Concatenation #include<stdio.h> void main(void) { char str1[25],str2[25]; int i=0, j=0; printf("nEnter First String:"); gets(str1); printf("nEnter Second String:"); gets(str2); while(str1[i]!='0') i++; while(str2[j]!='0') { str1[i]=str2[j]; j++; i++; } str1[i]='0'; printf("nConcatenated String is %s",str1); }
  • 3. //Pattern Matching # include<stdio.h> void main() { char str[80], search[10]; int count1 = 0, count2 = 0, i, j, flag; printf("Enter a string:"); gets(str); printf("Enter search substring:"); gets(search); while (str[count1] != '0') count1++; while (search[count2] != '0') count2++; for (i = 0; i <= count1 - count2; i++) { for (j = i; j < i + count2; j++) { flag = 1; if (str[j] != search[j - i]) { flag = 0; break; }
  • 4. } if (flag == 1) break; } if (flag == 1) printf("SEARCH SUCCESSFUL!"); else printf("SEARCH UNSUCCESSFUL!"); }
  • 5. // Program to Implement B+ Tree #include<stdio.h> #include<conio.h> #include<iostream> using namespace std; struct BTreeNode { int *data; BTreeNode **child_ptr; bool leaf; int n; }*root = NULL, *np = NULL, *x = NULL; BTreeNode * init() { int i; np = new BTreeNode; np->data = new int[5]; np->child_ptr = new BTreeNode *[6]; np->leaf = true; np->n = 0; for (i = 0; i < 6; i++) { np->child_ptr[i] = NULL; } return np;
  • 6. } void traverse(BTreeNode *p) { cout<<endl; int i; for (i = 0; i < p->n; i++) { if (p->leaf == false) { traverse(p->child_ptr[i]); } cout << " " << p->data[i]; } if (p->leaf == false) { traverse(p->child_ptr[i]); } cout<<endl; } void sort(int *p, int n) { int i, j, temp; for (i = 0; i < n; i++) { for (j = i; j <= n; j++) {
  • 7. if (p[i] > p[j]) { temp = p[i]; p[i] = p[j]; p[j] = temp; } } } } int split_child(BTreeNode *x, int i) { int j, mid; BTreeNode *np1, *np3, *y; np3 = init(); np3->leaf = true; if (i == -1) { mid = x->data[2]; x->data[2] = 0; x->n--; np1 = init(); np1->leaf = false; x->leaf = true; for (j = 3; j < 5; j++) { np3->data[j - 3] = x->data[j];
  • 8. np3->child_ptr[j - 3] = x->child_ptr[j]; np3->n++; x->data[j] = 0; x->n--; } for(j = 0; j < 6; j++) { x->child_ptr[j] = NULL; } np1->data[0] = mid; np1->child_ptr[np1->n] = x; np1->child_ptr[np1->n + 1] = np3; np1->n++; root = np1; } else { y = x->child_ptr[i]; mid = y->data[2]; y->data[2] = 0; y->n--; for (j = 3; j < 5; j++) { np3->data[j - 3] = y->data[j]; np3->n++; y->data[j] = 0;
  • 9. y->n--; } x->child_ptr[i + 1] = y; x->child_ptr[i + 1] = np3; } return mid; } void insert(int a) { int i, temp; x = root; if (x == NULL) { root = init(); x = root; } else { if (x->leaf == true && x->n == 5) { temp = split_child(x, -1); x = root; for (i = 0; i < (x->n); i++) { if ((a > x->data[i]) && (a < x->data[i + 1])) {
  • 10. i++; break; } else if (a < x->data[0]) { break; } else { continue; } } x = x->child_ptr[i]; } else { while (x->leaf == false) { for (i = 0; i < (x->n); i++) { if ((a > x->data[i]) && (a < x->data[i + 1])) { i++; break; } else if (a < x->data[0])
  • 11. { break; } else { continue; } } if ((x->child_ptr[i])->n == 5) { temp = split_child(x, i); x->data[x->n] = temp; x->n++; continue; } else { x = x->child_ptr[i]; } } } } x->data[x->n] = a; sort(x->data, x->n); x->n++; }
  • 12. int main() { int i, n, t; cout<<"enter the no of elements to be insertedn"; cin>>n; for(i = 0; i < n; i++) { cout<<"enter the elementn"; cin>>t; insert(t); } cout<<"traversal of constructed treen"; traverse(root); getch(); }
  • 13. //Threaded Binary Tree #include <iostream> #include <cstdlib> #define MAX_VALUE 65536 using namespace std; /* Class Node */ class Node { public: int key; Node *left, *right; bool leftThread, rightThread; }; /* Class ThreadedBinarySearchTree */ class ThreadedBinarySearchTree { private: Node *root; public: /* Constructor */ ThreadedBinarySearchTree() {
  • 14. root = new Node(); root->right = root->left = root; root->leftThread = true; root->key = MAX_VALUE; } /* Function to clear tree */ void makeEmpty() { root = new Node(); root->right = root->left = root; root->leftThread = true; root->key = MAX_VALUE; } /* Function to insert a key */ void insert(int key) { Node *p = root; for (;;) { if (p->key < key) { if (p->rightThread) break; p = p->right; }
  • 15. else if (p->key > key) { if (p->leftThread) break; p = p->left; } else { /* redundant key */ return; } } Node *tmp = new Node(); tmp->key = key; tmp->rightThread = tmp->leftThread = true; if (p->key < key) { /* insert to right side */ tmp->right = p->right; tmp->left = p; p->right = tmp; p->rightThread = false; } else {
  • 16. tmp->right = p; tmp->left = p->left; p->left = tmp; p->leftThread = false; } } /* Function to search for an element */ bool search(int key) { Node *tmp = root->left; for (;;) { if (tmp->key < key) { if (tmp->rightThread) return false; tmp = tmp->right; } else if (tmp->key > key) { if (tmp->leftThread) return false; tmp = tmp->left; } else
  • 17. { return true; } } } /* Fuction to delete an element */ void Delete(int key) { Node *dest = root->left, *p = root; for (;;) { if (dest->key < key) { /* not found */ if (dest->rightThread) return; p = dest; dest = dest->right; } else if (dest->key > key) { /* not found */ if (dest->leftThread) return; p = dest;
  • 18. dest = dest->left; } else { /* found */ break; } } Node *target = dest; if (!dest->rightThread && !dest->leftThread) { /* dest has two children*/ p = dest; /* find largest node at left child */ target = dest->left; while (!target->rightThread) { p = target; target = target->right; } /* using replace mode*/ dest->key = target->key; } if (p->key >= target->key) {
  • 19. if (target->rightThread && target->leftThread) { p->left = target->left; p->leftThread = true; } else if (target->rightThread) { Node *largest = target->left; while (!largest->rightThread) { largest = largest->right; } largest->right = p; p->left = target->left; } else { Node *smallest = target->right; while (!smallest->leftThread) { smallest = smallest->left; } smallest->left = target->left; p->left = target->right; }
  • 20. } else { if (target->rightThread && target->leftThread) { p->right = target->right; p->rightThread = true; } else if (target->rightThread) { Node *largest = target->left; while (!largest->rightThread) { largest = largest->right; } largest->right = target->right; p->right = target->left; } else { Node *smallest = target->right; while (!smallest->leftThread) { smallest = smallest->left; }
  • 21. smallest->left = p; p->right = target->right; } } } /* Function to print tree */ void printTree() { Node *tmp = root, *p; for (;;) { p = tmp; tmp = tmp->right; if (!p->rightThread) { while (!tmp->leftThread) { tmp = tmp->left; } } if (tmp == root) break; cout<<tmp->key<<" "; } cout<<endl;
  • 22. } }; int main() { ThreadedBinarySearchTree tbst; cout<<"ThreadedBinarySearchTree Testn"; char ch; int choice, val; /* Perform tree operations */ do { cout<<"nThreadedBinarySearchTree Operationsn"; cout<<"1. Insert "<<endl; cout<<"2. Delete"<<endl; cout<<"3. Search"<<endl; cout<<"4. Clear"<<endl; cout<<"Enter Your Choice: "; cin>>choice; switch (choice) { case 1 : cout<<"Enter integer element to insert: "; cin>>val; tbst.insert(val);
  • 23. break; case 2 : cout<<"Enter integer element to delete: "; cin>>val; tbst.Delete(val); break; case 3 : cout<<"Enter integer element to search: "; cin>>val; if (tbst.search(val) == true) cout<<"Element "<<val<<" found in the tree"<<endl; else cout<<"Element "<<val<<" not found in the tree"<<endl; break; case 4 : cout<<"nTree Clearedn"; tbst.makeEmpty(); break; default : cout<<"Wrong Entry n "; break; } /* Display tree */ cout<<"nTree = "; tbst.printTree();
  • 24. cout<<"nDo you want to continue (Type y or n): "; cin>>ch; } while (ch == 'Y'|| ch == 'y'); return 0; }
  • 25. //To Copy one string into other #include <stdio.h> int main() { char s1[100], s2[100], i; printf("Enter string s1: "); gets(s1); printf("Enter string s2: "); gets(s2); for(i = 0; s1[i] != '0'; ++i) { s2[i] = s1[i]; } s2[i] = '0'; printf("String s1: %s", s1); printf("nString s2: %s", s2); return 0; }
  • 26. // Read from binary file #include <stdio.h> struct threeNum { int n1, n2, n3; }; int main() { int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:program.bin","rb")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } for(n = 1; n < 5; ++n) {
  • 27. fread(&num, sizeof(struct threeNum), 1, fptr); printf("n1: %dtn2: %dtn3: %d", num.n1, num.n2, num.n3); } fclose(fptr); return 0; }
  • 28. //To count number of words #include<stdio.h> void main() { FILE *p; char ch; int w=1; clrscr(); p=fopen("source","r"); if(p==NULL) { printf("file not found"); } else { ch=fgetc(p); while(ch!=EOF) { printf("%c",ch); if(ch==' '||ch=='n') { w++; } ch=fgetc(p); } printf("nWords in a file are=%d",w); } fclose(p); getch(); }
  • 29. //writing into a binary file #include <stdio.h> struct threeNum { int n1, n2, n3; }; int main() { int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:program.bin","wb")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } for(n = 1; n < 5; ++n) { num.n1 = n; num.n2 = 5n; num.n3 = 5n + 1; fwrite(&num, sizeof(struct threeNum), 1, fptr); } fclose(fptr); return 0;