Resources » Articles/Knowledge Sharing » Computer & Technology

c program (binary tree traversals)


Posted Date: 23-Oct-2008  Last Updated:   Category: Computer & Technology    
Author: Member Level: Silver    Points: 1



Program:

/*Binary Tree Travesals*/

#include

#include

#include

typedef struct bst

{

int data;

struct bst *left,*right;

}node;

void insert(node *, node *);

void inorder(node *);

void postorder(node *);

void preorder(node *);

node *get_node();

/*Main Function*/

void main()

{

int ch,ans=5;

node *New,*root;

root=NULL;

clrscr();

while(1)

{

printf("\nEnter:\n1-Create\n2-Preorder\n3-Inorder\n4-Postorder\n5-Exit\n");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("\nEnter Elements 1 by 1: (0 to stop entering)\n");

do

{

New=get_node();

scanf("%d",&New->data);

ans=New->data;

if(ans!=0)

if(root==NULL)

root=New;

else

insert(root,New);

}while(ans!=0);

break;

case 2:

if(root==NULL)

printf("\nNo element In Tree\n");

else

{

printf("\n~~~PREORDER TRAVERSALS~~~\nThe Tree is:\n");

preorder(root);

}

break;

case 3:

if(root==NULL)

printf("\nNo element In Tree\n");

else

{

printf("\n~~~INORDER TRAVERSALS~~~\nThe Tree is:\n");

inorder(root);

}

break;

case 4:

if(root==NULL)

printf("\nNo element In Tree\n");

else

{

printf("\n~~~POSTORDER TRAVERSALS~~~\nThe Tree is:\n");

postorder(root);

}

break;

default:

printf("\n~~~Exit~~~\n");

getch();

exit(0);

break;

}

}

}

/*Get node*/

node *get_node()

{

node *temp;

temp=(node *)malloc(sizeof(node));

temp->left=NULL;

temp->right=NULL;

return temp;

}

/*Insert Function*/

void insert(node *root,node *New)

{

if(New->data < root->data)

{

if(root->left==NULL)

root->left=New;

else

insert(root->left,New);

}

if(New->data > root->data)

{

if(root->right==NULL)

root->right=New;

else

insert(root->right,New);

}

}

/*preorder Traversals*/

void preorder(node *temp)

{

if(temp!=NULL)

{

printf("-> %d ",temp->data);

preorder(temp->left);

preorder(temp->right);

}

}

/*Inorder Traversals*/

void inorder(node *temp)

{

if(temp!=NULL)

{

inorder(temp->left);

printf("-> %d ",temp->data);

inorder(temp->right);

}

}

/*postorder Traversals*/

void postorder(node *temp)

{

if(temp!=NULL)

{

postorder(temp->left);

postorder(temp->right);

printf("-> %d ",temp->data);

}

}



OUTPUT:



/*Binary Tree Traversals*/



Enter:

1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit

1



Enter Elements 1 by 1: (0 to stop entering)

5 3 7 2 4 6 9

0



Enter:

1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit

2



~~~PREORDER TRAVERSALS~~~

The Tree is:

-> 5 -> 3 -> 2 -> 4 -> 7 -> 6 -> 9



Enter:

1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit

3



~~~INORDER TRAVERSALS~~~

The Tree is:

-> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9



Enter:

1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit

4



~~~POSTORDER TRAVERSALS~~~

The Tree is:

-> 2 -> 4 -> 3 -> 6 -> 9 -> 7 -> 5



Enter:

1-Create 2-Preorder 3-Inorder 4-Postorder 5-Exit

5



~~~Exit~~~


Did you like this resource? Share it with your friends and show your love!




Responses to "c program (binary tree traversals)"
Author: Pankaj Gupta    04 Nov 2008Member Level: Gold   Points : 1
Thank you for this binary tree traversal.Can you please provide me information related to double Hashing algorithm in C.
it basically related to DSA Course in B.tech.

Thank for you response.



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.