Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Saturday, June 16, 2012

Some sorting methods in C

BubbleSort

#include<stdio.h>
int main()
{
int i,j,t,a[]={3,9,7,1,4},n=5;
for(i=0;i<=(n-2);i++)
for(j=0;j<5-i-1;j++)
{ if(a[j]>a[j+1])
  {t=a[j];
   a[j]=a[j+1];
   a[j+1]=t;
  }
}
for(i=0;i<5;i++)
printf("%d,",a[i]);
return(0);
}


CountingSort

#include<stdio.h>
int main()
{
int i,t,a[]={8,7,4,1,0,5,9,2},b[]={0,0,0,0,0,0,0,0,0,0},j,k=0;
for(i=0;i<8;i++)
printf("%d,",a[i]);
printf("\n\n");
for(i=0;i<8;i++)
{ t=a[i];
  b[t]=b[t]+1;
}
for(i=0;i<10;i++)
printf("%d,",b[i]);
printf("\n\n");
for(i=0;i<10;i++)
{ if(b[i]==0)
  continue;
  t=b[i];
  for(j=0;j<t;j++)
  {a[k]=i;
   k++;
  }
}
for(i=0;i<8;i++)
printf("%d,",a[i]);
printf("\n\n");
/*Time complexity 3n*/
return(0);
}


HeapSort

#include<stdio.h>
int main()
{
int i,a[]={0,68,-7,45,6,57},n=6,t,k,l;
int b[n+1];
int left,right,pos,j,parent;

for(i=0;i<6;i++)
{
b[i+1]=a[i];
j=i+1;
while(1)
{
parent=j/2;
if(parent<1)
break;
if(b[parent]>=b[j])
break;
t=b[parent];
b[parent]=b[j];
b[j]=t;
j=parent;
}
}
for(i=1;i<7;i++)
printf("%d,",b[i]);
printf("\n\n");


for(i=1;i<=n;i++)
{
a[n-i]=b[1];
l=n-i+1;
b[1]=b[l];
l--;
j=1;
while(1)
{
left=2*j;
right=2*j+1;
if(left>l)
break;
if(right>l)
{
if(b[j]>b[left])
break;
t=b[j];
b[j]=b[left];
b[left]=t;
break;
}
if(b[left]>b[right])
pos=left;
else
pos=right;
if(b[j]>b[pos])
break;
t=b[pos];
b[pos]=b[j];
b[j]=t;
j=pos;
}
}

for(i=0;i<6;i++)
printf("%d,",a[i]);
printf("\n\n");

/*Time complexity nlog(base 2)n*/
/*For Quick and Merge too.*/
return(0);
}
InsertionSort

#include<stdio.h>
int main()
{

int i,j,t,a[]={5,9,3,0,1},n=5;
for(i=0;i<=n-2;i++)
{
  if(a[i]<=a[i+1])
  continue;
 
  j=i+1;
  t=a[j];
  while(j>=1 && a[j-1]>t)
  { a[j]=a[j-1];
    j--;
  }
  a[j]=t;
}
for(i=0;i<n;i++)
printf("%d,",a[i]);
return(0);
}

MergeSort

#include<stdio.h>

