#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<iostream.h>
#define max 10
/*max is the maximum number of nodes in BST*/
int midx; // mid value of getmaxx()
struct node
{
int value;
int prob;
struct node *left,*right;
}*first,*p1,*p2; // NODE OF BST
struct node * create_bst(struct node*,int,int);
void print_bst(struct node*,int ,int,int,int col);
void draw_optimaltree(int,int,int,int,int,int);
int comparison[max+2][max+1],root_matrics[max+2][max+1];
int num[max]; //value of bst
int prob[max]; // Probability of corresponding value
void main()
{
int i,gd,gm;
clrscr();
gd=DETECT;
initgraph(&gd,&gm,"");
first=NULL;
randomize();
midx=getmaxx();
char str[10];
int j=0;
for(i=0;i<max;i++)
{
num[i]=random(100);
prob[i]=random(10)+1;
sprintf(str,"%5d",num[i]);
// outtextxy(0,j,str);
j=j+10;
}
int temp;
p1=first;
for(i=0;i<max;i++)
first=create_bst(first,num[i],prob[i]); //binary tree has been drawn
setfillstyle(1,2);
setcolor(14);
print_bst(first,0,getmaxx(),10,1);
setfillstyle(1,2);
setcolor(14);
outtextxy(0,0,"BINARY SEARCH TREE");
getch();
closegraph();
}
struct node *create_bst(struct node *p,int val,int prob)
{
if(p==NULL)
{
p=(struct node *)malloc(sizeof(struct node));
p->value=val;
p->prob=prob;
p->left=NULL;
p->right=NULL;
return p;
}
else
{
if(val<=p->value)
{
p->left=create_bst(p->left,val,prob);
}
else
{
p->right=create_bst(p->right,val,prob);
}
}
return p;
}
void print_bst(struct node *p,int x1,int x2,int yy,int col)
{ //x1 x2 are two x coordinate b/w them node is there
//yy is height of node
// col is color
if(p==NULL)
{
// return 0;
}
else
{
char str[10];
sprintf(str,"%d",p->value,p->prob);
// cout<<"\nFirst node is :"<<p->value;
if(p->left!=NULL)
{
line((x1+x2)/2,yy,(x1+(x1+x2)/2)/2,yy+50);
}
if(p->right!=NULL)
{
line((x1+x2)/2,yy,((x1+x2)/2+x2)/2,yy+50);
}
setfillstyle(1,col);
// fillellipse((x1+x2)/2,yy,10,10);
setcolor(col);
pieslice((x1+x2)/2,yy,0,360,10);
setcolor(14);
if(col==5)
{
outtextxy((x1+x2)/2,yy-4,str);
}
else
{
setcolor(15);
outtextxy((x1+x2)/2-15,yy-4,str);
}
print_bst(p->left,x1,(x1+x2)/2,yy+50,3);
print_bst(p->right,(x1+x2)/2,x2,yy+50,5);
free(p);
}
}