0% found this document useful (0 votes)
524 views

Binary Tree Traversal

The document describes implementing tree traversals using a linked list data structure in C. It includes algorithms for inorder, preorder and postorder tree traversals. The C program takes user input to build a binary tree and allows selection of traversal type to output the tree nodes in the corresponding order. Sample output is provided showing the tree built and results of each traversal type.

Uploaded by

Senthil Pi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
524 views

Binary Tree Traversal

The document describes implementing tree traversals using a linked list data structure in C. It includes algorithms for inorder, preorder and postorder tree traversals. The C program takes user input to build a binary tree and allows selection of traversal type to output the tree nodes in the corresponding order. Sample output is provided showing the tree built and results of each traversal type.

Uploaded by

Senthil Pi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAM: 05

IMPLEMENTATION OF TREE TRAVERSALS

AIM:To implement tree traversals using linked list. ALGORITHM:Step 1: Start the process. Step 2: Initialize and declare variables. Step 3: Enter the choice. Inorder / Preorder / Postorder. Step 4: If choice is Inorder then -Traverse the left subtree in inorder. -Process the root node. -Traverse the right subtree in inorder. Step 5: If choice is Preorder then -Process the root node. -Traverse the left subtree in preorder. -Traverse the right subtree in preorder. Step 6: If choice is postorder then -Traverse the left subtree in postorder. -Traverse the right subtree in postorder. -Process the root node. Step7: Print the Inorder / Preorder / Postorder traversal. Step 8: Stop the process.

PROGRAM: #include<stdio.h> #include<conio.h> typedef struct bin { int data; struct bin *left; struct bin *right; }node; void insert(node *,node *); void inorder(node *); void preorder(node *); void postorder(node *); node*get_node(); void main() { int choice; char ans='y'; node *new,*root; root=NULL; clrscr(); do { printf("\n Program for Simple Binary Tree"); printf("\n 1.Create"); printf("\n 2.Preorder"); printf("\n 3.inorder"); printf("\n 4.Postorder"); printf("\n 5.Exit"); printf("\n\t Enter Your Choice"); scanf("%d",&choice); switch(choice) { case 1:root=NULL; do { new=get_node(); printf("\n Enter the Element"); scanf("%d",&new->data); if(root==NULL) root=new; else insert(root,new); printf("\n Do you want to enter more data:"); ans=getch(); }while(ans=='y'||ans=='Y'); clrscr(); break; case 2:if(root==NULL)

printf("Tree is not created"); else inorder(root); break; case 3:if(root==NULL) printf("Tree is not created"); else preorder(root); break; case 4:if(root==NULL) printf("Tree is not created"); else postorder(root); break; } }while(choice!=5); } node *get_node() { node *temp; temp=(node*)malloc(sizeof(node)); temp->left=NULL; temp->right=NULL; return temp; } void insert(node *root,node *New) { char ch; printf("\n Where to insert left or right of %d",root->data); ch=getch(); if((ch=='r')||(ch=='R')) { if(root->right==NULL) { root->right=New; } else insert(root->right,New); } else { if(root->left==NULL) { root->left=New; } else insert(root->left,New); } }

void inorder(node *temp) { if(temp!=NULL) { inorder(temp->left); printf("%d",temp->data); inorder(temp->right); } } void preorder(node *temp) { if(temp!=NULL) { printf("%d",temp->data); preorder(temp->left); preorder(temp->right); } } void postorder(node *temp) { if(temp!=NULL) { postorder(temp->left); postorder(temp->right); printf("%d",temp->data); } }

OUTPUT: Program for Simple Binary Tree 1.Create 2.Preorder 3.inorder 4.Postorder 5.Exit Enter Your Choice: 1 Enter the Element: 5 Do you want to enter more data: y Enter the Element: 4 Where to insert left or right of 5: r Do you want to enter more data: y Enter the Element: 6 Where to insert left or right of 5: l Do you want to enter more data: y Enter the Element: 8 Where to insert left or right of 5: r Where to insert left or right of 6: r Do you want to enter more data: n Program for Simple Binary Tree 1.Create 2.Preorder 3.inorder 4.Postorder 5.Exit Enter Your Choice2 4 5 6 8 Program for Simple Binary Tree 1.Create 2.Preorder 3.inorder 4.Postorder 5.Exit

Enter Your Choice3 4 6 8

Program for Simple Binary Tree 1.Create 2.Preorder 3.inorder 4.Postorder 5.Exit 4 Enter Your Choice4 8 6 5

Program for Simple Binary Tree 1.Create 2.Preorder 3.inorder 4.Postorder 5.Exit Enter Your Choice :5

RESULT: Thus the given binary tree traversal has been executed successfully and output is verified.

You might also like