void mergesort(int a[],int left,int right);
void main()
{
int i;
int a[]={9,6,8,2,5,90,-8,4,1,10};
mergesort(a,0,9);
for(i=0;i<10;i++)
printf("%d,",a[i]);
printf("\n\n");
}
void mergesort(int a[],int left,int right)
{
int c[10];
int p,mid,i,j,k;
if(left>=right)
return;
mid=(left+right)/2;
mergesort(a,left,mid);
mergesort(a,mid+1,right);
i=left;
j=mid+1;
k=0;
while(i<=mid && j<=right)
{
if(a[i]<=a[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=a[j];
j++;
}
k++;
}
if(i<=mid)
for(p=i;p<=mid;p++)
{
c[k]=a[p];
k++;
}
else
for(p=j;p<=right;p++)
{
c[k]=a[p];
k++;
}

for(i=left;i<=right;i++)
a[i]=c[i-left];


printf("\nleft=%d  right=%d\n",left,right);

for(i=0;i<10;i++)
printf("%d,",a[i]);
printf("\n\n");
}
QuickSort

#include<stdio.h>
void quicksort(int a[],int left,int right);
int main()
{
int i,fp,pivot,t,a[]={9,1,17,3,10,-8,5},n=7;
quicksort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d,",a[i]);
return(0);
}
void quicksort(int a[],int left,int right)
{
int i,fp,pivot,t;
if(left>=right)
return;
fp=left;
pivot=a[left];

for(i=left + 1;i<=right;i++)
{
if(a[i]>=pivot)
continue;

fp++;
t=a[i];
a[i]=a[fp];
a[fp]=t;

}
t=a[fp];
a[fp]=pivot;
a[left]=t;
quicksort(a,left,fp-1);
quicksort(a,fp+1,right);
}
SelecionSort

#include<stdio.h>
int main()
{
int i,j,smallpos,a[]={3,9,7,1,4},t;
for(i=0;i<1;i++)
{
  smallpos=i;
  for(j=i+1;j<=4;j++)
  if(a[j]<a[smallpos])
 
  smallpos=j;
  t=a[smallpos];
  a[smallpos]=a[i];
  a[i]=t;
  a[0]=a[i];
 
}
printf("%d",a[0]);
return(0);
}

Saturday, June 2, 2012

Drawing Pyramids


#include<stdio.h>

#include<conio.h>

void main()

{

int n=5,i,j,k;

clrscr();

for(i=1;i<=n;i++)

{

for(j=1;j<=n-1;j++)

printf(" ");

for(k=1;k<=i;k++)

printf("*");

printf("\n");

}

getch();

}

#include<stdio.h>
#include<conio.h>
void main()
{
int n=5,i,j,k;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=0;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("*");
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
void main()
{
int n=5,i,j,k;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
printf("*");
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n=5,i,j,k;
int x,y;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
{
x=i-abs(i-k);
printf("%d",x);
}
printf("\n");
}
getch();
}









#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n=5,i,j,k;
int x,y;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
{
x=i-abs(i-k);
printf("%c",'A' + x -1);
}
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n=5,i,j,k;
int x,y;
clrscr();
for(i=n;i>=1;i--)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
{
x=i-abs(i-k);
printf("%c",'A' + x -1);
}
printf("\n");
}
getch();
}


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n=5,i,j,k;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=i;k++)
{
printf("* ");
}
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
#include<math.h>
int pascalvalue(int y,int x);
void main()
{
int n=5,i,j,k;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=i;k++)
{
printf("%d ",pascalvalue(i,k));
}
printf("\n");
}
getch();
}

int pascalvalue(int y,int x)
{
if(x==1)
return(1);
if(x==y)
return(1);
return(pascalvalue(y-1,x-1) + pascalvalue(y-1,x));
}
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=5,i,j,k;
int mid=(n+1)/2,x,y;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i);
y=mid-x;
for(j=1;j<=y;j++)
printf(" ");
for(k=1;k<=2*x+1;k++)
printf("*");
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=5,i,j,k;
int mid=(n+1)/2,x,y,z;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i);
y=mid-x;
for(j=1;j<=y;j++)
printf(" ");
for(k=1;k<=2*x+1;k++)
{
z=x-abs(x+1-k)+1;
printf("%d",z);
}
printf("\n");
}
getch();
}



