Friday, May 1, 2015

Find Minimum node make it Root node attach rest of Tree with it such that BST property is not violated

Find Minimum node make it Root node attach rest of Tree with it such that BST property is not violated 


#include<iostream.h>
#include<conio.h>
class Node{
private:
int data,i;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


Node* remove(Node *root, int value){
Node *t;
int cmpr = value - root->data;
if (cmpr < 0){
t = remove(root->left, value);
root->left = t;
} else if (cmpr > 0){
t = remove(root->right, value);
root->right = t;
} else if (root->left != NULL && root->right != NULL){
Node* minNode;
minNode = findMin(root->right);
root->data = minNode->data;
t = remove(root->right, minNode->data);
root->right = t;
} else {
Node* nodeToDelete = root;
if (root->left == NULL)
root = root->right;
else if (root->right == NULL)
root = root->left;
else root = NULL;
delete nodeToDelete;
}       return root;
}

void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}       Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}

Node* findMin(Node* te)
{
if (te->left == NULL){
return te;           }
return findMin(te->left);
}

 void _preorder(Node *root){
if (root == NULL) return;
else {  B[i]=root->data;
i++;
_preorder(root->left);
_preorder(root->right);
} }

void preorder(Node *root){
if (root == NULL) return;
else {  cout <<root->data <<"  ";B[i]=root->data;
i++;
preorder(root->left);
preorder(root->right);
} }

void inorder(Node *root){
if (root == NULL) return;
else {
inorder(root->left);
cout << root->data <<"  ";
inorder(root->right);
} }



 void min_to_root(){
root->data=(findMin(root)->data);
remove(root->left,root->data);
i=0;
cout<<endl;
_preorder(root);
}
int B[];


};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b,obj;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\n\nInorder triversal: ";
b.inorder(b.root);
cout<<"\n\nPreOrder triversal: ";
b.preorder(b.root);
b.min_to_root();

   for(int j=0;j<=10;j++){
   obj.insert(obj.B[j]);}

  cout<<"\n\nAfter Process: \n\nInorder: ";
  obj.inorder(b.root);

  cout<<"\n\nPreorder: ";
  obj.preorder(b.root);

   getch();

}


Traverse Tree According To User Value :Get Value From User ☻ :~C++ Code Tree

Traverse Tree According To User Value :Get Value From User ☻

#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}




void inorder(Node *root){
if(root == NULL) return;
else{
inorder(root->left);
cout <<root->data <<"  ";
inorder(root->right);
}    }


void preorder(Node *root){
if (root == NULL) return;
else {
cout << root->data <<"  ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root){
if (root == NULL) return;
else {
postorder(root->left);
postorder(root->right);
cout << root->data <<"  ";
}
}



};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
while(getch()!=30){

cout<<"\n\n\n\tSelect A Mode: \n\n1 for Inorder Treverse\n2 for PostOrder Treverse\n3 for PreOrder Treverse\n";
cin>>c;
switch(c){
case 1:{
cout<<"\n\nInorder: ";
b.inorder(b.root);
break;
}
case 2:
{
cout<<"\n\nPost Order: ";
b.postorder(b.root);
break;
}
case 3:
{
cout<<"\n\nPre Order: ";
b.preorder(b.root);
break;
}
default:{
}
}
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";
cout<<"\n\nPress Space Bar To Exit || Enter To Continue \n\n";
}
getch();

}



Traverse Left Subtree with Post Order and Right Sub tree with PreOrder ~ C++ Codes Tree

Traverse root left sub tree with post order ☻
and then print root ☻
and then root right sub tree with pre order ☻

#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}




void mixorder(Node *root){
cout<<"\n\nPost Order: ";
postorder(root->left);
cout <<"\n\nRoot: "<<root->data <<"  ";
cout<<"\n\nPre Order: ";
preorder(root->right);
}


