BINARY SEARCH TREE (Using Recursion)
Implemented using C Language
C Language Code:
BINTREE1.CPP
// DSTC --- BINTREE1.CPP .... using BINTREE1.H
// Binary Trees Using Recursion ....
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "bintree1.h"
int main()
{
node *nd=NULL;
node *temp;
int n=0;
clrscr();
while(n>=0)
{
printf("\n Enter Value:");
scanf("%d", &n);
if (n>=0)
nd = nodeadd(nd,n);
}
printf("\n PreOrder :\n");
temp=nd ;
nodeprintPreOrder(temp);
printf("\n InOrder :\n");
temp=nd ;
nodeprintInOrder(temp);
printf("\n PostOrder :\n");
temp=nd ;
nodeprintPostOrder(temp);
killnode(nd) ;
}
BINTREE1.H
// DSTC --- BINTREE1.H ....
// Binary Trees Using Recursion ....
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct nodetype
{
int info ;
struct nodetype *left ;
struct nodetype *right ;
}node;
node *nodeadd(node *nd,int n)
{
if (nd == NULL)
{
nd = (node *)malloc(sizeof(node));
nd->left = NULL;
nd->right = NULL;
nd->info = n ;
}
else if (n< nd->info)
nd->left = nodeadd(nd->left,n) ;
else
nd->right = nodeadd(nd->right,n);
return nd;
}
void killnode(node *nd)
{
if (nd)
{
killnode(nd->left);
killnode(nd->right);
free(nd);
}
}
void nodeprintPreOrder(const node *nd)
{
if (nd)
{
// printing of Binary Tree in PreOrder Traversal
printf("%d, ",nd->info);
nodeprintPreOrder(nd->left);
nodeprintPreOrder(nd->right);
}
}
void nodeprintInOrder(const node *nd)
{
if (nd)
{
// printing of Binary Tree in INORDER Traversal
nodeprintInOrder(nd->left);
printf("%d, ",nd->info);
nodeprintInOrder(nd->right);
}
}
void nodeprintPostOrder(const node *nd)
{
if (nd)
{
// printing of Binary Tree in PostORDER Traversal
nodeprintPostOrder(nd->left);
nodeprintPostOrder(nd->right);
printf("%d, ",nd->info);
}
}
No comments:
Post a Comment