0% found this document useful (0 votes)
4 views7 pages

7

The document contains a C program that implements both recursive and non-recursive tree traversal methods for a binary tree. It includes functions for recursive preorder, inorder, and postorder traversals, as well as their non-recursive counterparts using a stack. The program creates a sample binary tree and displays the results of each traversal method.

Uploaded by

alladichaitanya4
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)
4 views7 pages

7

The document contains a C program that implements both recursive and non-recursive tree traversal methods for a binary tree. It includes functions for recursive preorder, inorder, and postorder traversals, as well as their non-recursive counterparts using a stack. The program creates a sample binary tree and displays the results of each traversal method.

Uploaded by

alladichaitanya4
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/ 7

7. Write a program to implement the tree traversal methods( Recursive and Non Recursive).

Recursive Tree Traversal Program.

#include <stdio.h>

#include <stdlib.h>

// Define the structure for a tree node

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to create a new tree node with given data

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 Preorder Traversal: Root -> Left -> Right

void recursivePreorder(struct Node* root) {

if (root == NULL) return; // Base case: empty node

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

recursivePreorder(root->left); // Recur on left subtree

recursivePreorder(root->right); // Recur on right subtree

// Recursive Inorder Traversal: Left -> Root -> Right

void recursiveInorder(struct Node* root) {


if (root == NULL) return; // Base case: empty node

recursiveInorder(root->left); // Recur on left subtree

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

recursiveInorder(root->right); // Recur on right subtree

// Recursive Postorder Traversal: Left -> Right -> Root

void recursivePostorder(struct Node* root) {

if (root == NULL) return; // Base case: empty node

recursivePostorder(root->left); // Recur on left subtree

recursivePostorder(root->right); // Recur on right subtree

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

int main() {

// Create a sample binary tree

struct Node* root = createNode(1);

root->left = createNode(2);

root->right = createNode(3);

root->left->left = createNode(4);

root->left->right = createNode(5);

root->right->left = createNode(6);

root->right->right = createNode(7);

// Display recursive traversal results

printf("Recursive Preorder Traversal: ");

recursivePreorder(root);

printf("\nRecursive Inorder Traversal: ");

recursiveInorder(root);
printf("\nRecursive Postorder Traversal: ");

recursivePostorder(root);

return 0;

Non-Recursive Tree Traversal Program.

#include <stdio.h>

#include <stdlib.h>

// Define the structure for a tree node

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to create a new tree node with given data

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

// Define the structure for a stack node


struct Stack {

struct Node* data;

struct Stack* next;

};

// Push a new node onto the stack

void push(struct Stack** stack, struct Node* data) {

struct Stack* newStackNode = (struct Stack*)malloc(sizeof(struct Stack));

newStackNode->data = data;

newStackNode->next = *stack;

*stack = newStackNode;

// Pop a node from the stack

struct Node* pop(struct Stack** stack) {

if (*stack == NULL) return NULL; // Check if stack is empty

struct Stack* temp = *stack;

*stack = (*stack)->next;

struct Node* poppedData = temp->data;

free(temp);

return poppedData;

// Non-Recursive Preorder Traversal: Root -> Left -> Right

void nonRecursivePreorder(struct Node* root) {

if (root == NULL) return;

struct Stack* stack = NULL;

push(&stack, root);

while (stack != NULL) {


struct Node* current = pop(&stack);

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

// Push right and then left child to stack to process left first

if (current->right != NULL) push(&stack, current->right);

if (current->left != NULL) push(&stack, current->left);

// Non-Recursive Inorder Traversal: Left -> Root -> Right

void nonRecursiveInorder(struct Node* root) {

struct Stack* stack = NULL;

struct Node* current = root;

while (current != NULL || stack != NULL) {

// Reach the leftmost node of the current node

while (current != NULL) {

push(&stack, current);

current = current->left;

// Current is NULL at this point, process the top node

current = pop(&stack);

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

current = current->right; // Move to right subtree

// Non-Recursive Postorder Traversal: Left -> Right -> Root

void nonRecursivePostorder(struct Node* root) {

if (root == NULL) return;


struct Stack* stack1 = NULL; // Stack to process nodes

struct Stack* stack2 = NULL; // Stack to reverse order

// Push the root node to the first stack

push(&stack1, root);

while (stack1 != NULL) {

struct Node* current = pop(&stack1);

push(&stack2, current); // Add node to the second stack

// Push left and then right child to first stack

if (current->left != NULL) push(&stack1, current->left);

if (current->right != NULL) push(&stack1, current->right);

// Print nodes from the second stack for postorder traversal

while (stack2 != NULL) {

struct Node* current = pop(&stack2);

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

int main() {

// Create a sample binary tree

struct Node* root = createNode(1);

root->left = createNode(2);

root->right = createNode(3);

root->left->left = createNode(4);

root->left->right = createNode(5);

root->right->left = createNode(6);

root->right->right = createNode(7);
// Display non-recursive traversal results

printf("Non-Recursive Preorder Traversal: ");

nonRecursivePreorder(root);

printf("\nNon-Recursive Inorder Traversal: ");

nonRecursiveInorder(root);

printf("\nNon-Recursive Postorder Traversal: ");

nonRecursivePostorder(root);

return 0;

You might also like