answersLogoWhite

0

#include<stdio.h>

#include<stdlib.h>

//#include<conio.h>

struct treenode

{

int ele;

struct treenode *l_child, *r_child;

};

struct treenode *insert_node(struct treenode *t,int a);

void TraverseInorder(struct treenode *t);

void TraversePreorder(struct treenode *t);

void TraversePostorder(struct treenode *t);

/*main function*/

int main()

{

struct treenode *root_node = NULL;

int num,value;

int choice;

//clrscr();

printf("----------------------------------------------------\n");

printf("\t\t\tMENU\n");

printf("-----------------------------------------------------\n");

printf("[1] Create a Binary Tree and Use Inorder Traversal\n");

printf("[2] Create a Binary Tree and Use Preorder Traversal\n");

printf("[3] Create a Binary Tree and Use Postorder Traversal\n");

printf("-----------------------------------------------------\n");

printf("Enter your choice:");

scanf("%d",&choice);

if(choice>0 & choice<=3)

{

printf("\nEnter the number of nodes:");

scanf("%d",&num);

while(num-- > 0)

{

printf("\n\nEnter the data value:");

scanf("%d",&value);

root_node = insert_node(root_node,value);

}

switch(choice)

{

case 1:

printf("\n\nBinary tree using Inorder Traversal : ");

TraverseInorder(root_node);

getch();

break;

case 2:

printf("\n\nBinary tree using Preorder Traversal : ");

TraversePreorder(root_node);

getch();

break;

case 3:

printf("\n\nBinary tree using Postorder Traversal : ");

TraversePostorder(root_node);

getch();

break;

default:

printf("Invalid Choice");

break;

}

}

}

/*end main*/

/* Function to create a Binary Tree of integers data */

struct treenode *insert_node(struct treenode *t,int a)

{

struct treenode *temp_node1,*temp_node2;

if(t NULL)

{

printf("Value cannot be allocated.\n");

exit(0);

}

temp_node2->ele = a;

temp_node2->l_child=temp_node2->r_child = NULL;

}

}

return(t);

}

/* Function for Traversing the binary tree in inorder. */

void TraverseInorder(struct treenode *t)

{

if(t != NULL)

{

TraverseInorder(t->l_child);

printf("%d\t",t->ele);

in_order(t->r_child);

}

}

/* Function for Traversing the binary tree in preorder. */

void TraversePreorder(struct treenode *t)

{

if(t != NULL)

{

printf("%d\t",t->ele);

TraversePreorder(t->l_child);

TraversePreorder(t->r_child);

}

}

/* Function for Traversing the binary tree in postorder. */

void TraversePostorder(struct treenode *t)

{

if(t != NULL)

{

TraversePostorder(t->l_child);

TraversePostorder(t->r_child);

printf("%d\t",t->ele);

}

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

What is a real world example for a binary tree?

A binary tree is simply a way to create a flow chart for decisions. An example of a real life binary tree is anything that requires a series of yes or no answers.


C program which accepts in order and preorder traversal outputs of a binary tree as input and prints the corresponding binary tree?

any body can help on this ?


Write a program in C programming language to list the nodes of a binary tree in the following way List the root then nodes at depth 1 followed by nodes at depth 2 and so on?

tell me , how we create a username &amp; password for many users in c language.


How many types of binary tree?

A binary tree is type of tree with finite number of elements and is divided into three main parts. the first part is called root of the tree and itself binary tree which exists towards left and right of the tree. There are a no. of binary trees and these are as follows : 1) rooted binary tree 2) full binary tree 3) perfect binary tree 4) complete binary tree 5) balanced binary tree 6) rooted complete binary tree


Copy of binary tree?

Is another binary tree.


Are binary tree and binary tree same?

Yes.


Is a full binary tree the same as a complete binary tree, or are there differences between the two structures?

A full binary tree is a type of binary tree where each node has either 0 or 2 children. A complete binary tree is a binary tree where all levels are fully filled except possibly for the last level, which is filled from left to right. So, a full binary tree can be a complete binary tree, but not all complete binary trees are full binary trees.


When converting binary tree into extended binary tree all the original nodes in binary tree are?

will remain same


Does binary tree and binary search tree same?

no they are not same


What are the application of binary tree in computer science?

What are the applications of Binary Tree.


What is meant by left skewed binary tree?

a binary tree with only left sub trees is called as left skewed binary tree


What is incomplete binary tree?

Incomplete Binary Tree is a type of binary tree where we do not apply the following formula: 1. The Maximum number of nodes in a level is 2