#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=5,i,j,k;
int mid=(n+1)/2,x,y,z;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i);
y=mid-x;
for(j=1;j<=y;j++)
printf(" ");
for(k=1;k<=2*x+1;k++)
{
z=x-abs(x+1-k);
printf("%c",'A' + z);
}
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=5,i,j,k;
int mid=(n+1)/2,x,y,l,r;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i)+1;
y=mid-x;
for(j=1;j<=n;j++)
{
l=j;
r=n+1-j;
if(j<=x || r<=x)
printf("*");
else
printf(" ");
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=11,i,j,k;
int mid=(n+1)/2,x,y,l,r,z;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i)+1;
y=mid-x;
for(j=1;j<=n;j++)
{
l=j;
r=n+1-j;
z=abs(mid-j);
if(j<=x || r<=x)
printf("%d",z);
else
printf(" ");
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n=11,i,j,k;
int mid=(n+1)/2,x,y,l,r,z;
clrscr();
for(i=1;i<=n;i++)
{
x=abs(mid-i)+1;
y=mid-x;
for(j=1;j<=n;j++)
{
l=j;
r=n+1-j;
z=mid-abs(mid-j)-1;
if(j<=x || r<=x)
printf("%c",'A' + z);
else
printf(" ");
}
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define n 4
void main()
{

int l=0,r=n-1,t=0,b=n-1,i,x=1,j;
int a[n][n];
clrscr();
while(x<=n*n)
{
for(i=l;i<=r;i++)
a[t][i]=x++;
t++;
for(i=t;i<=b;i++)
a[i][r]=x++;
r--;
for(i=r;i>=l;i--)
a[b][i]=x++;
b--;
for(i=b;i>=t;i--)
a[i][l]=x++;
l++;
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
printf("%4d",a[i][j]);
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define n 4
void main()
{

int l=0,r=n-1,t=0,b=n-1,i,x=1,j;
int a[n][n];
clrscr();
while(x<=n*n)
{
for(i=l;i<=r;i++)
a[t][i]=x++;
t++;
for(i=t;i<=b;i++)
a[i][r]=x++;
r--;
for(i=r;i>=l;i--)
a[b][i]=x++;
b--;
for(i=b;i>=t;i--)
a[i][l]=x++;
l++;
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
printf("%4d",n*n + 1-a[i][j]);
printf("\n");
}
getch();
}

Saturday, May 19, 2012

Implementing 1's Complement & 2's Complement in C

1's complement and 2's complement arithmetic is used in Computer Science for implementing the basic operations of addition and subtraction. In this program , I have endeavored to provide a simple menu based example in C of these basic operations. 

To implement these operations I have developed the following functions:-

void printMenu()
{
clrscr();
gotoxy(5,5);
printf("Read\tPrint\t1'sComplement\t2'sComplement\tAdd\tSub\teXit\n");
printf("A = ");
printNumber(a);
printf("  B = ");
printNumber(b);
printf("  C = ");
printNumber(c);
printf("\n");
}
This is the Menu. It works by pressing R,P,1,2,A,S,X
The menu is not case sensitive.

void copyArray(int dest[],int src[])
{
 int i;
 for(i=0;i<=8;i++)
 dest[i]=src[i];
}
copies the src array into dest.


int getCarry(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(0);
 case 2:return (1);
 default:return(1);
}
}
adds 3 bits and returns the carry.



int getSum(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(1);
 case 2:return (0);
 default:return(1);
}
}
Adds three bits and returns the sum


void TwosComplement(int x[])
{
int temp[]={1,0,0,0,0,0,0,0,0};
int out[9];
copyArray(out,x);
OnesComplement(out);
addNumber(out,temp,x);
}
finds the 2's complement of a number and stores it in the same array.


void OnesComplement(int x[])
{
int i;
for(i=8;i>=0;i--)
x[i]=1-x[i];
}
finds the 1's complement of a number.

void printNumber(int x[])
{
int i;
for(i=8;i>=0;i--)
printf("%d",x[i]);
}
prints the number

void readNumber(int x[])
{
int i;
char str[10];
scanf("%s",str);
for(i=8;i>=0;i--)
x[i]=(str[8-i]-'0'); //0,1,2,3,4 '0'-'0'=0,'1'-'0'=1
 }

reads a number as a string of 9 characters and stores it in a int array.

void subNumber(int x[],int y[],int sub[])
{
int temp[9];
copyArray(temp,y);
TwosComplement(temp);
addNumber(x,temp,sub);
}
subtracts 2 numbers and stores it in the 3rd parameter

Important

Each array is 9 elements, 8 for the byte and the ninth for the sine bit.
Therefore values must be entered as such

Here are some snapshots:-









The Complete Program

Binary.c

#include<stdio.h>                                                  
#include<conio.h>
#include<math.h>
int a[9]={0,0,0,0,0,0,0,0,0},b[9]={0,0,0,0,0,0,0,0,0},c[9]={0,0,0,0,0,0,0,0,0};
void readNumber(int x[]);
void printNumber(int x[]);
void OnesComplement(int x[]);
void addNumber(int x[],int y[],int sum[]);
void TwosComplement(int x[]);
int getSum(int a,int b,int c);
int getCarry(int a,int b,int c);
void copyArray(int dest[],int src[]);
void subNumber(int x[],int y[],int sub[]);
void printMenu();
void main()
{

int option,ch;
clrscr();
while(1)
{
printMenu();
option=getch();
switch(option)
{
case'X':
case'x':
return;
case 'r':
case 'R':
printf("\nEnter a or b: ");
ch=getche();
printf(" = ");
if(ch=='a' || ch=='A')
readNumber(a);
else
readNumber(b);
break;
case '1':
printf("\nEnter a or b: ");
ch=getche();
printf(" = )");
if(ch=='a' || ch=='A')
OnesComplement(a);
else
OnesComplement(b);
break;

case '2':
printf("\nEnter a or b: ");
ch=getche();
printf(" = ");
if(ch=='a' || ch=='A')
TwosComplement(a);
else
TwosComplement(b);
break;
case 'a':
case 'A':
addNumber(a,b,c);
break;
case 's':
case 'S':
subNumber(a,b,c);
break;
}
}
}
void printMenu()
{
clrscr();
gotoxy(5,5);
printf("Read\tPrint\t1'sComplement\t2'sComplement\tAdd\tSub\teXit\n");
printf("A = ");
printNumber(a);
printf("  B = ");
printNumber(b);
printf("  C = ");
printNumber(c);
printf("\n");
}
void copyArray(int dest[],int src[])
{
 int i;
 for(i=0;i<=8;i++)
 dest[i]=src[i];
}
void addNumber(int x[],int y[],int sum[])
{
 int i,s,carry=0;
 for(i=0;i<=8;i++)
{
   s=getSum(x[i] ,y[i],carry);
   carry=getCarry(x[i],y[i],carry);
   sum[i]=s;
}
}
int getCarry(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(0);
 case 2:return (1);
 default:return(1);
}
}
int getSum(int a,int b,int c)
{
int sum=a+b+c;
switch(sum)
{
 case 0:return(0);
 case 1:return(1);
 case 2:return (0);
 default:return(1);
}
}
void TwosComplement(int x[])
{
int temp[]={1,0,0,0,0,0,0,0,0};
int out[9];
copyArray(out,x);
OnesComplement(out);
addNumber(out,temp,x);
}
void OnesComplement(int x[])
{
int i;
for(i=8;i>=0;i--)
x[i]=1-x[i];
}
void printNumber(int x[])
{
int i;
for(i=8;i>=0;i--)
printf("%d",x[i]);
}

void readNumber(int x[])
{
int i;
char str[10];
scanf("%s",str);
for(i=8;i>=0;i--)
x[i]=(str[8-i]-'0'); //0,1,2,3,4 '0'-'0'=0,'1'-'0'=1
 }
void subNumber(int x[],int y[],int sub[])
{
int temp[9];
copyArray(temp,y);
TwosComplement(temp);
addNumber(x,temp,sub);
}
Next, we shall use it to implement the Booth's algorithm

Thursday, April 12, 2012

Exchanging the value of Variables

In this post we shall look at the various ways of interchanging the values of variables. We shall not be looking into methods involving loops, but only simple expression based methods.

Essentially, any binary operation which has an inverse can be used as a basis for the exchange. Thus, we shall use the assignment operator, addition, subtraction, multiplication and division. The last two will require the divisor to be non zero.


Here is the program which does this exchange.

Exchange.c

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a=15,b=10,t;
clrscr();
printf("Original values a=%f,b=%f",a,b);
printf("\nSwap using a temporary variable\n");
t=a;a=b;b=t;
printf("Swapped values a=%f,b=%f",a,b);
printf("\nSwap using a substraction\n");
a=a-b;b=a+b;a=b-a;
printf("Swapped values a=%f,b=%f",a,b);
printf("\nSwap using a addition\n");
a=a+b;b=a-b;a=a-b;
printf("Swapped values a=%f,b=%f",a,b);
printf("\nSwap using a multiplication\n");
a=a*b;b=a/b;a=a/b;
printf("Swapped values a=%f,b=%f",a,b);
printf("\nSwap using a division\n");
a=a/b;b=a*b;a=b/a;
printf("Swapped values a=%f,b=%f",a,b);
getch();
}
This is the output of the program

