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

DS LAB MANUAL new (6)

Uploaded by

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

DS LAB MANUAL new (6)

Uploaded by

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

return 0;

7. Write a program to implement the tree traversal methods using recursive and Non recursive.

/* C Program to Traverse the Tree Recursively */

#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child and a pointer to right child */
struct node {
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node with the given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node
= (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/* Given a binary tree, print its nodes according to the "bottom-up" postorder traversal. */
void printPostorder(struct node* node)
{
if (node == NULL)
return;
// first recur on left subtree
printPostorder(node->left);
// then recur on right subtree
printPostorder(node->right);
// now deal with the node
printf("%d ", node->data);
}
/* Given a binary tree, print its nodes in inorder*/
void printInorder(struct node* node)
{
if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);
/* then print the data of node */
printf("%d ", node->data);
/* now recur on right child */
printInorder(node->right);

32
}
/* Given a binary tree, print its nodes in preorder*/
void printPreorder(struct node* node)
{
if (node == NULL)
return;
/* first print data of node */
printf("%d ", node->data);
/* then recur on left sutree */
printPreorder(node->left);
/* now recur on right subtree */
printPreorder(node->right);
}
int main()
{
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("\nPreorder traversal of binary tree is \n");
printPreorder(root);
printf("\nInorder traversal of binary tree is \n");
printInorder(root);
printf("\nPostorder traversal of binary tree is \n");
printPostorder(root);
getchar();
return 0;
}

OUTPUT:

Preorder traversal of binary tree is


12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231

33
Non Recursive

/* C Program to Traverse the Tree Non-Recursively */


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

struct node
{
int a;
struct node *left;
struct node *right;
};

void generate(struct node **, int);


int search(struct node *, int);
void delete(struct node **);

int main()
{
struct node *head = NULL;
int choice = 0, num, flag = 0, key;

do
{
printf("\nEnter your choice:\n1. Insert\n2. Search\n3. Exit\nChoice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter element to insert: ");
scanf("%d", &num);
generate(&head, num);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
flag = search(head, key);
if (flag)
{
printf("Key found in tree\n");
}
else
{
printf("Key not found\n");
}

34
break;
case 3:
delete(&head);
printf("Memory Cleared\nPROGRAM TERMINATED\n");
break;
default: printf("Not a valid input, try again\n");
}
} while (choice != 3);
return 0;
}

void generate(struct node **head, int num)


{
struct node *temp = *head, *prev = *head;

if (*head == NULL)
{
*head = (struct node *)malloc(sizeof(struct node));
(*head)->a = num;
(*head)->left = (*head)->right = NULL;
}
else
{
while (temp != NULL)
{
if (num > temp->a)
{
prev = temp;
temp = temp->right;
}
else
{
prev = temp;
temp = temp->left;
}
}
temp = (struct node *)malloc(sizeof(struct node));
temp->a = num;
if (num >= prev->a)
{
prev->right = temp;
}
else
{
prev->left = temp;
}
}
}

35
int search(struct node *head, int key)
{
while (head != NULL)
{
if (key > head->a)
{
head = head->right;
}
else if (key < head->a)
{
head = head->left;
}
else
{
return 1;
}
}
return 0;
}

void delete(struct node **head)


{
if (*head != NULL)
{
if ((*head)->left)
{
delete(&(*head)->left);
}
if ((*head)->right)
{
delete(&(*head)->right);
}
free(*head);
}
}

Output:

Enter your choice:


1. Insert
2. Search
3. Exit
Choice: 1

36
Enter element to insert: 1

Enter your choice:


1. Insert
2. Search
3. Exit
Choice: 1
Enter element to insert: 2

Enter your choice:


1. Insert
2. Search
3. Exit
Choice: 1
Enter element to insert: 3

Enter your choice:


1. Insert
2. Search
3. Exit
Choice: 1
Enter element to insert:
4

Enter your choice:


1. Insert
2. Search
3. Exit
Choice: 2
Enter key to search: 1
Key found in tree

Enter your choice:


1. Insert
2. Search
3. Exit
Choice: 2
Enter key to search: 6
Key not found

Enter your choice:


1. Insert
2. Search
3. Exit
Choice: 3
Memory Cleared
PROGRAM TERMINATED

37

You might also like