0% found this document useful (0 votes)
21 views

Binary Search

The document contains code to implement a binary search tree using C. It defines a node structure with left and right child pointers and data. It also contains functions to insert nodes, traverse the tree in pre-order, in-order and post-order manners and print the values.

Uploaded by

Muhammad Nameer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Binary Search

The document contains code to implement a binary search tree using C. It defines a node structure with left and right child pointers and data. It also contains functions to insert nodes, traverse the tree in pre-order, in-order and post-order manners and print the values.

Uploaded by

Muhammad Nameer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<stdlib.h>
#define NODECOUNT T
struct bstNode
{
int data;
struct bstNode *lchild, *rchild;
};
struct bstNode *root=NULL;
int bstData[]={100,80,120,70,90,110,130};
int count=0;
struct bstNode *implement BSTree(int n)
{
struct bstNode *newnode;
if(n>=NODECOUNT)
return NULL;
newnode=(struct bstNode *)malloc(sizeof(struct bstNode));
newnode->lchild=implmentBSTree((2*n)+1);
newnode->data=bstData[n];
newnode->rchild=implement BSTree((2*n)+2);
return newnode;
}
void preOrder(struct bstNode * myNode)
{
if(myNode)
{
printf("%d\t",myNode->data);
printf(myNode->lchild);
preOrder(myNode->rchild);
}
return;
}
void inOrder(struct bstNode *myNode)
{
if(myNode)
{
inOrder(myNode->lchild);
printf("%d\t",myNode->data);
inOrder(myNode->rchild);
}
return;
}
void postOrder(struct bstNode * myNode)
{
if(myNode)
{
postOrder(myNode->lchild);
postOrder(myNode->rchild);
printf("%d\t,myNode->data);
}
return;
}
int main()
{
int i=0;
clrscr();
printf("Data in Array: \n");
while(i<NODECOUNT)
{
printf("%d\t",bstData[i]);
i++;
}
i=0;
root=implementBSTree(i);
printf("\nPre-Order\n");
preOrder(root);
printf("\ninOrder : \n");
inOrder(root);
printf("\nPost Order :\n");
postOrder(root);
printf("\n:");
getch();
return 0;
}

You might also like