You are on page 1of 67

COLUMN MAJOR

#include <iostream>
using namespace std;
int main ()
{
int row;
int col;
cout << "MATRIX ROWS: ";
cin >> row;
cout << "MATRIX COLUMNS: ";
cin >> col;
int **array = new int *[row];
for (int i = 0; i < row; i++)
{
array[i] = new int[col];
}
cout << "ELEMENTS OF MATRIX: " << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cin >> array[i][j];
}
}
cout << "MATRIX:" << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << array[i][j] << " " ;
if (j == row -1)
cout << endl;

}
int *Arrr = new int[row * col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Arrr[j * row + i] = array[i][j];
}
}
cout << endl << "COLUMN MAJOR:" << endl << endl;
for (int i = 0; i < row * col; i++)
{

cout <<" "<< Arrr[i] ;


}
cout << endl;
system ("pause");
}

ROW MAJOR
#include<iostream>
#include<iomanip>
using namespace std;

class matrix
{

int **p;

int r;

int c;

int *rowmajor;

public:
matrix (int row, int col)
{

r = row;

c = col;

p = new int *[r];

for (int i = 0; i < r; i++)

p[i] = new int[c];

}
}
void input ()
{
cout << endl;
cout << " Enter your MATRIX :" << endl;

for (int i = 0; i < r; i++)

for (int j = 0; j < c; j++)

{
cout << " " ;
cin >> p[i][j];

}
}
cout << endl;
}
void disp2D ()
{
// original Matrix
cout << endl;

for (int i = 0; i < r; i++)


{

for (int j = 0; j < c; j++)

cout << " " << p[i][j] << " ";

}
cout << endl << endl;

}
}
void dispRowMajor ()
{

rowmajor = new int[r * c];

for (int i = 0; i < r; i++)

for (int j = 0; j < c; j++)

rowmajor[i * c + j] = p[i][j];

}
}
//matrix Row Major

for (int i = 0; i < r * c; i++)

cout << " " << rowmajor[i] << " ";

}
}
void Multiply_rowMajor (matrix & x, int &row, int &col2)
{

int **m = new int *[row];

for (int i = 0; i < row; i++)

m[i] = new int[col2];

}
for (int i = 0; i < row; ++i)
{

for (int j = 0; j < col2; ++j)

{
m[i][j] = 0;

}
}
for (int i = 0; i < row; i++)

for (int j = 0; j < col2; j++)

for (int k = 0; k < c; k++)

m[i][j] += p[i][k] * x.p[k][j];

}
}
}
// multiplied matrix row major

for (int i = 0; i < row; i++)

for (int j = 0; j < col2; j++)

cout << " " << m[i][j] << " ";

}
cout << endl << endl;
}
};

