Professional Documents
Culture Documents
#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++)
{
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;
}
}
void input ()
{
cout << endl;
cout << " Enter your MATRIX :" << endl;
{
cout << " " ;
cin >> p[i][j];
}
}
cout << endl;
}
void disp2D ()
{
// original Matrix
cout << endl;
}
cout << endl << endl;
}
}
void dispRowMajor ()
{
rowmajor[i * c + j] = p[i][j];
}
}
//matrix Row Major
}
}
void Multiply_rowMajor (matrix & x, int &row, int &col2)
{
}
for (int i = 0; i < row; ++i)
{
{
m[i][j] = 0;
}
}
for (int i = 0; i < row; i++)
}
}
}
// multiplied matrix row major
}
cout << endl << endl;
}
};
int main ()
{
int row;
int col;
a.input ();
int row2;
int col2;
b.input ();
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");
#include <iostream>
using namespace std;
private:
T data;
Node * next;
public:
Node ()
{
Node (T element)
{
data = element;
next = NULL;
}
data = pVal;
}
T getData ()
{
return data;
}
next = e;
}
Node * getnext ()
{
return next;
}
};
private:
Node < T > *first;
public:
List ()
{
first = NULL;
void Insert (Node < T > *pBefore, Node < T > *pNew)
{
if (first == NULL)
first = pNew;
}
pBefore->setnext (pNew);
pNew->setnext (NULL);
else
pBefore->setnext (pNew);
temp = first;
if (pToBeDeleted == first)
delete pToBeDeleted;
temp->setnext (NULL);
delete pToBeDeleted;
else
delete pToBeDeleted;
void printList ()
temp = first;
while (temp)
};
int main ()
{
list->printList ();
list->Delete (a);
list->printList ();
list->printList ();
return 0;
#include <iostream>
using namespace std;
private:
T data;
Node * next;
public:
Node ()
{
Node (T element)
{
data = element;
next = NULL;
}
data = pVal;
}
T getData ()
{
return data;
}
next = e;
}
Node * getnext ()
{
return next;
}
};
private:
Node < T > *last;
public:
List ()
{
last = NULL;
}
void Insert (Node < T > *pBefore, Node < T > *pNew)
{
if (last == NULL)
last = pNew;
}
pBefore->setnext (pNew);
pNew->setnext (last);
else
pBefore->setnext (pNew);
temp = last;
if (pToBeDeleted == last)
delete pToBeDeleted;
temp->setnext (NULL);
delete pToBeDeleted;
else
delete pToBeDeleted;
void printList ()
temp = last;
int n=1;
if (temp!=NULL)
{
do
{
cout << n << " = " << temp->getData() << endl;
temp=temp->getnext();
n++;
}
while(temp!=last);
}
}
};
int main ()
{
list->printList ();
list->Delete (a);
list->printList ();
list->printList ();
return 0;
public:
Node (T element)
{
data = element;
next = NULL;
prevs = NULL;
data = pVal;
}
T getData ()
{
return data;
Node * getNext ()
{
return next;
next = x;
}
void setprevs (Node * x)
{
prevs = x;
}
Node * get_prevs ()
{
return prevs;
}
private:
T data;
Node * next;
Node * prevs;
};
private:
Node < T > *first;
public:
DList ()
{
first = NULL;
void Insert (Node < T > *pBefore, Node < T > *pNew)
{
if (first == NULL)
first = pNew;
pBefore->setNext (pNew);
pNew->setprevs (pBefore);
pNew->setNext (NULL);
else
pBefore->setNext (pNew);
pNew->setprevs (pBefore);
temp = first;
if (pToBeDeleted == first)
first->setprevs (NULL);
delete pToBeDeleted;
temp->setNext (NULL);
delete pToBeDeleted;
else
{
void printList ()
{
while (temp)
{
}
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 ()
{
list->printList ();
list->Delete (a);
list->printList ();
//cout << "Printing in reverse order:" << endl;
//list-> reverseList();
return 0;
#include<iostream>
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;
}
#include <iostream>
using namespace std;
class node
{
public:
T data;
node<T>* next;
};
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)
{
}
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()
{
return 0;
}
#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";
}
}
}
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;
}
if (!first) {
first = pNew;
return;
}
pNew -> next = pBefore -> next;
pBefore -> next = pNew;
}
temp -> setNext(pToBeDeleted -> next);
delete pToBeDeleted;
}
void printList() {
if (var == NULL)
cout << " empty linked list." << endl;
}
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() {
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++;
}
#include <iostream>
using namespace std;
public:
Node (T element)
{
data = element;
data = pVal;
}
T gdata ()
{
return data;
Node * gnext ()
{
return link;
link = x;
}
private:
T data;
Node * link;
};
public:
List ()
{
first = NULL;
}
void Insert (Node < T > *pBefore, Node < T > *pNew)
{
if (first == NULL)
first = pNew;
pBefore->snext (pNew);
pNew->snext (NULL);
else
pBefore->snext (pNew);
temp = first;
if (pToBeDeleted == first)
delete pToBeDeleted;
}
temp->snext (NULL);
delete pToBeDeleted;
else
delete pToBeDeleted;
private:
Node < T > *first;
};
private:
Node < T > *front;
front = NULL;
rear = NULL;
if (IsEmpty ())
else
temp->snext (rear);
rear = temp;
T Get ()
{
T x = rear->gdata ();
delete temp;
return x;
bool IsEmpty ()
{
if (rear == NULL)
return true;
else
return false;
bool IsFull ()
{
return false;
};
int
main ()
{
if (q->IsEmpty ())
q->Put (3);
q->Put (2);
q->Put (1);
}
return 0;
system ("pause");
Queues By array
#include<iostream>
using namespace std;
class Queue
{
T * arr;
int front;
int rear;
int Ms;
public:
Ms = size;
front = 0;
rear = -1;
}
void Put (T element)
{
if (IsFull ())
return;
rear++;
arr[rear] = element;
T Get ()
{
if (IsEmpty ())
return 0;
else
{
T element;
element = arr[front];
front++;
return element;
bool IsFull ()
{
if (rear == Ms)
return true;
else
return false;
bool IsEmpty ()
{
return true;
else
return false;
};
int
main ()
{
if (q->IsEmpty ())
q->Put (1);
q->Put (2);
q->Put (3);
while (!q->IsEmpty ())
}
return 0;
system ("pause");
Palindrome
#include<iostream>
#include<string>
using namespace std;
class Queue
{
T * arr;
int front;
int rear;
int Ms;
public:
Ms = size;
front = 0;
rear = -1;
}
void Put (T element)
{
if (IsFull ())
rear++;
arr[rear] = element;
T Get ()
{
if (IsEmpty ())
return 0;
else
T element;
element = arr[front];
front++;
return element;
bool IsFull ()
{
if (rear == Ms)
return true;
else
return false;
bool IsEmpty ()
{
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;
}
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;
}
}
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);
}
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);
}
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());
else
{
return(RightDepth + 1);
}
}
}
void Depth()
{
}
private:
BNode<T>*root;
};
int main()
{
BinaryTree<int> *BT=new BinaryTree<int>();
BT->BuildTree(Arr,15);
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;
int Greatest = t;
int Lvalue = 2 * t + 1;
int Rvalue = 2 * t + 2;
if (Greatest != t)
{
swap(Array[t], Array[Greatest]);
}
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();
#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)
{
}
if(t->getData() < p->getData())
{
p->setLeftChild(t);
}
else
{
p->setRightChild(t);
}
}
}
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()
{
BST->insert(12);
BST->insert(4);
BST->insert(9);
BST->insert(2);
BST->insert(14);
BST->insert(16);
BST->insert(13);
BST->insert(12);
if(BST->remove(2))
{
cout<<"-> "<<"Node carrying 2 removed successfully"<<endl;
}
if(BST->remove(12))
{
cout<<"-> "<<"Node carrying 12 removed successfully"<<endl;
}
cout << endl;
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;
#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;
}
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);
#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];
}
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);
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)
{
}
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()
{
int size;
cout<<"Size Of Tree: ";
cin>>size;
cout<<endl;
int array[size];
array[i]=s;
cin>>s;
BST->insert(s);
}
cout << endl;