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

Binary Tree Implementation and Traversal Method Code in C

The document contains C code for implementing and traversing a binary tree data structure. It defines functions for creating nodes, inserting nodes as left and right children, and performing inorder, preorder, and postorder tree traversals. These functions are used in a main function to build a sample binary tree and print out the results of each type of traversal.

Uploaded by

Nikhil Kachhawah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Binary Tree Implementation and Traversal Method Code in C

The document contains C code for implementing and traversing a binary tree data structure. It defines functions for creating nodes, inserting nodes as left and right children, and performing inorder, preorder, and postorder tree traversals. These functions are used in a main function to build a sample binary tree and print out the results of each type of traversal.

Uploaded by

Nikhil Kachhawah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Binary Tree Implementation and Traversal Method Code in C

// Tree traversal in C

#include <stdio.h>

#include <stdlib.h>

struct node {

int item;

struct node* left;

struct node* right;

};

// Inorder traversal

void inorderTraversal(struct node* root) {

if (root == NULL) return;

inorderTraversal(root->left);

printf("%d ", root->item);

inorderTraversal(root->right);

// Preorder traversal

void preorderTraversal(struct node* root) {

if (root == NULL) return;

printf("%d ", root->item);

preorderTraversal(root->left);

preorderTraversal(root->right);

// Postorder traversal

void postorderTraversal(struct node* root) {

if (root == NULL) return;

postorderTraversal(root->left);

postorderTraversal(root->right);

printf("%d ", root->item);

// Create a new Node


struct node* create(int value) {

struct node* newNode = malloc(sizeof(struct node));

newNode->item = value;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

// Insert on the left of the node

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

root->left = create(value);

return root->left;

// Insert on the right of the node

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

root->right = create(value);

return root->right;

int main() {

struct node* root = create(1);

insertLeft(root, 4);

insertRight(root, 6);

insertLeft(root->left, 42);

insertRight(root->left, 3);

insertLeft(root->right, 2);

insertRight(root->right, 33);

printf("Traversal of the inserted binary tree \n");

printf("Inorder traversal \n");

inorderTraversal(root);

printf("\nPreorder traversal \n");

preorderTraversal(root);
printf("\nPostorder traversal \n");

postorderTraversal(root);

You might also like