Original values a=15.000000,b=10.000000
Swap using a temporary variable
Swapped values a=10.000000,b=15.000000
Swap using a substraction
Swapped values a=15.000000,b=10.000000
Swap using a addition
Swapped values a=10.000000,b=15.000000
Swap using a multiplication
Swapped values a=15.000000,b=10.000000
Swap using a division
Swapped values a=10.000000,b=15.000000


Rotation of values
Rotation of values a<--b, b<-- c, c<-- a

This is accomplished through the following program code.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,b=2,c=3,t;
clrscr();
printf("Original values a=%d,b=%d,c=%d\n",a,b,c);
t=a;
a=b;
b=c;
c=t;
printf("Rotated values a=%d,b=%d,c=%d\n",a,b,c);
getch();
}

Here is the output of this program

Original values a=1,b=2,c=3
Rotated values a=2,b=3,c=1


Exchanging two elements via a temporary variable is a special case of the general rotation problem. As an interesting aside we can see that for n elements to be rotated, n rotations would give back the original set.

Friday, January 14, 2011

Breadth First traversal in Binary Trees.

Developed this Program with a friend today. For Depth First traversal a stack is used, and for Breadth First a queue is used.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 10


typedef struct mynode
{
int data;
struct mynode* lft;
struct mynode* rt;
}TreeNode;
typedef struct
{
int front,back,count;
TreeNode* data[SIZE];
}Queue;
void addNode(TreeNode** root,int value);
void enque(Queue* q,TreeNode* link);
void deque(Queue* q,TreeNode** link);
void init(Queue* q);
int isEmpty(Queue q);
void breadthFirst(TreeNode* root);
void main()
{
TreeNode* root=NULL;
clrscr();
addNode(&root,10);
addNode(&root,8);
addNode(&root,9);
addNode(&root,7);
addNode(&root,12);
addNode(&root,11);
addNode(&root,13);
breadthFirst(root);
getch();
}
void breadthFirst(TreeNode* root)
{
Queue q;
init(&q);
enque(&q,root);
while(!isEmpty(q))
{
deque(&q,&root);
if(root==NULL)
continue;
printf("%d,",root->data);
enque(&q,root->lft);
enque(&q,root->rt);
}
}
int isEmpty(Queue q)
{
if(q.count<=0)
return(1);
else
return(0);
}
void deque(Queue* q,TreeNode** link)
{
if(q->count<=0)
printf("\nQueue is Empty\n");
else
{
*link=q->data[q->front];
q->front =(q->front + 1) % SIZE;
q->count--;
if(q->count==0)
{
q->front=-1;
q->back=-1;
}
}
}

