The document discusses creating a binary search tree of characters and traversing it recursively in different orders. Functions are defined to insert nodes, delete nodes, find the minimum and maximum nodes, and traverse the tree in inorder, preorder, and postorder.
The document discusses creating a binary search tree of characters and traversing it recursively in different orders. Functions are defined to insert nodes, delete nodes, find the minimum and maximum nodes, and traverse the tree in inorder, preorder, and postorder.
Write a C program that uses functions to perform the following:
a) Create a binary search tree of characters. b) Traverse the above Binary search tree recursively in preorder, Postorder, inorder.
AIM: To create a binary search tree of characters and to traverse recursively in
postorder. DESCRIPTION: A binary search tree (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node's left subtree and smaller than the keys in all nodes in that node's right subtree.
PROGRAM: Binary Search Tree
#include<stdio.h> #include<stdlib.h> struct node { char data; struct node *left; struct node *right; }*ptr; struct node *searched,*max,*min; struct node *deleted(struct node *ptr,char ele); struct node *insert(struct node *ptr,char ele); struct node *findmin(struct node *ptr); struct node *findmax(struct node *ptr); struct node *search(struct node *ptr,char ele); void inorder(struct node *ptr); void preorder(struct node *ptr); void postorder(struct node *ptr); int main() { int ch; char ele; ptr=NULL; printf("\n ----Main Menu---"); printf("\n1.Insert\n2.Delete\n3.Search\n4.Inoredr\n5.Preorder\n6.Post order\n7.Findmin\n8.Findmax\n9.Exit"); while(1) { printf("\nEnter ur choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter an element to insert : "); scanf("\n%c",&ele); ptr=insert(ptr,ele); break; case 2: printf("\nEnter element to delete : "); scanf("\n%c",&ele); ptr=deleted(ptr,ele); printf("\n Deleted Successfully"); break; case 3: printf("\nEnter a element to search : "); scanf("\n%c",&ele); searched=search(ptr,ele); if(searched==NULL) printf("\nElement is not found"); else printf("\nElement is found"); break; case 4: inorder(ptr); break; case 5: preorder(ptr); break; case 6: postorder(ptr); break; case 7: min=findmin(ptr); if(min!=NULL) printf("\n Minimum value is %c" ,min->data); else printf("\nTree is empty"); break; case 8: max=findmax(ptr); if(max!=NULL) printf("\n Maximum value is %c ",max->data); else printf("\nTree is empty"); break; case 9: exit(0); default: printf("\n Wrong choice"); } } }
{ if(ptr!=NULL) { while(ptr->left!=NULL) ptr=ptr->left; return(ptr); } else return(NULL); } struct node *findmax(struct node *ptr) { if(ptr!=NULL) { while(ptr->right!=NULL) ptr=ptr->right; return(ptr); } else return(NULL); } struct node *search(struct node *ptr,char ele) { if(ptr==NULL) return NULL; else { if(ele<ptr->data) return(search(ptr->left,ele)); else if(ele>ptr->data) return(search(ptr->right,ele)); else if(ele==ptr->data) return(ptr); else return(NULL); } } void inorder(struct node *ptr) { if(ptr==NULL) return; else { inorder(ptr->left); printf("%c ",ptr->data); inorder(ptr->right); } } void preorder(struct node *ptr) { if(ptr==NULL) return; else { printf("%c ",ptr->data); preorder(ptr->left); preorder(ptr->right); } } void postorder(struct node *ptr) { if(ptr==NULL) return; else { postorder(ptr->left); postorder(ptr->right); printf("%c ",ptr->data); } } OUTPUT: ----Main Menu--- 1.Insert 2.Delete 3.Search 4.Inoredr 5.Preorder 6.Postorder 7.Findmin 8.Findmax 9.Exit Enter ur choice : 1 Enter an element to insert : C Enter ur choice : 1 Enter an element to insert : A Enter ur choice : 1 Enter an element to insert : B Enter ur choice : 1 Enter an element to insert : E Enter ur choice : 1 Enter an element to insert : F Enter ur choice : 1 Enter an element to insert : D Enter ur choice : 4 ABCDEF Enter ur choice : 5 CABEDF Enter ur choice : 6 BADFEC Enter ur choice : 2 Enter element to delete : C Deleted Successfully Enter ur choice : 4 ABDEF Enter ur choice : 5 DABEF Enter ur choice : 6 BAFED Enter ur choice : 7 Minimum value is A Enter ur choice : 8 Maximum value is F Enter ur choice : 3 Enter a element to search : A Element is found Enter ur choice : 9