void preorder(Node *root){
if (root == NULL) return;
else {
cout << root->data <<"  ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root){
if (root == NULL) return;
else {
postorder(root->left);
postorder(root->right);
cout << root->data <<"  ";
}
}



};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\n\n Traverse Left Subtree with Post Order and Right Sub tree with PreOrder\n\n";
b.mixorder(b.root);
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";

getch();

}


Insert A Value on Left Side which is greater Then Root and Small Value on Right side ~ C++ Tree codes

Insert A Value on Left Side which is greater Then Root and Small Value on Right side

#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data< q->data)
q = q->right;
else q = q->left;
}
if (newnode->data< p->data)
p->right = newnode;
else p->left = newnode;
}




void inorder(Node *root){
if (root == NULL) return;
else {
inorder(root->left);
cout << root->data <<"  ";
inorder(root->right);
}
}

void preorder(Node *root){
if (root == NULL) return;
else {
cout << root->data <<"  ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root){
if (root == NULL) return;
else {
postorder(root->left);
postorder(root->right);
cout << root->data <<"  ";
}
}



};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\n\nGreater then root on left side and smaller values on Right side\n\n";
cout<<"\n\nInorder triversal: ";
b.inorder(b.root);
cout<<"\n\nPreorder triversal: ";
b.preorder(b.root);
cout<<"\n\nPostorder triversal: ";
b.postorder(b.root);
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";

getch();

}





Convert Left subtree into right subtree and right into left in tree C++ code: ~ BST

Convert Left Sub Tree of a Root into Right Sub Tree and Right sub Tree into Left Sub 
Tree ☺ ☻


#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}




void inorder(Node *root){
if (root == NULL) return;
else {
inorder(root->left);
cout << root->data <<"  ";
inorder(root->right);
}
}

void preorder(Node *root){
if (root == NULL) return;
else {
cout << root->data <<"  ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root){
if (root == NULL) return;
else {
postorder(root->left);
postorder(root->right);
cout << root->data <<"  ";
}
}
//Function is used to join left sub tree on right side of root
//and right sub tree on left side of root
void exchange(){
Node *temp=root->left;
root->left=root->right;
root->right=temp;
}


};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\n\nInorder triversal: ";
b.inorder(b.root);
cout<<"\n\nPreorder triversal: ";
b.preorder(b.root);
cout<<"\n\nPostorder triversal: ";
b.postorder(b.root);
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";
cout<<"\n\nPress Enter to see result after exchanging right subtree with left subtree\n\n";
getch();
b.exchange();
cout<<"\n\nInorder triversal: ";
b.inorder(b.root);
cout<<"\n\nPreorder triversal: ";
b.preorder(b.root);
cout<<"\n\nPostorder triversal: ";
b.postorder(b.root);
getch();


}



Thursday, April 30, 2015

Swapping Without Temporary variable C++

Swap Two No without using any temporary variable 
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"\n\n--------Program To Swap Two Numbers without Temprary veriale-------\n\n";
cout<<"\n\nEnter First No: ";
cin>>a;
cout<<"\n\nEnter Secont No: ";
cin>>b;
cout<<"\n\nA= "<<a<<" B= "<<b;
cout<<"\n\n----------Swap-------------\n\n";
a=a+b;
b=a-b;
a=a-b;
cout<<"\n\nA= "<<a<<" B=  "<<b;
getch();
}


Tuesday, April 21, 2015

BST (Binary Search Tree) in C++

Read Also:  


Creation of BST(binary Search Tree),Insertion at any point,Deletion At any point,Maximum Value,Minimum Value,Searching in tree 

[[Important Data Structure Codes in C++]]
Tree Non Liner Data Structure


#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;

void insert(int data){

Node* newnode = new Node();

newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}

Node* remove(Node *root, int value){
Node *t;
int cmp = value - root->data;

if (cmp < 0){
t = remove(root->left, value);
root->left = t;
}
else if (cmp > 0){
t = remove(root->right, value);
root->right = t;
}
else if (root->left != NULL && root->right != NULL){
Node* minNode;
minNode = findMin(root->right);
root->data = minNode->data;
t = remove(root->right, minNode->data);
root->right = t;
}
else {

Node* nodeToDelete = root;
if (root->left == NULL) //will handle 0 children
root = root->right;
else if (root->right == NULL)
root = root->left;
else root = NULL;
delete nodeToDelete;
}

return root;
}