void enque(Queue* q,TreeNode* link)
{
if(q->count>=SIZE)
printf("\nQueue is full\n");
else
{
q->back =(q->back + 1) % SIZE;
q->count++;
q->data[q->back]=link;
if(q->count==1)
q->front=0;
}
}
void init(Queue* q)
{
q->count=0;
q->front=-1;
q->back=-1;
}
void addNode(TreeNode** root,int value)
{
if((*root)==NULL)
{
*root=(TreeNode*)malloc(sizeof(TreeNode));
(*root)->data=value;
(*root)->lft=NULL;
(*root)->rt=NULL;
}
else
{
if(value<(*root)->data)
addNode(&((*root)->lft),value);
else
addNode(&(*root)->rt,value);
}
}



Think of this as a supplement to the earlier post


Implementing Binary Trees in C


Monday, November 15, 2010

Implementing Binary Trees in C

This is a program that I had in my mind for almost 3 years before I actually implemented it.
This program uses RECURSION to implement most of the algorithms here.
The DrawTree function is the one that draws the tree. It's based on the simple concept that at the Root Level we have just one node, two at the next, 4 at the next, in general 2 to the power h at level h.
// *******************************Trees.c***********************************


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
typedef struct mynode
{
int value;
struct mynode* left;
struct mynode* right;
}TreeNode;
void AddNode(TreeNode** root,int n);
void InOrder(TreeNode* root);
void PreOrder(TreeNode* root);
void PostOrder(TreeNode* root);
int Max(int a,int b);
int height(TreeNode* root);
void DrawTree(TreeNode* root,int yPosition,int xPosition,int dy,int dx,int level);
void main()
{
int gdriver = DETECT, gmode, errorcode;
TreeNode *root=NULL;
char ch=`1`;
int n;
clrscr();
while(ch!=27)
{
switch(ch)
{
case `A`:
case `a`:printf("\nEnter value to Add = ");
    scanf("%d",&n);
    AddNode(&root,n);
    break;
case `I`:
case `i`:printf("\n Inorder = ");
    InOrder(root);
    printf("\n");
    break;
case `P`:
case `p`:printf("\n PreOrder=");
     PreOrder(root);
     printf("\n");
     break;
case `T`:
case `t`:printf("\n PostOrder=");
     PostOrder(root);
     printf("\n");
     break;
case `d`:
case `D`:initgraph(&gdriver, &gmode, "");
    DrawTree(root,10,getmaxx()/2,getmaxy()/height(root),getmaxx()/2,1);
    getch();
    closegraph();
}
ch=getch();
}
}
void AddNode(TreeNode** root,int n)
{
if(*root==NULL)
{
*root=malloc(sizeof(TreeNode));
(*root)->value=n;
(*root)->left=NULL;
(*root)->right=NULL;
return;
}
if(n<(*root)->value)
AddNode(&(*root)->left,n);
else
AddNode(&(*root)->right,n);
}
void InOrder(TreeNode* root)
{
if(root==NULL)
return;
InOrder(root->left);
printf("%d,",root->value);
InOrder(root->right);
}
void PreOrder(TreeNode* root)
{
  if(root==NULL)
  return;
 printf("%d,",root->value);
 PreOrder(root->left);
 PreOrder(root->right);
}
void PostOrder(TreeNode* root)
{
   if(root==NULL)
   return;
  PostOrder(root->left);
 PostOrder(root->right);
printf("%d,",root->value);
}
void DrawTree(TreeNode* root,int yPosition,int xPosition,int dy,int dx,int level)
{
char s[80];
int radius=10,cx,cy,adjust=5;
if(root==NULL)
return;
if(level==1)
{
circle(xPosition,yPosition,radius);
itoa(root->value,s,10);
outtextxy(xPosition-adjust,yPosition-adjust,s);
DrawTree(root->left,yPosition,xPosition,dy,-dx/2,level+1);
DrawTree(root->right,yPosition,xPosition,dy,dx/2,level+1);
}
else
{
cx=xPosition + dx;
cy=yPosition + dy;
circle(cx,cy,radius);
itoa(root->value,s,10);
outtextxy(cx-adjust,cy-adjust,s);
line(xPosition,yPosition,cx,cy);
DrawTree(root->left,cy,cx,dy,-abs(dx)/2,level+1);
DrawTree(root->right,cy,cx,dy,abs(dx)/2,level+1);
}
}
int Max(int a,int b)
{
if(a>b)
return(a);
else
return(b);
}
int height(TreeNode* root)
{
if(root==NULL)
return(0);
return(1 + Max(height(root->left),height(root->right)));
}



// *******************************Trees.c***********************************
There is a supplementary post

Breadth First traversal in Binary Trees.