0% found this document useful (0 votes)
15 views5 pages

C Program for Binary Search Tree

The document describes a C program to implement a binary search tree. It defines a node structure with left and right pointers and includes functions to insert nodes, search for a node, and traverse the tree using inorder, preorder and postorder traversal. The main function allows the user to insert nodes, search, and traverse the tree by calling the respective functions.

Uploaded by

kutti21
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)
15 views5 pages

C Program for Binary Search Tree

The document describes a C program to implement a binary search tree. It defines a node structure with left and right pointers and includes functions to insert nodes, search for a node, and traverse the tree using inorder, preorder and postorder traversal. The main function allows the user to insert nodes, search, and traverse the tree by calling the respective functions.

Uploaded by

kutti21
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

PROGRAM TO IMPLEMENT BINARY SEARCH TREE IN C.

#include<stdio.h>

struct BT
{
int data;

struct BT *right, *left;


};

void insert( struct BT ** ptr, int d )


{
if ( ( *ptr ) == NULL )
{

( *ptr ) = ( struct BT* ) malloc( sizeof( struct BT ) );


( *ptr ) ->data = d;
( *ptr ) ->left = ( *ptr ) ->right = NULL;
}
else
{
if ( ( *ptr ) ->data > d )
insert( &( ( *ptr ) ->left ), d );
else
insert( &( ( *ptr ) ->right ), d );
}

return ;
}

int search( struct BT *ptr, int no )


{
if ( ptr == NULL )
return ( 0 );

if ( ptr->data == no )
return ( 1 );

if ( ptr->data > no )
return ( search( ptr->left, no ) );
else
return ( search( ptr->right, no ) );
}

void inorder( struct BT *ptr )


{
if ( ptr == NULL )
return ;
else
{
inorder( ptr->left );
printf( "\t%d", ptr->data );
inorder( ptr->right );
}
}

void preorder( struct BT*ptr )


{
if ( ptr == NULL )
return ;
else
{
printf( "\t%d", ptr->data );
preorder( ptr->left );
preorder( ptr->right );
}
}

void postorder( struct BT*ptr )


{
if ( ptr == NULL )
return ;
else
{
postorder( ptr->left );
postorder( ptr->right );
printf( "\t%d", ptr->data );
}
}

main()
{

struct BT * root;
int ch, d, no, f;

root = NULL;

while ( ch != 6 )
{
printf( "\n [Link]\n [Link]\n [Link]\n [Link]\n [Link]\n [Link]\n" );
printf( "\n Enter the choice:" );
scanf( "%d", &ch );

switch ( ch )
{

case 1:
printf( "Enter the data:" );
scanf( "%d", &d );
insert( &root, d );
break;

case 2:
printf( "Enter the node:" );
scanf( "%d", &no );
f = search( root, no );

if ( f == 0 )
printf( "Node is not present" );
else
printf( "Node is present" );

break;

case 3:
inorder( root );

break;

case 4:
preorder( root );

break;

case 5:
postorder( root );

break;

case 6:
break;
}
}
}

/******************OUTPUT******************

[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:1


Enter the data:2
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:1


Enter the data:54

[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:1


Enter the data:32

[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:1


Enter the data:33

[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:2


Enter the node:2
Node is present
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:3


2 32 33 54
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:1


Enter the data:13

[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:4


2 54 32 13 33
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:5


13 33 32 54 2
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Enter the choice:6


*********************************************/

Common questions

Powered by AI

The main function provides a menu for the user with options to insert a node, search for a node, and perform inorder, preorder, and postorder traversals. It continuously loops until the user chooses to exit the program, responding to user input by calling the corresponding function based on the menu choice .

A binary search tree node in the provided C implementation consists of an integer data field and two pointers: left and right, each pointing to the left and right child nodes, respectively .

The inorder traversal visits nodes in left-root-right order and prints nodes in non-decreasing order. Preorder traversal visits in root-left-right, printing root before subtrees, useful for copying trees. Postorder traversal visits in left-right-root order, important for deleting a tree because it starts with leaves .

Performing an inorder traversal after inserting the nodes 2, 54, 32, and 33 yields the output 2, 32, 33, 54. This order is significant because inorder traversal prints the nodes in non-decreasing order, demonstrating the sorted order inherent in a binary search tree .

The insertion function first checks if the current pointer is null. If it is, a new node is created and initialized with the data. If the current node's data is greater than the data to be inserted, the function calls itself recursively to insert the data in the left subtree; otherwise, it inserts it into the right subtree .

Improvements could include consistently using descriptive variable names, such as renaming 'd', 'no', and 'f' to 'data', 'node_to_search', and 'found', respectively. Adding comments explaining each function's purpose and parameters would aid understanding. Modularizing the code further into separate functions or files and following formatting conventions would enhance readability and maintainability .

The postorder traversal after inserting nodes 2, 54, 32, 33, and 13 results in the order 13, 33, 32, 54, 2. This traversal is significant because it processes child nodes before their parent, making it the optimal sequence for operations like deletion, where subtrees need to be processed before the parent node .

The search function will return that the node is present because during insertion, node 2 is placed as the root, and the searching process begins at the root. Since the root node matches the search query, it returns 1, confirming the node's presence .

The search function operates recursively. If the current node is null, it returns 0, indicating the node was not found. If the current node's data matches the target value, it returns 1. If the target value is smaller than the current node's data, the search proceeds to the left subtree; otherwise, it goes to the right subtree .

To include error handling during node insertion, implement checks for successful memory allocation with 'malloc'. If memory allocation fails, handle it by printing an error message and/or terminating the program to avoid segmentation faults. Additionally, add range checks for input data to ensure they meet predefined constraints .

You might also like