Node* findMin(Node* tree)
{
if (tree == NULL)
return NULL;
if (tree->left == NULL)
return tree;
return findMin(tree->left);
}

void inorder(Node *root){
if (root == NULL) return;
else {
inorder(root->left);
cout << root->data <<"  ";
inorder(root->right);
}
}



void search(Node *root,int v)
{
Node *q=root;
while(v != q->data)
{
if(v < q->data)
{
q=q->left;
}
else
{
q=q->right;
}
}
cout<<"\nNode is  found= "<<q->data<<endl;

}

Node *min_value(Node *r)
{
Node *q=r;
Node *p;
if(q!=NULL)
{
p=q;
q=q->left;
min_value(q);
 }
else
cout<<"\n Minimum Value = "<<p->data<<endl;
return q;

}

Node  *max_value(Node *r)
{
Node *q=r;
Node *p;
if(q!=NULL)
{
p=q;
q=q->right;
max_value( q);
}
else
cout<<"\n Maximum Value= "<<p->data<<endl;
return q;
}
};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
b.inorder(b.root);
getch();cout<<"\n\nEnter Value For Search ";cin>>c;
b.search(b.root,c); getch();
b.min_value(b.root);getch();
b.max_value(b.root);getch();
cout<<"\n\nEnter Value For Delete: ";cin>>c;
b.remove(b.root, c);
b.inorder(b.root);getch();
cout<<"\n\nInsert a New Value ";cin>>c;
b.insert(c);
b.inorder(b.root);
getch();

}



Thursday, March 26, 2015

Program To Find Factorial Of any no ~ Assembly language

include irvine32.inc

.data
msg byte "Please enter a number: ",0,13h,10h
num dword ?
fact dword 1

.code
main proc
call clrscr
call crlf
mov edx,offset msg
call writestring
call readint
mov num,eax
mov ecx,num
mov ebx,1

D2:
mov eax,fact
mul ebx
mov fact,eax
inc ebx
loop D2

call crlf
mov eax,fact
call writeint
exit
main endp
end main

Find No is Prime or Consonant ~ Assembly Language

include irvine32.inc
.data
msg1 byte "Plese Enter the No to Find its Prime or not :",0,13h,10h
msg2 byte "No is Prime",0,13h,10h
msg3 byte "No is Consunant",0,13h,10h
num dword ?
loop1 dword ?
result dword ?
.code
main proc
call clrscr
call crlf
mov edx,offset msg1
call writestring
call readint
mov num,eax

mov loop1,eax
sub loop1,2
mov eax,loop1
mov ecx,eax
mov ebx,2

L1:
mov eax,num
mov edx,0
div ebx
mov result,edx

cmp result,0
je b
jne next
next:
inc ebx
loop L1

call crlf
mov edx,offset msg2
call writestring
jmp s
b:
call crlf
mov edx,offset msg3
call writestring
s:
exit
main endp
end main


Program To Find No is Even or Odd ~ Assembly Language

include irvine32.inc
.data
msg byte "Plese enter a no : ",0,13h,10h
msg1 byte "NO is Even ",0,13h,10h
msg2 byte "No is Odd ",0,13h,10h
num word ?
result word ?
.code
main proc
call clrscr
call crlf
mov edx,offset msg
call writestring
call readint
mov num,ax
mov bx,2
mov ax,num
mov dx,0
div bx
mov result,dx
cmp result,0
JE E
JNE O

E:
call crlf
mov edx,offset msg1
call writestring
jmp stop
O:
call crlf
mov edx,offset msg2
call writestring

stop:
exit
main endp
end main


Sunday, March 22, 2015

C++ Program to print Hexagonal Shape

