#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct BinaryTree
{
struct BinaryTree *LeftChild;
int data;
struct BinaryTree *RightChild;
};
void insert(struct BinaryTree **,int);
void inorder(struct BinaryTree *);
void main()
{
struct BinaryTree *BTree;
int Data[10]={11,2,9,13,57,25,17,1,90,3};
int i;
BTree=NULL;
clrscr();
printf(" Binary Tree Sort .\n");
printf("\nArray :\n");
for(i=0;i<10;i++)
printf("%d\t",Data[i]);
for(i=0;i<=9;i++)
insert(&BTree,Data[i]);
printf("\n In-order traversal of Binary Tree : \n");
inorder(BTree);
getch();
}
void insert(struct BinaryTree **sr,int num)
{
if(*sr == NULL)
{
(*sr)=malloc(sizeof(struct BinaryTree));
(*sr)->LeftChild=NULL;
(*sr)->data=num;
(*sr)->RightChild=NULL;
}
else
{
if(num < (*sr)->data)
insert(&((*sr)->LeftChild),num);
else
insert(&((*sr)->RightChild),num);
}
}
void inorder(struct BinaryTree *sr)
{
if(sr!=NULL)
{
inorder(sr -> LeftChild);
printf("%d\t",sr->data);
inorder(sr -> RightChild);
}
}
No comments:
Post a Comment