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

Binary Tree Program

The document describes tree traversal algorithms for binary trees: 1. Inorder traversal visits the left subtree, then the root, then the right subtree. Preorder visits the root first, then left subtree, then right subtree. Postorder visits left subtree, then right subtree, then the root. 2. Sample C code is provided to implement tree nodes, tree insertion functions, and functions for the three traversal algorithms using recursion. 3. The code constructs a sample binary tree and prints the results of each traversal algorithm to demonstrate their output order.

Uploaded by

Apoorv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Binary Tree Program

The document describes tree traversal algorithms for binary trees: 1. Inorder traversal visits the left subtree, then the root, then the right subtree. Preorder visits the root first, then left subtree, then right subtree. Postorder visits left subtree, then right subtree, then the root. 2. Sample C code is provided to implement tree nodes, tree insertion functions, and functions for the three traversal algorithms using recursion. 3. The code constructs a sample binary tree and prints the results of each traversal algorithm to demonstrate their output order.

Uploaded by

Apoorv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Inorder traversal

1. First, visit all the nodes in the left subtree


2. Then the root node
3. Visit all the nodes in the right subtree
inorder(root->left)
display(root->data)
inorder(root->right)

Preorder traversal
1. Visit root node
2. Visit all the nodes in the left subtree
3. Visit all the nodes in the right subtree
display(root->data)
preorder(root->left)
preorder(root->right)

Postorder traversal
1. visit all the nodes in the left subtree
2. visit the root node
3. visit all the nodes in the right subtree
postorder(root->left)
postorder(root->right)
display(root->data)
/*********Program *****************/
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node* left;
struct node* right;
};
void inorder(struct node* root){
if(root == NULL) return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}

void preorder(struct node* root){


if(root == NULL) return;
printf("%d ->", root->data);
preorder(root->left);
preorder(root->right);
}

void postorder(struct node* root) {


if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}

struct node* createNode(value){


struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

struct node* insertLeft(struct node *root, int value) {


root->left = createNode(value);
return root->left;
}

struct node* insertRight(struct node *root, int value){


root->right = createNode(value);
return root->right;
}

int main(){
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);

insertLeft(root->left, 5);
insertRight(root->left, 6);

printf("Inorder traversal \n");


inorder(root);

printf("\nPreorder traversal \n");


preorder(root);

printf("\nPostorder traversal \n");


postorder(root);
}
The output of the code will be

Inorder traversal
5 ->12 ->6 ->1 ->9 ->
Preorder traversal
1 ->12 ->5 ->6 ->9 ->
Postorder traversal
5 ->6 ->12 ->9 ->1 ->
Using Stack:

You might also like