#include<iostream.h>
#include<conio.h>
void main()
{   int d=0,b=0;
clrscr();
for(int i=9;i<=19;i++){
gotoxy(i,12);
cout<<char(2);
}
for(int j=9;j<=19;j++){
gotoxy(j,26);
cout<<char(2);
}
d=9;
for( b=8;b<=14;b++){
d=d-1;
gotoxy(d,b+5);
cout<<char(2);
} d=19;
for( b=18;b<=24;b++){
d=d+1;
gotoxy(d,b-5);
cout<<char(2);
} d=9;
for( b=20;b>=14;b--){
d=d-1;
gotoxy(d,b+5);
cout<<char(2);
}d=27;
for( b=14;b<=20;b++){
d=d-1;
gotoxy(d,b+5);
cout<<char(2);
}
cout<<"\n\n\n\n Who's it is :) . . . .";
getch();}


Binary Search ~ Program to search a value from array using Binary search

#include<iostream.h>
#include<conio.h>

void binary_search(int *A,int value,int s,int e);

void display(int *arr,int count)
{
for(int i=0;i<count;i++)
{ cout<<arr[i]<<"\t";}
}
void binary_search(int *A,int value,int s,int e)
{
int mid=0;
mid=(s+e)/2;
if(value==A[mid])
{ cout<<"\nValue found\n"; }
else if(value>A[mid])
{ s=mid+1;
 binary_search(A,value,s,e);
}
else if(value<A[mid])
{
e=mid-1;
binary_search(A,value,s,e);
}
else
{ cout<<"not found";}
}

void main()
{
clrscr();
int A[]={5,4,9,7,8,1,3,2,6},value=0;
cout<<"\nEnter Value You Want to Search: ";
cin>>value;
binary_search(A,value,0,9);
cout<<"\n\nArray is following:\n";
display(A,9);

getch();
}


Saturday, March 21, 2015

Program To Print Table of Any Number :: Assembly Language

Read Also:  

include irvine32.inc

.data
result dword ?
num dword ?
msg byte "Enter number You Want To Print Table",0,13h,10h
msg1 byte " * ",0
msg2 byte " = ",0

.code
main proc
call clrscr
call crlf
mov edx,offset msg
call writestring
call crlf
call readint
mov num,eax
mov ecx,10
mov ebx,1
L1:
mov eax,num
call writeint
mul ebx
mov edx,offset msg1
call writestring
mov result,eax
mov eax,ebx
call writeint
mov edx,offset msg2
call writestring
mov eax,result
call writeint
call crlf
inc ebx
loop L1
exit
main endp
end main




Saturday, March 14, 2015

How To Program In Assembly Language

As we Know Assembly Language is a Low Level language ...
 So Their is a Compiler IDE for almost every programming language so for assembly we use Masm615 to compile over assembly code .

Download MASAM615 :: :: Click Here-

Download masm615 from above link and extract it in any where in your PC Disk drives..
 How to Extract: 
Move that .Rar file to any disk or folder then Right Click on it and Chose Extract Files like

then open notepad and write your code in it like


and Save As it with extension of .asm 

Complete code of Hello World in Assembly  

Then save that Notepad file in Your Masm615 Folder
 Now How To Compile it:
Open cmd (Command Prompt )


it whould be like it

so if you save masm615 in Your C: Disk then For go back to C:
just Type CD \


then it will go back to C:
if you save Your Masm in other disk simple type Disk Name with Colon like D: , E: 


now to open Masm615 simple type cd masm615


Now type make32 and the name of notepad file

like: make32 hello

and press ENTER


if their is no error in your code it will compile successfully and show cmd like this then again press Enter 


and type again the name of your note pad file



and that's it your output is here in the cmd


Small review :
C:\Document and setting\ugd>
C:\Document and setting\ugd>cd \
C:\>cd masm615
C:\masm615>
C:\masm615>make32 hello
.
.
.
Press any key to continue [Enter]
C:\masm615>hello


OUTPUT ON THE TOP OF CMD SCREEN

IMPORTANT PROGRAMS IN ASSEMBLY LANGUAGE