Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
Binary.search.tree.c

Binary.search.tree.c

Ratings: (0)|Views: 112 |Likes:
Published by Farhan Ahmad
C code for binary search
C code for binary search

More info:

Published by: Farhan Ahmad on Oct 24, 2009
Copyright:Public Domain

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

02/11/2011

pdf

text

original

 
/** Implementation of a Binary Search Tree in C* Author : Ahmad F.* Date : 2009.AUG.20 00:20 HRS* Version : 0.0**/#include<stdio.h>#include<stdlib.h>#include<ctype.h>struct node{int data;struct node *rchild;struct node *lchild;};typedef struct node NODE;struct family{NODE *self;NODE *parent;};typedef struct family FAMILY;NODE *CreateBSTree(void);void InsertBSTree(NODE*,int);void DeleteBSTree(NODE*,int);void PrintTree(NODE*);FAMILY *Exists(NODE*,int);int GetMaxLeft(NODE*);/** entry point for main()*/int main(void){NODE *root;char choice;int value;while(1){printf("\nWhat do yo wish to do?");printf("\n[C]: Create");printf("\n[I]: Insert");printf("\n[D]: Delete");printf("\n[Q]: Quit");printf("\n>>");scanf(" %c",&choice);
 
choice = toupper(choice);switch (choice){case 'C':{root= CreateBSTree();break;}case 'Q':{exit(EXIT_SUCCESS);}case 'I':{printf("\nEnter value for new node>>");scanf("%d",&value);InsertBSTree(root,value);break;}case 'D':{printf("\nEnter value to be deleted>>");scanf("%d",&value);DeleteBSTree(root,value);break;}default:{printf("\nInvalid choice %c",choice);break;}}//end of switch(choice)printf("\n");PrintTree(root);printf("\n");}//End of whileexit(EXIT_SUCCESS);}//End of main()/** Entry point for CreateBSTree()
 
*/NODE *CreateBSTree(){NODE *root;int value;root = (NODE*)malloc(sizeof(NODE));root->rchild = NULL;root->lchild = NULL;printf("\nEnter value for root node>>");scanf("%d",&value);root->data = value;return root;}//End of CreateBSTree()/** Entry point for InsertBSTree()*/void InsertBSTree(NODE* root,int value){NODE *NEW;/** check if value is already present*/if( root->data == value ){printf("\nvalue %d already exists",value);return;}else if(value > root->data){if(root->rchild == NULL){NEW = (NODE*)malloc(sizeof(NODE));NEW->data = value;NEW->rchild = NULL;NEW->lchild = NULL;root->rchild = NEW;}else{InsertBSTree(root->rchild,value);}}else if(value < root->data){

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->