This C Program constructs Tree & Perform Insertion, Deletion, Display.
Here is source code of the C Program to construct a Tree & Perform Insertion, Deletion, Display. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Construct a Tree & Perform Insertion, Deletion, Display
*/
#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL;
// Function Prototype
void printout(struct btnode*);
struct btnode* newnode(int);
void main()
{
root=newnode(50);
root->l=newnode(20);
root->r=newnode(30);
root->l->l=newnode(70);
root->l->r=newnode(80);
root->l->r->r=newnode(60);
root->l->l->l=newnode(10);
root->l->l->r=newnode(40);
printf("tree elements are\n");
printf("\nDISPLAYED IN INORDER\n");
printout(root);
printf("\n");
}
// Create a node
struct btnode* newnode(int value)
{
struct btnode* node = (struct btnode*)malloc(sizeof(struct btnode));
node->value = value;
node->l = NULL;
node->r = NULL;
return(node);
}
// to display the tree in inorder
void printout (struct btnode *tree)
{
if (tree->l)
printout(tree->l);
printf("%d->", tree->value);
if (tree->r)
printout(tree->r);
}
$ gcc tree1.c $ a.out tree elements are DISPLAYED IN INORDER 10->70->40->20->80->60->50->30
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Information Technology Internship
- Practice Design & Analysis of Algorithms MCQ
- Apply for Computer Science Internship
- Apply for Data Structure Internship
- Practice Programming MCQs