Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views4 pages

Iprepost

The document contains C code for a binary tree implementation, including functions to create nodes and perform inorder, preorder, and postorder traversals. It defines a binary tree structure and demonstrates the traversal methods on a sample tree. The output of each traversal method is provided as comments in the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Iprepost

The document contains C code for a binary tree implementation, including functions to create nodes and perform inorder, preorder, and postorder traversals. It defines a binary tree structure and demonstrates the traversal methods on a sample tree. The output of each traversal method is provided as comments in the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>

// Definition of a binary tree node


struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Recursive inorder traversal function


void inorderTraversal(struct Node* root) {
if (root == NULL)
return;

inorderTraversal(root->left); // Traverse left subtree


printf("%d ", root->data); // Visit node
inorderTraversal(root->right); // Traverse right subtree
}

int main() {

struct Node* root = createNode(4);


root->left = createNode(2);
root->right = createNode(5);
root->left->left = createNode(1);
root->left->right = createNode(3);

printf("Inorder Traversal: ");


inorderTraversal(root);
printf("\n");

return 0;
}
Output: 1 2 3 4 5
/ Example tree:
4
/\
2 5
/\
1 3
#include <stdio.h>
#include <stdlib.h>

// Definition of a binary tree node


struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Preorder traversal: Root -> Left -> Right


void preorderTraversal(struct Node* root) {
if (root == NULL)
return;

printf("%d ", root->data); // Visit root node


preorderTraversal(root->left); // Traverse left subtree
preorderTraversal(root->right); // Traverse right subtree
}

int main() {

struct Node* root = createNode(4);


root->left = createNode(2);
root->right = createNode(5);
root->left->left = createNode(1);
root->left->right = createNode(3);

printf("Preorder Traversal: ");


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

return 0;
} // Output: 4 2 1 3 5
// Same example tree:
// 4
// / \
// 2 5
// / \
// 1 3
#include <stdio.h>
#include <stdlib.h>

// Definition of a binary tree node


struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Inorder traversal: Left -> Root -> Right


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

// Preorder traversal: Root -> Left -> Right


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

// Postorder traversal: Left -> Right -> Root


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

int main() {
// Example tree:
// 4
// / \
// 2 5
// / \
// 1 3
struct Node* root = createNode(4);
root->left = createNode(2);
root->right = createNode(5);
root->left->left = createNode(1);
root->left->right = createNode(3);

printf("Inorder Traversal: ");


inorderTraversal(root); // Output: 1 2 3 4 5
printf("\n");

printf("Preorder Traversal: ");


preorderTraversal(root); // Output: 4 2 1 3 5
printf("\n");

printf("Postorder Traversal: ");


postorderTraversal(root); // Output: 1 3 2 5 4
printf("\n");

return 0;
}

You might also like