From 99ddb715bf024c1b6434d7f30064a36a08ac2d8d Mon Sep 17 00:00:00 2001 From: astral29 <57220751+astral29@users.noreply.github.com> Date: Thu, 31 Oct 2019 17:59:50 +0530 Subject: [PATCH] tree_trv.c --- tree_trv.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tree_trv.c diff --git a/tree_trv.c b/tree_trv.c new file mode 100644 index 0000000..c88e812 --- /dev/null +++ b/tree_trv.c @@ -0,0 +1,57 @@ +#include +#include +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; +}