int main ()
{

int row;

int col;

cout << " Enter MATRIX 1 Rows : ";

cin >> row;

cout << " Enter MATRIX 1 Columns : ";

cin >> col;

matrix a (row, col);

a.input ();
int row2;

int col2;

cout << " Enter MATRIX 2 Rows : ";

cin >> row2;

cout << " Enter MATRIX 2 Columns : ";

cin >> col2;

matrix b (row2, col2);

b.input ();
cout << endl;

cout << " MATRIX 1:" << endl;


a.disp2D ();
cout << endl;

cout << " MATRIX 1 Row Major:" << endl;


a.dispRowMajor ();
cout << endl;

cout << " MATRIX 2:" << endl;


b.disp2D ();
cout << endl;

cout << " MATRIX 2 Row Major:" << endl;


b.dispRowMajor ();
cout << endl;

cout << " After Multiplication MATRIX row Major :" << endl;
a.Multiply_rowMajor (b, row, col2);
cout << endl;
cout << setw(60) << " DARK KING " << setw(60) <<endl;
system ("pause");

SINGLY LINKED LIST

#include <iostream>
using namespace std;

template < class T >


class Node
{

private:
T data;

Node * next;

public:
Node ()
{

Node (T element)
{

data = element;

next = NULL;
}

void setData (T pVal)


{

data = pVal;
}

T getData ()
{

return data;
}

void setnext (Node * e)


{

next = e;
}

Node * getnext ()
{

return next;
}

};

template < class T >


class List
{

private:
Node < T > *first;

public:
List ()
{

first = NULL;

void Insert (Node < T > *pBefore, Node < T > *pNew)
{
if (first == NULL)

first = pNew;
}

else if (pBefore->getnext () == NULL)

pBefore->setnext (pNew);

pNew->setnext (NULL);

else

pNew->setnext (pBefore->getnext ());

pBefore->setnext (pNew);

void Delete (Node < T > *pToBeDeleted)


{

Node < T > *temp;

temp = first;

if (pToBeDeleted == first)

first = first->getnext ();

delete pToBeDeleted;

else if (pToBeDeleted->getnext () == NULL)

while (temp->getnext () != pToBeDeleted)

temp = temp->getnext ();


}

temp->setnext (NULL);

delete pToBeDeleted;

else

while (temp->getnext () != pToBeDeleted)

temp = temp->getnext ();

temp->setnext (pToBeDeleted->getnext ());

delete pToBeDeleted;

void printList ()

Node < T > *temp;

temp = first;

while (temp)

cout << temp->getData () << " ";

temp = temp->getnext ();

cout << endl;

};

int main ()
{

Node < int >*a, *b, *c, *d, *e;


a = new Node < int >(1);

b = new Node < int >(2);

c = new Node < int >(3);

d = new Node < int >(4);

e = new Node < int >(5);

List < int >*list;

list = new List < int >();

list->Insert (0, a);

list->Insert (a, b);

list->Insert (b, c);

list->Insert (a, d);

list->Insert (b, e);

list->printList ();

list->Delete (a);

cout << "\nAfter deleting first node" << endl;

list->printList ();

cout << "\nAfter calling function" << endl;

list->printList ();

return 0;

SINGLY CIRCULAR LIST

#include <iostream>
using namespace std;

template < class T >


class Node
{

private:
T data;

Node * next;

public:
Node ()
{

Node (T element)
{

data = element;

next = NULL;
}

void setData (T pVal)


{

data = pVal;
}

T getData ()
{

return data;
}

void setnext (Node * e)


{

next = e;
}

Node * getnext ()
{

return next;
}

};

template < class T >


class List
{

private:
Node < T > *last;

public:
List ()
{

last = NULL;

}
void Insert (Node < T > *pBefore, Node < T > *pNew)
{

if (last == NULL)

last = pNew;
}

else if (pBefore->getnext () == NULL)

pBefore->setnext (pNew);

pNew->setnext (last);

else

pNew->setnext (pBefore->getnext ());

pBefore->setnext (pNew);

void Delete (Node < T > *pToBeDeleted)


{

Node < T > *temp;

temp = last;

if (pToBeDeleted == last)

last = last->getnext ();

delete pToBeDeleted;

else if (pToBeDeleted->getnext () == NULL)

while (temp->getnext () != pToBeDeleted)


{

temp = temp->getnext ();

temp->setnext (NULL);

delete pToBeDeleted;

else

while (temp->getnext () != pToBeDeleted)

temp = temp->getnext ();

temp->setnext (pToBeDeleted->getnext ());

delete pToBeDeleted;

void printList ()

Node < T > *temp;

temp = last;
int n=1;

if (temp!=NULL)

{
do
{
cout << n << " = " << temp->getData() << endl;
temp=temp->getnext();
n++;
}
while(temp!=last);
}

// cout << endl;

}
};

int main ()
{

Node < int >*a, *b, *c, *d, *e;

a = new Node < int >(1);

b = new Node < int >(2);

c = new Node < int >(3);

d = new Node < int >(4);

e = new Node < int >(5);

List < int >*list;

list = new List < int >();

list->Insert (0, a);

list->Insert (a, b);

list->Insert (b, c);

list->Insert (a, d);

list->Insert (b, e);

list->printList ();

list->Delete (a);

cout << "\nAfter deleting first node" << endl;

list->printList ();

cout << "\nAfter calling function" << endl;

list->printList ();

return 0;

DOUBLY LINKED LIST


#include<iostream>

using namespace std;

template < class T >


class Node
{

public:
Node (T element)
{

data = element;

next = NULL;

prevs = NULL;

void setdata (T pVal)


{

data = pVal;

}
T getData ()
{

return data;

Node * getNext ()
{

return next;

void setNext (Node * x)


{

next = x;

}
void setprevs (Node * x)
{

prevs = x;

}
Node * get_prevs ()
{

return prevs;

}
private:
T data;

Node * next;

Node * prevs;

};

template < class T >


class DList
{

private:
Node < T > *first;

public:
DList ()
{

first = NULL;

void Insert (Node < T > *pBefore, Node < T > *pNew)
{

if (first == NULL)

first = pNew;

else if (pBefore->getNext () == NULL && pBefore->get_prevs () == NULL)

pBefore->setNext (pNew);

pNew->setprevs (pBefore);

pNew->setNext (NULL);

else

pNew->setNext (pBefore->getNext ());

pBefore->setNext (pNew);

pNew->setprevs (pBefore);

pBefore->getNext ()->setprevs (pNew);


}

void Delete (Node < T > *pToBeDeleted)


{

Node < T > *temp;

temp = first;

if (pToBeDeleted == first)

first = first->getNext ();

first->setprevs (NULL);

delete pToBeDeleted;

else if (pToBeDeleted->getNext () == NULL)

while (temp->getNext () != pToBeDeleted)

temp = temp->getNext ();

temp->setNext (NULL);

temp->setprevs (pToBeDeleted->get_prevs ());

delete pToBeDeleted;

else
{

while (temp->getNext () != pToBeDeleted)

temp = temp->getNext ();

temp->setNext (pToBeDeleted->getNext ());

temp->setprevs (pToBeDeleted->get_prevs ());


delete pToBeDeleted;

void printList ()
{

Node < T > *temp = first;

while (temp)
{

cout << temp->getData () << " ";

temp = temp->getNext ();

cout << "\n";

}
void reverseList()
{
Node<T>* temp = first;
while ( temp && temp->getNext() != NULL)

temp = temp->getNext();

while(temp)
{
cout << temp->getData() << " ";
temp = temp->get_prevs();

}
}

};

int main ()
{

Node < int >*a, *b, *c, *d, *e, *f;

a = new Node < int >(200);

b = new Node < int >(30);

c = new Node < int >(40);

d = new Node < int >(45);


e = new Node < int >(450);

DList < int >*list;

list = new DList < int >();

list->Insert (0, a);

list->Insert (a, b);

list->Insert (b, c);

list->Insert (c, d);

list->Insert (d, e);

list->printList ();

list->Delete (a);

cout << "\nAfter deleting first node" << endl;

list->printList ();
//cout << "Printing in reverse order:" << endl;
//list-> reverseList();

return 0;

DOUBLY CIRCULAR LINKED LIST

#include<iostream>

using namespace std;


template < class T > class Node
{
public:
Node (T element)
{
data = element;
next = NULL;
previous = NULL;
}
void setData (T pVal)
{
data = pVal;
}
T getData ()
{
return data;
}
Node *getNext ()
{
return next;
}
void setNext (Node * x)
{
next = x;
}
void setPrevious (Node * x)
{
previous = x;
}
Node *getPrevious ()
{
return previous;
}
private:
T data;
Node *next;
Node *previous;
};

template < class T > class DCList


{
private:
Node < T > *first;
public:
DCList ()
{
first = NULL;
}
void Insert (Node < T > *pBefore, Node < T > *pNew)
{

if (first == NULL)
{
first = pNew;
first->setNext (first->getPrevious ());
first->setPrevious (first->getNext ());
}
else if (pBefore->getNext () == first->getPrevious ())
{
pBefore->setNext (pNew);
pNew->setPrevious (pBefore);
first->setPrevious (pNew);
pNew->setNext (first);
}
else
{
pBefore->getNext ()->setPrevious (pNew);
pNew->setNext (pBefore->getNext ());
pBefore->setNext (pNew);
pNew->setPrevious (pBefore);
}

}
void Delete (Node < T > *pToBeDeleted)
{
Node < T > *temp;
temp = first;
if (pToBeDeleted == first)
{
temp = first = first->getNext ();
first->setPrevious (NULL);
delete pToBeDeleted;
}
else if (pToBeDeleted->getNext () == NULL)
{
while (temp->getNext () != pToBeDeleted)
{
temp = temp->getNext ();
}
temp->setNext (NULL);
delete pToBeDeleted;
}
else
{
while (temp->getNext () != pToBeDeleted)
{
temp = temp->getNext ();
}
temp->setNext (pToBeDeleted->getNext ());
pToBeDeleted->getNext ()->setPrevious (temp);
delete pToBeDeleted;
}
}
void printList ()
{
Node < T > *temp = first;

do
{
cout << temp->getData () << " ";
temp = temp->getNext ();
}
while (temp != first);

};

int
main ()
{
Node < int >*a, *b, *c, *d, *e, *f;
a = new Node < int >(200);
b = new Node < int >(30);
c = new Node < int >(40);
d = new Node < int >(45);
e = new Node < int >(450);
DCList < int >*list;
list = new DCList < int >();
list->Insert (0, a);
list->Insert (a, b);
list->Insert (b, c);
list->Insert (c, d);
list->Insert (d, e);
list->printList ();
list->Delete (a);
cout << "\nAfter deleting first node" << endl;
list->printList ();
return 0;
}

STACKS using Singly Linked list

#include <iostream>
using namespace std;

template <class T>

class node
{
public:
T data;
node<T>* next;
};

template <class T>

class Stack
{
private:
node<T>* top;
public:
Stack()
{
top = NULL;
}
bool isEmpty()
{
if (top == NULL)
return false;
else
return true;
}
T Top()
{
if (top)
return top->data;
else
cout << "Stack is empty" << endl;
}
void push(const T & val)
{

node<T>* temp = new node<T>;


temp->data = val;
if(top == NULL)
{
temp->next = NULL;
}
else
{
temp->next = top;
}
top = temp;

}
int pop()
{
if (top == NULL)
{
cout << "Stack is empty" << endl;
return 0;
}
else
{
top->data;
cout << top->data << " is popped" << endl;
node<T>* temp = top;
top = top->next;
delete temp;

}
void print()
{
node<T>* temp = top;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
};
int main()
{

Stack<int> *st =new Stack<int>();


if(st->isEmpty())
cout<<"Stack is currently empty"<<endl;
st->push(1);
st->push(2);
st->push(3);
while (!st->isEmpty())
{
int value = st->pop();
cout<<value<<endl;
}

return 0;
}

STACKS USING ARRAYS AND POSTFIXED

#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
class Stack
{
T *arr;
T top;
int capacity;

public:
Stack(int size = 5)
{
arr = new int[size];
capacity = size;
top = -1;
}

void push(T x)
{
if (isFull())
{
cout<<"Stack is FUll";
return;
}
arr[++top] = x;
}
T pop()
{
if (isEmpty())
{
cout << "Empty";
}

return arr[top--];
}
bool isEmpty()
{
return top == -1;
}
bool isFull()
{
return top == capacity - 1;
}

};

int main()
{
Stack<int> *st=new Stack<int>(5);

char ehs[]={'3','4','-','6','*'};
int sixe =sizeof (ehs);
for (int i=0; i<sixe ;i++)
{
if (ehs[i] !='+' && ehs[i] !='*' && ehs[i] !='-' &&ehs[i] !='/' )
{
st->push(ehs[i]-'0');
}
else
{
int x,y;
x=st->pop();
y=st->pop();
if(ehs[i]=='/')
{
st ->push(x/y);

}
else if(ehs[i]=='*')
{
st ->push(x*y);
}
else if(ehs[i]=='-')
{
st ->push(x-y);
}
else if(ehs[i]=='+')
{
st ->push(x+y);
}
else
{
cout << "null";
}
}
}

POSTFIX AND INFIX LAB 7


#include <iostream>

using namespace std;


template < class T >
class Node {
public:
T data;
Node * next;

Node() {

}
Node(T x) {
data = x;
}

void setData(T x) {
data = x;
}
T getData() {
return data;
}
void setNext(Node * Link) {
next = Link;
}
Node * getNext() {
return next;
}
};
template < class T >
class List {
private:
Node < T > * first;
public:
List() {
first = NULL;
}

void Insert(Node < T > * pBefore, Node < T > * pNew) {

if (!first) {
first = pNew;
return;
}
pNew -> next = pBefore -> next;
pBefore -> next = pNew;

void Delete(Node < T > * pToBeDeleted) {


Node < T > * temp;
temp = first;
if (pToBeDeleted == first) {
first = first -> next;
delete pToBeDeleted;
return;
}
while (temp -> next != pToBeDeleted) {
temp = temp -> next;

}
temp -> setNext(pToBeDeleted -> next);
delete pToBeDeleted;
}
void printList() {

Node < T > *


var = first;

if (var == NULL)
cout << " empty linked list." << endl;

while (var != NULL) {


cout << "data " <<
var -> data << endl;
var =
var -> next;

}
cout << endl;
}
};
template < class T >
class STACK {
public:
Node < T > * top;
List < T > * list;
STACK() {
top = NULL;
}
bool IsEmpty() {
return (top == NULL);
}
bool isFull() {
return false;
}
void push(const T element) {
Node < T > * n = new Node < T > (element);

n -> setNext(top);
top = n;
}
T pop() {

Node < T > * temp = top;


T element = top -> getData();
top = top -> getNext();

delete temp;
return element;
}
T peek() {
T element = top -> getData();
return element;
}

};

int main() {
STACK < char > * st = new STACK < char > ();
char infix[10] = {'2','/','3','*','2','+','1'};
char postfix[10];
int i;
for ( i=0; i<10; i++)
{
if(infix[i]!='/'&&infix[i]!='*'&&infix[i]!='+'&&infix[i]!='-')
st->push(infix[i]);
}
for ( i=0; i<10; i++)
{
if(infix[i]=='/' ||infix[i]=='*'||infix[i]=='+'||infix[i]=='-')
st->push(infix[i]);
}
i=0;
while(!st->IsEmpty())
{
postfix[i]=st->pop();
i++;
}

cout<<"Postfix Expression : ";


for(i=0; i<10; i++)
{
cout<<postfix[i];
}
return 0;
}
Queues by li9nked list

#include <iostream>
using namespace std;

template < typename T >


class Node

public:

Node (T element)
{

data = element;

void sdata (T pVal)


{

data = pVal;

}
T gdata ()
{

return data;

Node * gnext ()
{

return link;

void snext (Node * x)


{

link = x;

}
private:
T data;

Node * link;

};

template < typename T >


class List
{

public:

List ()
{

first = NULL;

}
void Insert (Node < T > *pBefore, Node < T > *pNew)
{

if (first == NULL)

first = pNew;

else if (pBefore->gnext () == NULL)

pBefore->snext (pNew);

pNew->snext (NULL);

else

pNew->snext (pBefore->gnext ());

pBefore->snext (pNew);

void Delete (Node < T > *pToBeDeleted)


{

Node < T > *temp;

temp = first;

if (pToBeDeleted == first)

first = first->gnext ();

delete pToBeDeleted;
}

else if (pToBeDeleted->gnext () == NULL)

while (temp->gnext () != pToBeDeleted)

temp = temp->gnext ();

temp->snext (NULL);

delete pToBeDeleted;

else

while (temp->gnext () != pToBeDeleted)

temp = temp->gnext ();

temp->snext (pToBeDeleted->gnext ());

delete pToBeDeleted;

private:
Node < T > *first;

};

template < typename T >


class Queue
{

private:
Node < T > *front;

Node < T > *rear;


public:
Queue ()
{

front = NULL;

rear = NULL;

void Put (T element)


{

Node < T > *temp = new Node < T > (element);

if (IsEmpty ())

front = rear = temp;

else

Node < T > *temp = new Node < T > (element);

temp->snext (rear);

rear = temp;

T Get ()
{

Node < T > *temp = rear;

T x = rear->gdata ();

rear = temp->gnext ();

delete temp;

return x;

bool IsEmpty ()
{

if (rear == NULL)

return true;
else

return false;

bool IsFull ()
{

return false;

};

int
main ()
{

Queue < int >*q = new Queue < int >();

if (q->IsEmpty ())

cout << "Queue is currently empty" << endl;

q->Put (3);

q->Put (2);

q->Put (1);

while (!q->IsEmpty ())

int value = q->Get ();

cout << value << endl;

}
return 0;

system ("pause");

Queues By array
#include<iostream>
using namespace std;

template < class T >

class Queue
{
T * arr;

int front;

int rear;

int Ms;

public:

Queue (int size)


{

arr = new T[size];

Ms = size;

front = 0;

rear = -1;

}
void Put (T element)
{

if (IsFull ())

cout << "Queue is Full!";

return;

rear++;

arr[rear] = element;

T Get ()
{

if (IsEmpty ())

cout << " Empty!";

return 0;

else

{
T element;

element = arr[front];

front++;

return element;

bool IsFull ()
{

if (rear == Ms)

return true;

else

return false;

bool IsEmpty ()
{

if (front > rear)

return true;

else

return false;

};

int
main ()
{

Queue < int >*q = new Queue < int >(5);

if (q->IsEmpty ())

cout << "Queue is currently empty" << endl;

q->Put (1);

q->Put (2);

q->Put (3);
while (!q->IsEmpty ())

int value = q->Get ();

cout << value << endl;

}
return 0;

system ("pause");

Palindrome

#include<iostream>
#include<string>
using namespace std;

template < class T >

class Queue
{

T * arr;

int front;

int rear;

int Ms;

public:

Queue (int size)


{

arr = new T[size];

Ms = size;

front = 0;

rear = -1;

}
void Put (T element)
{

if (IsFull ())

cout << "Queue is Full!";


return;

rear++;

arr[rear] = element;

T Get ()
{

if (IsEmpty ())

cout << "Queue is Empty!";

return 0;

else

T element;

element = arr[front];

front++;

return element;

bool IsFull ()
{

if (rear == Ms)

return true;

else

return false;

bool IsEmpty ()
{

if (front > rear)

return true;
else

return false;

};
bool Palindrome(string word)
{
int leng = word.length();
int i;
Queue<char> *q1 =new Queue<char>(leng/2);
Queue<char> *q2 =new Queue<char>(leng/2);
bool Pal = false;
if(leng%2==0)
{
return false;
}

for(i=0; i<(leng/2); i++)


{
q1->Put(word[i]); //puttimg word and pointing q1 on it
}
for(i=(leng-1); i>(leng/2); i--)
{
q2->Put(word[i]); //puttimg word and pointing q1 on it
}
while(!q1->IsEmpty()&&!q2->IsEmpty())
{
char x,y;
x = q1->Get();
y = q2->Get();
if(x==y)
{
Pal = true;
cout << "Palindrome" <<endl;
}
else
{
Pal = false;
cout <<"Not Palindrome"<<endl;
break;
}
}
}

int main()
{
string e;
cout<<"Enter the Desire word "<<endl;
getline(cin,e);
Palindrome(e);
system("pause");
return 0;
}

BINARY TREES

#include<iostream>
using namespace std;
template<class T>
class BNode
{
public:
BNode()
{
leftchild=NULL;
rightchild=NULL;
}
void setLeftChild(BNode<T>* n)
{
leftchild=n;

}
BNode<T>* getLeftChild()
{
return leftchild;
}
void setRightChild(BNode<T>* n)
{
rightchild=n;
}
BNode<T>* getRightChild()
{
return rightchild;
}
void setData(T pdate)
{
data=pdate;
}
T getData()
{
return data;
}
private:
T data;
BNode* leftchild;
BNode* rightchild;
};
template<class T>
class BinaryTree
{
public:
BinaryTree()
{
root=NULL;
}

void BuildTree(T *Arr, int size)


{
if (size < 2)
return;
BNode<T> **temp = new BNode<T>*[size];

for (int i = 1; i < size; i++)


{
if (Arr[i] == 0)
temp[i] = NULL;
else
{
temp[i] = new BNode<T>();
temp[i]->setData(Arr[i]);

}
if (i / 2 >= 1)
{
if (i % 2 == 0)
{
temp[i / 2]->setLeftChild(temp[i]);

else
{
temp[i / 2]->setRightChild(temp[i]);

}
}
root = temp[1];

//*************88POST ORDER***************8
void PostOrder()
{
BNode<T> * ptr =root;

printPostorder(ptr);
}

void printPostorder(BNode<T> * node)


{
if (node == NULL)
{
return;
}

else
{
printPostorder(node->getLeftChild());
printPostorder(node->getRightChild());
cout << node->getData() << " ";

}
}
//***********Pre order*******************
void PreOrder()
{
BNode<T> * ptr =root;

printPreorder(ptr);
}
void printPreorder(BNode<T> * node)
{
if (node == NULL)
{
return;
}

else
{
cout << node->getData() << " ";
printPreorder(node->getLeftChild());
printPreorder(node->getRightChild());

}
//****************************In order***************

void InOrder()
{
BNode<T> * ptr =root;
printInorder(ptr);
}

void printInorder(BNode<T> * node)


{
if (node == NULL)
{
return;
}

else
{

printInorder(node->getLeftChild());
cout << node->getData()<< " ";
printInorder(node->getRightChild());
}

}
int maxDepth(BNode<T>* node)
{
if (node == NULL)
return 0;
else
{
/* compute the depth of each subtree */
int LeftDepth = maxDepth(node->getLeftChild());
int RightDepth = maxDepth(node->getRightChild());

/* use the larger one */


if (LeftDepth > RightDepth)
{
return(LeftDepth + 1);
}

else
{
return(RightDepth + 1);
}
}
}
void Depth()
{

BNode<T> * ptr =root;


cout << maxDepth(ptr);

}
private:
BNode<T>*root;
};
int main()
{
BinaryTree<int> *BT=new BinaryTree<int>();

//array to pass, 0 means no node exists


int Arr[15]={0,1,2,3,4,5,6,7,8,9,10,0,12,13,14};

BT->BuildTree(Arr,15);

cout<<" Traversal Orders: "<<endl;


cout << endl;

cout<<"Post order Traversal(Recursive) is: "<<endl;


BT->PostOrder();
cout<<endl;

cout<<endl;
cout<<"Pre order Traversal(Recursive) is: "<<endl;
BT->PreOrder();
cout<<endl;

cout<<endl;
cout<<"In order Traversal(Recursive) is: "<<endl;
BT->InOrder();
cout<<endl;

cout <<endl;
cout << "Depth of the binary Tree is :" << endl;

BT->Depth();
cout<<endl;

return 0;
}
HEAP SORTING
#include<iostream>
#include<ctime>
using namespace std;
template <class T>

class HeapSorting
{
private:
T*Array;
int length;

public:

HeapSorting(int l)
{
length=l;

Array = new T[length];


for(int i=0; i<length; i++)
Array[i]=rand()%100;

void HeapifyingArray(T Array[], int length, int t)


{

int Greatest = t;
int Lvalue = 2 * t + 1;
int Rvalue = 2 * t + 2;

if (Lvalue < length && Array[Lvalue] > Array[Greatest])


Greatest = Lvalue;

if (Rvalue < length && Array[Rvalue] > Array[Greatest])


Greatest = Rvalue;

if (Greatest != t)
{
swap(Array[t], Array[Greatest]);

HeapifyingArray(Array, length, Greatest);


}
}
void HSorting(T Array[], int length)
{
for (int i = length / 2 - 1; i >= 0; i--)
{

HeapifyingArray(Array, length, i);

}
for (int i = length - 1; i >= 0; i--)
{
swap(Array[0], Array[i]);
HeapifyingArray(Array, i, 0);

}
}
void HSorting()
{
HSorting(Array,length);
}

void Display()
{
for(int i=0; i<length; i++)
cout<<Array[i]<<" ";
}

};

int main()
{

int t;
cout<<"Enter size of Array: ";
cin>>t;
HeapSorting<int> H(t);
cout<<"Initial Array:\n";
H.Display();
cout<<"\n After sorting:\n";
H.HSorting();
H.Display();

BINAARY TREE TRAVERSALS

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
template < class T >
class BNode
{
public:
BNode ()
{
leftchild = NULL;
rightchild = NULL;
}
void setLeftChild (BNode < T > *n)
{
Data Structures Page 2
Gift University Gujranwala Electrical Engineering
leftchild = n;
}
BNode < T > *getLeftChild () {
return leftchild;
}
void setRightChild (BNode < T > *n) {
rightchild = n;
}
BNode < T > *getRightChild () {
return rightchild;
}
void setData (T pdate)
{
data = pdate;
}
T getData ()
{
return data;
}
private:
T data;
BNode * leftchild;
BNode * rightchild;
};
Data Structures Page 3
Gift University Gujranwala Electrical Engineering
template < class T >
class BinaryTree
{
public:
BinaryTree ()
{
root = NULL;
}
void BuildTree (T * Arr, int size)
{
if (size < 2)
return;
BNode < T > **temp = new BNode < T > *[size];
for (int i = 1; i < size; i++)
{
if (Arr[i] == 0)
temp[i] = NULL;
else
{
temp[i] = new BNode < T > ();
temp[i]->setData (Arr[i]);
}
if (i / 2 >= 1)
{
if (i % 2 == 0)
Data Structures Page 4
Gift University Gujranwala Electrical Engineering
{
temp[i / 2]->setLeftChild (temp[i]);
}
else
{
temp[i / 2]->setRightChild (temp[i]);
}
}
}
root = temp[1];
}
//*************88POST ORDER***************8 void PostOrder () //post
{
stack
<BNode < int >*>*s;
s = new stack < BNode < int >*>();
BNode < int >* current;
Data Structures Page 5
Gift University Gujranwala Electrical Engineering
stack < int >*st;
st = new stack < int >();
current = root;
s->push (current);
while (!s->empty ())
{
current = s->top ();
s->pop ();
if (current->getLeftChild ()) s->push (current->getLeftChild ());
if (current->getRightChild ()) s->push (current->getRightChild ());
st->push (current->getData ());
Data Structures Page 6
Gift University Gujranwala Electrical Engineering
}
while (!st->empty ())
{
cout << st->top () << " ";
st->pop ();
}
}
//***********Pre order*******************
void PreOrder ()
{
stack<BNode < int >*>*s;
s = new stack < BNode < int >*>();
BNode < int >*current;
stack < int >*st;
st = new stack < int >();
current = root;
s->push (current);
while (!s->empty ())
{
current = s->top ();
s->pop ();
Data Structures Page 7
Gift University Gujranwala Electrical Engineering
if (current->getRightChild ())
s->push (current->getRightChild ());
if (current->getLeftChild ())
s->push (current->getLeftChild ());
st->push (current->getData ());
}
while (!st->empty ())
{
cout << st->top () << " ";
st->pop ();
}
}
//****************************In order***************
void Inorder ()
{
stack < BNode < int >*>*s = new stack < BNode < int >*>(); BNode < T > *curr =
root;
while (1)
{
while (curr)
{
s->push (curr);
curr = curr->getLeftChild ();
}
if (!s->empty ())
{
Data Structures Page 8
Gift University Gujranwala Electrical Engineering
curr = s->top ();
s->pop ();
cout << curr->getData()<<" ";
curr = curr->getRightChild ();
}
else
break;
}
}
//***********LEVEL ORDER***************
void LevelOrder()
{
queue < BNode < int >*>*q = new queue < BNode < int >*>(); BNode < T > *curr =
root;
if (root == NULL) return;
// Enqueue Root and NULL node.
q->push(root);
q->push(NULL);
while (q->size() > 1)
{
curr = q->front();
q->pop();
if (curr == NULL)
{
q->push(NULL);
}
else {
if(curr->getLeftChild())
q->push(curr->getLeftChild());
if(curr->getRightChild())
Data Structures Page 9
Gift University Gujranwala Electrical Engineering
q->push(curr->getRightChild());
cout << curr->getData() << " ";
}
}
}
int Height(BNode<T>* root)
{
queue < BNode < int >*>*q = new queue < BNode < int >*>(); int height = 0;
int nodeCount = 0;
BNode < T > *curr ;
if (root == NULL)
{
return 0;
}
q->push(root);
while (!q->empty())
{
height++;
nodeCount = q->size();
while (nodeCount--)
{
curr = q->front();
if (curr->getLeftChild() != NULL) {
q->push(curr->getLeftChild() );
}
if (curr->getRightChild() != NULL) {
q->push(curr->getRightChild());
}
q->pop();
}
}
return height;
}
void height ()
{
BNode < T > *ptr = root;
cout << Height (ptr);
Data Structures Page 10
Gift University Gujranwala Electrical Engineering
}
private:
BNode < T > *root;
};
int main ()
{
BinaryTree < int >*BT = new BinaryTree < int >(); //array to pass, 0 means no node
exists
int Arr[15] ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 12, 13, 14}; BT->BuildTree
(Arr, 15);
cout <<" Traversal Orders: " <<endl;
cout << endl;
cout << "Post order Traversal(Iteratively) is: " << endl; BT->PostOrder ();
cout << endl;
cout << endl;
cout << "Pre order Traversal(Iteratively) is: " << endl; BT->PreOrder ();
cout << endl;
cout << endl;
cout << "In order Traversal(Iteratively) is: " << endl; BT->Inorder ();
cout << endl;
cout << endl;
cout << "Level Order Traversal(Iteratively)"<< endl; BT->LevelOrder();
cout << endl;
cout << endl;
Data Structures Page 11
Gift University Gujranwala Electrical Engineering
cout << "Height of the binary Tree is :" << endl;
BT->height ();
cout << endl;
return 0;
}

BST

#include <iostream>
using namespace std;
template<class DT>
class BNode
{
public:
BNode()
{
data=0;
leftchild=NULL;
rightchild=NULL;
}
void setLeftChild(BNode<DT>* n)
{
leftchild=n;

}
BNode<DT>* getLeftChild()
{

return leftchild;
}
void setRightChild(BNode<DT>* n)
{
rightchild=n;

}
BNode<DT>* getRightChild()
{
return rightchild;
}
void setData(DT pdate)
{

data=pdate;
}
DT getData()
{
return data;
}
private:
DT data;
BNode* leftchild;
BNode* rightchild;
};
template<class DT>
class BinarySearchTree
{
public:

BinarySearchTree()
{
root=NULL;

}
bool isEmpty() const
{
return root==NULL;
}

bool insert( DT x)
{

BNode<DT> *t=new BNode<DT>();


t->setLeftChild(NULL);
t->setRightChild(NULL);
t->setData(x);
BNode<DT> *p;
p=NULL;
if(root==NULL)
{
root=t;
}
else
{
BNode<DT>*q;
q=root;
while(q)
{
p=q;
if(t->getData() > q->getData())
{
q=q->getRightChild();
}
else
{
q=q->getLeftChild();
}
if(t->getData()==p->getData())
{

cout<<"Already node :"<<t->getData() << " exists"<<endl;


return false;
}

}
if(t->getData() < p->getData())
{
p->setLeftChild(t);
}
else
{
p->setRightChild(t);
}

}
}

BNode<DT> * search( DT data)

BNode<DT>*q;
q=root;
while(q)
{
if(q->getData() == data)
{ return q;
break;
}
if(data < q->getData())
{

q = q->getLeftChild();
}
else
{
q=q->getRightChild();

}
}
return NULL;
}

void printSorted()

{
BNode<DT> * ptr =root;

printPreorder(ptr);
}
void printPreorder(BNode<DT> * node)
{
if (node == NULL)
{
return;
}

else
{
cout << node->getData() << " ";
printPreorder(node->getLeftChild());
printPreorder(node->getRightChild());

}
bool remove(DT k)
{
BNode<DT>*CN = root;
BNode<DT>*PN = NULL;
while(CN!=NULL && CN->getData()!=k)
{
PN = CN;
if(k<CN->getData())
CN = CN->getLeftChild();
else
CN = CN->getRightChild();
}
if(CN==NULL)
{
cout<<k<<" is not in BST";

}
if (CN->getLeftChild()== NULL || CN->getRightChild()== NULL)
{
BNode<DT>*newCN;
if (CN->getLeftChild() == NULL)
newCN = CN->getRightChild();
else
newCN = CN->getLeftChild();
if (PN == NULL)
PN=newCN;

if (CN == PN->getLeftChild())
PN->setLeftChild(newCN);
else
PN->setRightChild(newCN);

}
else {
BNode<DT>*p = NULL;
BNode<DT>*temp;

temp = CN->getRightChild();
while (temp->getLeftChild() != NULL)
{
p = temp;
temp = temp->getLeftChild();
}
if (p != NULL)
p->setLeftChild(temp->getRightChild());
else
CN->setRightChild(temp->getRightChild()) ;
CN->setData(temp->getData());

}
return true;
}

//~BinarySearchTree();
private:
BNode<DT> * root;
};
int main()
{

BinarySearchTree<int> *BST=new BinarySearchTree<int>();

BST->insert(12);
BST->insert(4);
BST->insert(9);
BST->insert(2);
BST->insert(14);
BST->insert(16);
BST->insert(13);

BST->insert(12);

cout << endl;


cout <<"Pre Order :"<<endl;
BST->printSorted();
cout <<endl;

cout << endl;


cout <<"Searching Nodes : " << endl;
BNode<int>*n =BST->search(12);
if(n)
{
cout<<"-> "<<"Value 12 exists"<<endl;
}
else
{
cout<<"-> "<<"Value 12 does not exist"<<endl;

cout << endl;


n =BST->search(23);
if(n)
{

cout<<"-> "<<"Value 23 exists"<<endl;


}
else
{
cout<<"-> "<<"Value 23 does not exist"<<endl;
}
cout << endl;

cout << endl;


cout << "Removing Nodes :"<<endl;
if(BST->remove(16))
{
cout<<"-> "<<"Node carrying 16 removed successfully"<<endl;
}

if(BST->remove(2))
{
cout<<"-> "<<"Node carrying 2 removed successfully"<<endl;
}

if(BST->remove(12))
{
cout<<"-> "<<"Node carrying 12 removed successfully"<<endl;
}
cout << endl;

cout << endl;


cout <<"Pre Order :"<<endl;
BST->printSorted();
return 0;
}

MIN HEAP

#include <iomanip>
#include <iostream>
using namespace std;

class MinHeap
{
private:
int *array;
int n;
int max;
public:
MinHeap(int size)
{ max=size;
n=0;
array = new int[size];
for (int i = 0; i < size; i++)
array[i] = 0;
}
void insert(const int data)
{
for (int i = 0; i <n-1; i++)
{
if (data == array[i])
{
cout<< data <<" Already exists." << endl;
return;
}
}
int i;
if (n == max)
{
cout << "Heap full." << endl;
return;
}
n++;
for ( i = n; 1;)
{
if (i == 1)
break;
if (data >= array[i / 2])
{
break;
}
array[i] = array[i/2];
i /= 2;

}
array[i] = data;
}

void printContents()
{
for (int i = 0; i < max; i++)
{
if (array[i] != 0)
cout << array[i] << " ";
}
cout << endl;
}
int del ()

{
int x, n;
int size=0;
int i,j;
size=max;
if (!size)
{
cout << "Heap is empty." << endl;
return 0;
}

x = array[1];
x = array[size];

size--;
for(i=1,j = 2; j <= size;)
{
if (j < size)
{
if( array[j] < array[j + 1])
j++;
}
if (x >= array[j])
break;
array [i] = array [j];
i = j;
j *=2;

}
return array[i]=0;
array[size + 1]=x ;

return x;
}
};

int main()
{
MinHeap *mnHeap; //creating an object of MinHeap
mnHeap=new MinHeap(40);
//insert following data in the MinHeap
mnHeap->insert(12);
mnHeap ->insert(43);
mnHeap ->insert(9);
mnHeap ->insert(2);
mnHeap ->insert(14);
mnHeap ->insert(16);
mnHeap ->insert(13);
mnHeap ->insert(12);
mnHeap->printContents();
//Carrayy out 2 deletions from the MinHeap
int output;
output=mnHeap->del();
cout<<"Output of first deletion is "<< output <<endl;
mnHeap->printContents();
output=mnHeap->del();
cout<<"Output of second deletion is "<<output<<endl;
mnHeap->printContents();
return 0;
}

MAX HEAP

#include <iomanip>
#include <iostream>
using namespace std;

class MaxHeap
{
private:
int *array;
int n;
int max;
public:
MaxHeap(int size)
{ max=size;
n=0;
array = new int[size];
for (int i = 0; i < size; i++)
array[i] = 0;
}
void insert(const int data)
{
for (int i = 0; i < n-1; i++)
{
if (data == array[i])
{
cout<<data <<" Already exists." << endl;
return;
}
}
int i;
if (n == max)
{
cout << "Heap full." << endl;
return;
}
n++;
for ( i = n; 1;)
{
if (i == 1)
break;
if (data <= array[i / 2])
{
break;
}
array[i] = array[i/2];
i /= 2;

}
array[i] = data;
}

void printContents()
{
for (int i = 0; i < max; i++)
{
if (array[i] != 0)
cout << array[i] << " ";
}
cout << endl;
}
int del ()

{
int x, n;
int size=0;
int i,j;
size=max;
if (!size)
{
cout << "Heap is empty." << endl;
return 0;
}

x = array[1];
x = array[size];

size--;
for(i=1,j = 2; j <= size;)
{
if (j < size)
{
if( array[j] < array[j + 1])
j++;
}
if (x >= array[j])
break;
array [i] = array [j];
i = j;
j *=2;

}
return array[i]=0;
array[size + 1]=x ;

return x;
}

};

int main()
{
MaxHeap *mxHeap; //creating an object of MaxHeap
mxHeap=new MaxHeap(40);
//insert following data in the MaxHeap
mxHeap->insert(12);
mxHeap ->insert(43);
mxHeap ->insert(9);
mxHeap ->insert(2);
mxHeap ->insert(14);
mxHeap ->insert(16);
mxHeap ->insert(13);
mxHeap ->insert(12);
mxHeap->printContents();
//Carrayy out 2 deletions from the MaxHeap
int output;
output=mxHeap->del();
cout<<"Output of first deletion is "<<output<<endl;
mxHeap->printContents();
output=mxHeap->del();
cout<<"Output of second deletion is "<<output<<endl;
mxHeap->printContents();
return 0;

AIRPORT LANDING SYSTEM USING HEAPS

#include <iomanip>
#include <iostream>
using namespace std;

class MinHeap
{
private:
int *array;
int n;
int max;
public:
MinHeap(int size)
{ max=size;
n=0;
array = new int[size];
for (int i = 0; i < size; i++)
array[i] = 0;
}
void insert(const int data)
{
for (int i = 0; i <n-1; i++)
{
if (data == array[i])
{
cout<< data <<" Already exists." << endl;
return;
}
}
int i;
if (n == max)
{
cout << "Heap full." << endl;
return;
}
n++;
for ( i = n; 1;)
{
if (i == 1)
break;
if (data > array[i / 2])
{
break;
}
array[i] = array[i/2];
i /= 2;

}
array[i] = data;
}

void printContents()
{
for (int i = 0; i < max; i++)
{
if (array[i] != 0)
cout << array[i] << " ";
}
cout << endl;
}
};
int main()
{
MinHeap *mnHeap; //creating an object of MinHeap
mnHeap=new MinHeap(40);
cout<<setw(80)<<"Welcome To DARK KING'S Airport Landing System"<<setw(100)<<endl;
int x=1;
while (x==1)
{
int y;
cout<<"\n Is there an aircraft in airspace requesting to land?"<<endl<<"If yes then
Press 1, If No Press 2"<<endl;
cin>>y;
cout<<endl;
while (y != 1 && y != 2)
{
cout << "SORRY! We cant help you now."<<endl;
break;
}
if(y==1)
{
int z;
cout<<"Chronicle The Amount Of Fuel Present In Aircraft's fuel Tank:"<<endl;
cin>>z;
mnHeap->insert(z);
cout<<endl;
}
else
{
x++;

}
}
cout<<"The List Planes On The Runway For Flying Is As Follows: "<<endl;
cout<<endl;
mnHeap->printContents();
cout<<endl;
cout<<setw(80)<<"Thanks For Visiting DARK KING's AirPort System"<<setw(100)<<endl;
return 0;
}

ADJANCEY MATRIC GRPAHS


#include <iostream>
#include <queue>
using namespace std;

class Graph
{
private:
bool** adjMatrix;
int numVertices;
int numedg;

public:
Graph(int numVertices)
{
this->numVertices = numVertices;
adjMatrix = new bool*[numVertices];
for (int i = 0; i < numVertices; i++)
{
adjMatrix[i] = new bool[numVertices];
for (int j = 0; j < numVertices; j++)
adjMatrix[i][j] = false;
numedg=0;
}

}
void inserEdge(int fv, int tv)
{
adjMatrix[fv][tv] = true;
adjMatrix[tv][fv] = true;
numedg++;
}

int numberOfEdges()
{

return numedg/2;
}
void removeEdge(int fv, int tv)
{
adjMatrix[fv][tv] = false;
adjMatrix[tv][fv] = false;
numedg--;
}
int degree(int vertex)
{
int deg;
deg = 0;
for (int i = 0; i<numVertices; i++)
deg = deg + adjMatrix[vertex][i];
return deg;
}
void depthfirstSearch(int s)
{
bool* arr;
arr = new bool[numVertices];
for (int i = 0; i<numVertices; i++)

arr[i] = 0;
DFS(arr, s);

}
void DFS(bool*& ar, int st)
{
cout << st << " ";
ar[st] = 1;
for (int a = 0; a<numVertices; a++)
if (adjMatrix[st]
[a] == 1 && !ar[a])
DFS(ar, a);
}
void breadthfirstSearch(int s)
{
queue<int> q;
bool* arr;
int n;
arr = new bool[numVertices];
for (int b = 0; b<numVertices; b++)
arr[b] = 0;
arr[s] = 1;
q.push(s);
while (!q.empty())
{
n = q.front();
cout << n << " ";
q.pop();
for (int v = 0; v<numVertices; v++)
if (adjMatrix[n]
[v] == 1 && !arr[v])
{
q.push(v);
arr[v] = 1;
}
}
}
void Print()
{
for (int i = 0; i < numVertices; i++)
{
cout << i << " : ";
for (int j = 0; j < numVertices; j++)
cout << adjMatrix[i][j] << " ";
cout << "\n";
}
}

~Graph()
{
for (int i = 0; i < numVertices; i++)
delete[] adjMatrix[i];
delete[] adjMatrix;
}
};

int main()
{
Graph *g; //creating an object of graph with 5 vertices
g=new Graph(5);
//inserting edges in the graph
cout << "Initial Matrix"<<endl;

g->Print();
cout<<endl;
g->inserEdge(0,1);
g->inserEdge(0,4);
g->inserEdge(1,0);
g->inserEdge(1,2);
g->inserEdge(1,3);
g->inserEdge(1,4);
g->inserEdge(2,1);
g->inserEdge(2,3);
g->inserEdge(3,1);
g->inserEdge(3,2);
g->inserEdge(3,4);
g->inserEdge(4,0);
g->inserEdge(4,1);
g->inserEdge(4,3);

cout<<"After Insertion :"<<endl;


g->Print();
cout<<endl;
cout<<"Number of edges are "<<g->numberOfEdges()<<endl;
cout<<endl;
cout<<"Degree of vertex "<<g->degree(4)<<endl;
cout<<endl;
cout<<"Output for Depth first search starting from vertex 0 "<<endl;
g->depthfirstSearch(0);
cout<<endl;
cout<<"Output for Breadth first search starting from vertex 0 "<<endl;
g->breadthfirstSearch(0);
cout<<endl;
cout<<endl;
cout<<"**********************************************************"<<endl;
cout<<"After removing: "<<endl;
g->removeEdge(3,1);
cout<<endl;
cout<<"Degree of vertex "<<g->degree(4)<<endl;
cout<<endl;
g->Print();
cout<<endl;
cout<<"Number of edges are "<<g->numberOfEdges()<<endl;
}

ADJANCEY LIST GRAPHS

#include<iostream>
using namespace std;
class Node
{
private:
int data;
Node *link;
public:
Node(int element)
{
data = element;
link = NULL;
}
void SD(int pVal)
{
data = pVal;
}
int GD()
{
return data;
}
void SN(Node *x)
{
link = x;
}
Node* GN()
{
return link;
}

};
class GList
{
private:
Node *start;
int **Adj_List;
int NV;
public:
GList()
{
start = NULL;
NV = 5;
Adj_List = new int*[NV];
for (int i = 0; i < NV; i++)
{
Adj_List[i] = new int[NV];
}

for (int i = 0; i < NV; i++)


for (int j = 0; j < NV; j++)
Adj_List[i][j] = 0;
}

void Insert(Node* PB, Node* pNew)


{
if (start == NULL)
{
start = pNew;
pNew->SN(NULL);
return;
}
else if (PB->GD() == start->GD() && PB->GN() != NULL)
{
Node *temp;
temp = PB;
while (temp->GN() != NULL)
temp = temp->GN();
temp->SN(pNew);
pNew->SN(NULL);
return;
}
else if (PB->GD() == start->GD())
{
PB->SN(pNew);
pNew->SN(NULL);

else if (PB->GN() == NULL)


{
PB->SN(pNew);
pNew->SN(NULL);
}

else
{
pNew->SN(PB->GN());
PB->SN(pNew);
}

void Print()
{
int x=1;
Node *temp;
temp = start;
if (temp->GD() == x)
temp = temp->GN();
cout << "List number " << x++ << ": ";
while (temp)
{
cout << temp->GD() << " ";
temp = temp->GN();
}
cout << endl;
}

};

int main()
{
Node *e, *h, *s;
e = new Node(0);
h = new Node(4);
s = new Node(2);

GList *d, *a, *k;


d = new GList();
a = new GList();
k = new GList();

d->Insert(0, e);
d->Insert(e, h);
d->Insert(e, s);
d->Print();

a->Insert(h, h);
a->Insert(h, e);
a->Insert(h, s);
a->Print();

k->Insert(s, s);
k->Insert(s, e);
k->Insert(s, h);
k->Print();

system("pause");
}
OPEN ENDED{SPLITING}
#include <iostream>
using namespace std;
template<class DT>
class BNode
{
public:
BNode()
{
data=0;
leftchild=NULL;
rightchild=NULL;
}
void setLeftChild(BNode<DT>* n)
{
leftchild=n;

}
BNode<DT>* getLeftChild()
{

return leftchild;
}
void setRightChild(BNode<DT>* n)
{
rightchild=n;

}
BNode<DT>* getRightChild()
{
return rightchild;
}
void setData(DT pdate)
{

data=pdate;
}
DT getData()
{
return data;
}
private:
DT data;
BNode* leftchild;
BNode* rightchild;
};

template<class DT>
class BinarySearchTree
{
public:

BinarySearchTree()
{
root=NULL;
}
bool isEmpty() const
{
return root==NULL;
}

bool insert( DT x)
{

BNode<DT> *t=new BNode<DT>();


t->setLeftChild(NULL);
t->setRightChild(NULL);
t->setData(x);
BNode<DT> *p;
p=NULL;
if(root==NULL)
{
root=t;
}
else
{
BNode<DT>*q;
q=root;
while(q)
{
p=q;
if(t->getData() > q->getData())
{
q=q->getRightChild();
}
else
{
q=q->getLeftChild();
}
if(t->getData()==p->getData())
{

cout<<"Already node :"<<t->getData() << " exists"<<endl;


return false;
}

}
if(t->getData() < p->getData())
{
p->setLeftChild(t);
}
else
{
p->setRightChild(t);
}

}
}

void printSorted()

{
BNode<DT> * ptr =root;
printPreorder(ptr);
}
void printPreorder(BNode<DT> * node)
{
if (node == NULL)
{
return;
}

else
{
cout << node->getData() << " ";
printPreorder(node->getLeftChild());
printPreorder(node->getRightChild());

//~BinarySearchTree();
private:
BNode<DT> * root;
};

int main()
{

BinarySearchTree<int> *BST=new BinarySearchTree<int>();

int size;
cout<<"Size Of Tree: ";
cin>>size;
cout<<endl;
int array[size];

cout<<" Enter Values:";


for(int i=0; i<size; i++)
{
int x;
int s;

array[i]=s;
cin>>s;
BST->insert(s);

cout << endl;


cout << endl;
}
BinarySearchTree<int>* n1;
if ( size % 2 == 0)
{
int x;
cout << "Can be split Equally" << endl;
array[size] = array[size / 2];

for (int i = 1; i <= size/2; i++)


{

cout << size[i];


}
cout<<endl;
for (int i = (size / 2)+1; i < size;i++)
{
cout<<array[i];
}
}
else
{
cout << "Can Not be split Equally" << endl;

}
cout << endl;

cout <<"level Order :"<<endl;


BST->printSorted();
return 0;
}

You might also like