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

11BST

The document describes a C program to implement a binary search tree. The program allows the user to specify the number of nodes to insert into the tree. It then prompts the user to enter data for each node and inserts the nodes using an insert function. The insert function allocates memory for new nodes and recursively inserts them in the correct position based on whether the data is less than or greater than the current node's data. Finally, a display function prints out the tree by recursively traversing it.

Uploaded by

skyend_512
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

11BST

The document describes a C program to implement a binary search tree. The program allows the user to specify the number of nodes to insert into the tree. It then prompts the user to enter data for each node and inserts the nodes using an insert function. The insert function allocates memory for new nodes and recursively inserts them in the correct position based on whether the data is less than or greater than the current node's data. Finally, a display function prints out the tree by recursively traversing it.

Uploaded by

skyend_512
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

AIM:Write a Program to implement a Binary search tree.

#include <stdio.h> #include <conio.h> #include <alloc.h> struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; }; void insert ( struct btreenode **, int ) ; void display ( struct btreenode * ) ; void main( ) { struct btreenode *bt ; int req, i = 1, num ; bt = NULL ; /* empty tree */ clrscr( ) ; printf ( "Specify the number of items to be inserted: " ) ; scanf ( "%d", &req ) ; while ( i++ <= req ) { printf ( "Enter the data: " ) ; scanf ( "%d", &num ) ; insert ( &bt, num ) ; } printf ( "\n Tree is as follows : \n" ) ; display ( bt ) ; getche(); } /* inserts a new node in a binary search tree */ void insert ( struct btreenode **sr, int num ) { if ( *sr == NULL ) { *sr = malloc ( sizeof ( struct btreenode ) ) ;

( *sr ) -> leftchild = NULL ; ( *sr ) -> data = num ; ( *sr ) -> rightchild = NULL ; return ; } else /* search the node to which new node will be attached */ { /* if new data is less, traverse to left */ if ( num < ( *sr ) -> data ) insert ( &( ( *sr ) -> leftchild ), num ) ; else /* else traverse to right */ insert ( &( ( *sr ) -> rightchild ), num ) ; } return ; } void display ( struct btreenode *sr ) { if ( sr != NULL ) { /* print the data of a node */ printf ( "\t%d", sr -> data ) ; /* traverse till leftchild is not NULL */ display ( sr -> leftchild ) ; /* traverse till rightchild is not NULL */ display ( sr -> rightchild ) ; } else return ; }

OUTPUT
Specify the number of items to be inserted: 6 Enter the data: 76 Enter the data: 34 Enter the data: 90 Enter the data: 56 Enter the data: 34 Enter the data: 23 76 Tree is as follows: 34 23 56 34 90

You might also like