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.
Related Posts:
- Check Computer Science Books
- Apply for Computer Science Internship
- Check Programming Books
- Practice Programming MCQs
- Check Data Structure Books