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

Tree Traversal Methods in C

The document contains two C programs for implementing tree traversal methods on a binary tree: one using recursive techniques and the other using non-recursive techniques with a stack. Each program defines a binary tree structure, creates a sample tree, and performs pre-order, in-order, and post-order traversals, printing the results. Both implementations demonstrate the functionality of tree traversal methods effectively.

Uploaded by

kokkularohan72
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 views6 pages

Tree Traversal Methods in C

The document contains two C programs for implementing tree traversal methods on a binary tree: one using recursive techniques and the other using non-recursive techniques with a stack. Each program defines a binary tree structure, creates a sample tree, and performs pre-order, in-order, and post-order traversals, printing the results. Both implementations demonstrate the functionality of tree traversal methods effectively.

Uploaded by

kokkularohan72
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

DATA STRUCTURES LAB

WEEK-7 EXPERIMENT

1. Write a program to implement the tree traversal methods using Recursive


#include <stdio.h>
#include <stdlib.h>

// Definition of a node in a binary tree


struct Node {
int data;
struct Node *left;
struct Node *right;
}*root=NULL;

// 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 = NULL;
newNode->right = NULL;
return newNode;
}

void preOrder(struct Node* root)


{
if(root != NULL) {
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);
}
}

void inOrder(struct Node* root)


{
if(root != NULL) {
inOrder(root->left);
printf("%d ",root->data);
inOrder(root->right);
}
}

void postOrder(struct Node* root)


{
if(root != NULL) {
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
}

int main() {
// Constructing a binary tree
// 1
// /\
// 2 3
// / \
// 4 5
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);

printf("Pre-order traversal: ");


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

printf("In-order traversal: ");


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

printf("Post-order traversal: ");


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

return 0;
}

2. Write a program to implement the tree traversal methods using Non Recursive
#include <stdio.h>
#include <stdlib.h>

// Definition of a node in a binary tree


struct Node
{
int data;
struct Node *left;
struct Node *right;
}*root=NULL;
int top=-1;
struct Node *s[40];

//push into stack


int push(struct Node *x)
{
s[++top]=x;
}

//pop from stack


struct Node* pop()
{
struct Node *x=s[top--];
return(x);
}

// 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 = NULL;
newNode->right = NULL;
return newNode;
}

void preOrder(struct Node *root)


{
struct Node *ptr;
ptr=root;
if(root==NULL){
printf("\nTree is empty");
}
else
{
push(root);
while(top!=-1)
{
ptr=pop();
if(ptr!=NULL)
{
printf("%d ",ptr->data);
push(ptr->right);
push(ptr->left);
}
}
}
}

void inOrder(struct Node *root)


{
struct Node *ptr;
ptr=root;
if(root==NULL)
{
printf("\nTree is empty");
}
else
{
while(top!=-1||ptr!=NULL)
{
if(ptr!=NULL)
{
push(ptr);
ptr=ptr->left;
}
else{
ptr=pop();
printf("%d ",ptr->data);
ptr=ptr->right;
}
}
}
}

void postOrder(struct Node *root)


{
struct Node *ptr,*temp;
ptr=root;
temp=NULL;
if(root==NULL)
{
printf("\nTree is empty");
}
else{
while(ptr->left!=NULL)
{
push(ptr);
ptr=ptr->left;
}
while(ptr!=NULL){
if(ptr->right==NULL||ptr->right==temp)
{
printf("%d ",ptr->data);
temp=ptr;
ptr=pop();
}
else
{
push(ptr);
ptr=ptr->right;
while(ptr->left!=NULL)
{
push(ptr);
ptr=ptr->left;
}
}
}
}
}

int main() {
// Constructing a binary tree
// 1
// /\
// 2 3
// / \
// 4 5
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);

printf("Pre-order traversal: ");


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

printf("In-order traversal: ");


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

printf("Post-order traversal: ");


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

return 0;
}

You might also like