Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

BINARY SEARCH TREE

#include<stdio.h> #include<stdlib.h> #define NULL 0 struct binary { int number; struct binary *parent; struct binary *rightchild; struct binary *leftchild; }; typedef struct binary node; main() { void create(node *head); void inorder(node *head); node *head; head=(node *)malloc(sizeof(node)); printf("enter number and press -999 at the end"); scanf("%d",&head->number); head->parent=NULL; head->rightchild=NULL; head->leftchild=NULL; create(head); inorder(head); } void create(node *list1) { node *list,*head1; int x; head1=list1; printf("enter number and press -999 at the end"); scanf("%d",&x); if(x==-999) return; else { while(head1!=NULL) { list=head1; if(x<head1->number) { if(head1->leftchild==NULL) {

list->leftchild=(node *)malloc(sizeof(node)); list=list->leftchild; } head1=head1->leftchild; continue; } if(x>=head1->number) { if(head1->rightchild==NULL) { list->rightchild=(node *)malloc(sizeof(node)); list=list->rightchild; } head1=head1->rightchild; } } list->number=x; list->parent=list; list->rightchild=NULL; list->leftchild=NULL; create(list1); } return; } void inorder(node *head1) { if(head1!=NULL) { inorder(head1->leftchild); printf("%d",head1->number); inorder(head1->rightchild); } return; }

You might also like