Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tree_trv.c #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions tree_trv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newnode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
void printpostorder(struct node* node)
{
if (node == NULL)
return;
printpostorder(node->left);
printpostorder(node->right);
printf("%d ", node->data);
}
void printinorder(struct node* node)
{
if (node == NULL)
return;
printinorder(node->left);
printf("%d ", node->data);
printinorder(node->right);
}
void printpreorder(struct node* node)
{
if (node == NULL)
return;
printf("%d ", node->data);
printpreorder(node->left);
printpreorder(node->right);
}
int main()
{
struct node *root = newnode(8);
root->left=newnode(65);
root->right=newnode(25);
root->left->left=newnode(41);
root->left->right=newnode(51);
root->right->left=newnode(-51);
root->right->left->left=newnode(85);
printf("\nPreorder traversal of binary tree is \n");
printpreorder(root);
printf("\nInorder traversal of binary tree is \n");
printinorder(root);
printf("\nPostorder traversal of binary tree is \n");
printpostorder(root);
return 0;
}