How to write a C Program to implement Binary Tree using linked list. Display the three.. Search and item from the tree with proper message and traverse the tree in Pre Order, Post order and In order in C Programming Language ?
Solution:
/* Write a program to implement Binary Tree using linked list. Display the three.. Search and item from the tree with proper message and traverse the tree in i) Pre Order ii)Post order iii) In order */ #include< stdio.h> #include< conio.h> #include< alloc.h> struct bstree { int data; struct bstree *lchild,*rchild; };/*End of struct bstree*/ typedef struct bstree BST; BST *root=NULL; BST *create(BST *,int); void inorder(BST *); void preorder(BST *); void postorder(BST *); void search(BST *,int); void main() { int ch; int ele; while(1) { clrscr(); printf("\nPress 1 For Insertion."); printf("\nPress 2 For Inorder Traversal."); printf("\nPress 3 For Preorder Traversal."); printf("\nPress 4 For Postorder Traversal."); printf("\nPress 5 For Search Item."); printf("\nPress 6 For Exit."); printf("\nEnter Your Choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter Element: "); scanf("%d",&ele); root=create(root,ele); getch(); break; case 2: inorder(root); getch(); break; case 3: preorder(root); getch(); break; case 4: postorder(root); getch(); break; case 5: printf("\nEnter Element To Be Searched: "); scanf("%d",&ele); search(root,ele); getch(); break; case 6: exit(0); break; default: printf("\nInvalid Choice... Try Again.."); getch(); }/*End of switch(ch)*/ }/*End of while(1)*/ }/*End of void main()*/ BST *create(BST *node,int x) { if(node==NULL) { node=(BST *)malloc(sizeof(BST)); node->lchild=NULL; node->rchild=NULL; node->data=x; }/*End of if(node==NULL)*/ else if(xdata) node->lchild=create(node->lchild,x); else if(x>=node->data) node->rchild=create(node->rchild,x); return node; }/*End of BST *create(BST *,int)*/ void inorder(BST *node) { if(node!=NULL) { inorder(node->lchild); printf("%d ",node->data); inorder(node->rchild); }/*End of if(node!=NULL)*/ }/*End of void inorder(BST *)*/ void preorder(BST *node) { if(node!=NULL) { printf("%d ",node->data); preorder(node->lchild); preorder(node->rchild); }/*End of if(node!=NULL)*/ }/*End of void preorder(BST *node)*/ void postorder(BST *node) { if(node!=NULL) { postorder(node->lchild); postorder(node->rchild); printf("%d ",node->data); }/*End of if(node!=NULL)*/ }/*End of void postorder(BST *node)*/ void search(BST *node,int num) { if(node==NULL) printf("\nThe Number Is Not Present."); else if(node->data==num) printf("\nElement Found."); else if(node->data>num) search(node->lchild,num); else search(node->rchild,num); }/*End of void search(BST *node,int num)*/
Learn More :
Binary Tree
Implementation
- C Program To Implement Heap Sort
- Implements an 8-bit sample and hold ADC on the MSP430
- C Program to Implements a dictionary's functionality.
- C Program to Implements a dictionary's functionality
- C Program to Implementation of List ADT as linked-list
- Implementation of your "Fury of Dracula" Dracula AI
- C Program to Implement Dijkstra's Algorithm
- C Program to Implement Quick Sort
- C Program to Implemention Bubble Sort using array
- C Program to Create, Display, Insert and Deletion of Queue Elements
- Linked List For Getting Employee Details, Display and Search For Salary C Program
Search
- Dictionary Word Search C Program
- C Depth First Search
- C Program To Search A Number Inside The Array
- C Program To Write Data In A File And Search Data From File
- C Program to search an element using linear search or binary search (menu driven program)
- C Program Performs a search and replace for a specified target and replacement string
- C Program Recursive function that searches for a given file in a given folder
- BST Tree in C
- C Program Search Function For Sequence
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
Linked List
- C Program to Demonstrates a linked list for numbers.
- C Program to Implementation of List ADT as linked-list
- C program allocates new nodes and creates a four element list with fixed values
- C Program Simple linked list
- Program to Add Two Polynomials Using Linked List C Program
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
In Order
Item
Display
- DISPLAY SOURCE CODE AS OUTPUT IN C PROGRAM
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program To Display The Number In A Specific Formats
- C Program that Display a IBM Logo
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- LED ON OFF For One Sec/Count and Display on the Attached Serial Monitor
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C Program to accept n numbers & store all prime numbers in an array & display this result
- C program to display the transpose of given 3 X 3 matrix
- C Program to accept a string from user, delete all vowels from that string & display the result
- C Program To Create Two Singly Linked List and Perform Following Operation
- Create Two Singly Linked List Perform Differences Display It C Program
- C Program to Create, Display, Insert and Deletion of Queue Elements
- Creation and Display of Elements in Both Forward and Reverse Direction in a Doubly Linked List
- C Program to Display a real time clock (HH:MM:SS) on the LCD
- Program to Display Pie Chart Accepting User Input C Program
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
- Program to display the following pattern in C
- Display the Following Pattern * ** *** **** ***** C Program
Post Order
Pre Order