Professional Documents
Culture Documents
Question#1
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
};
int data, i;
node *start = NULL;
node *p;
node *q;
}
}
}
void inorder(node *p)
{
if (p != NULL)
{
inorder(p->left);
cout << "tree: " << p->data << endl;
inorder(p->right);
}
}
system("pause");
return 0;
}
Lab#7 3
Question#2
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
};
int data, i;
node *start = NULL;
node *p;
node *q;
node *t;
node *old;
else
{
p->right = new node;
p = p->right;
p->data = data;
p->left = NULL;
p->right = NULL;
}
}
}
void inorder(node *p)
{
if (p != NULL)
{
inorder(p->left);
cout<< p->data<< " ";
inorder(p->right);
}
}
void preorder(node *p)
{
if (p != NULL)
{
Question#3
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
};
int data, i;
node *start = NULL;
node *p;
node *q;
else
{
q = p;
p = p->right;
}
}
p = q;
if (p->data > data)
{
p->left = new node;
p = p->left;
p->data = data;
p->left = NULL;
p->right = NULL;
}
else
Lab#7 8
{
p->right = new node;
p = p->right;
p->data = data;
p->left = NULL;
p->right = NULL;
}
}z:;
}
bool search(int d, node*p){
if (p == NULL)
return false;
else if (p->data > d)
search(d, p->left);
else if (p->data < d)
search(d, p->right);
else
return true;
}
return;
if (x < t->data)
delet(x, t->left);
else if (x>t->data)
delet(x, t->right);
else if (t->left != NULL && t->right != NULL){
node* temp = findminrec(t->right);
t->data = temp->data;
delet(t->data, t->right);
}
else
{
node* old = t;
if (t->left != NULL)
t = t->left;
else
t = t->right;
delete old;
}
};
int main()
{
node *a, b;
a = &b;
int n,x,y,d;
cout << "entetr size 'N':";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "enter values:";
cin >> x;
insertnodefront(x);
}
m:
cout << "enter 1 for inorder." << endl;
cout<<"enter 2 for preorder."<<endl;
cout<<"enter 3 for postorder."<<endl;
cout<<"enter 4 for delete."<<endl;
cout << "press key:";
cin >> y;
switch (y)
{
case 1:
inorder(start);
goto m;
break;
case 2:
preorder(start);
goto m;
break;
Lab#7 10
case 3:
postorder(start);
goto m;
break;
case 4:
cout << "Enter Node you want to delete :";
cin >> d;
delet(d, start);
goto m;
break;
system("pause");
return 0;
}
Lab#7 11
Question#4
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
};
int data, i;
node *start = NULL;
node *p;
node *q;
}
}
}
void inorder(node *p)
{
if (p != NULL)
{
inorder(p->left);
cout << "tree: " << p->data << endl;
inorder(p->right);
}
}
void search(int data, node*p)
{
if (p == NULL)
{
cout << "data not found."<<endl;
}
}
else if (data < p->data)
{
search(data,p->left);
}
else
{
search(data,p->right);
}
}
int main()
{
node *a, b;
a = &b;
int n, x, y,m;
cout << "entetr size 'N':";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "enter values:";
cin >> x;
insertnodefront(x);
}
inorder(start);
cout << "enter search value:";
cin >> m;
search(m,start);
system("pause");
return 0;
}
Lab#7 13
Question#5
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
};
int data, i;
node *start = NULL;
node *p;
node *q;
void insertnodefront(int data)
{ p = start;
if (start == NULL)
{
start = new node;
start->data = data;
start->left = NULL;
start->right = NULL;
}
else
{
while (p != NULL)
{
if (p->data == data)
{
cout << "duplicate" << endl;
break;
}
else if (p->data > data)
{
q = p;
p = p->left;
}
else if (p->data<data)
{
q = p;
p = p->right;
} }
p = q;
if (p->data > data)
{
p->left = new node;
p = p->left;
p->data = data;
p->left = NULL;
p->right = NULL; }
else
{
p->right = new node;
p = p->right;
p->data = data;
p->left = NULL;
Lab#7 14
p->right = NULL; } } }
void inorder(node *s)
{
if (s != NULL)
{
inorder(s->left);
cout<< s->data<< " ";
inorder(s->right);
}
}
void delete_leaf()
{
if(p->left==NULL&&p->right==NULL)
{
if(q->left==p)
{
p->left=NULL;
}
else
{
q->right=NULL;
}
delete p;
goto end;
}
end:;
}
int main()
{ node s;
int a, n;
cout << "Enter size 'N':";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter Value :";
cin >> a;
insertnodefront(a);
}
cout<<endl;
cout<<"TREE BEFORE DELETE LEAF."<<endl;
cout<<endl << "Data in In-Order :";
inorder(start);
delete_leaf();
Question#6
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
};
int data, i;
node *start = NULL;
node *p;
node *q;
{
p->right = new node;
p = p->right;
p->data = data;
p->left = NULL;
p->right = NULL;
}
}
}
void inorder(node *s)
{
if (s != NULL)
{
inorder(s->left);
cout<< s->data<< " ";
inorder(s->right);
}
}
int main()
{
int a, n;
cout << "Enter size 'N':";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter Value :";
cin >> a;
insertnodefront(a);
}
system("pause");
return 0;
}