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

Pre Post in Order

This C program implements a binary search tree data structure. It includes functions to insert nodes, search for a node, and traverse the tree using inorder, preorder, and postorder traversal. The main function provides a menu for the user to insert nodes, search for nodes, and view the different traversals. New nodes are created using a struct definition and memory is dynamically allocated for each new node.

Uploaded by

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

Pre Post in Order

This C program implements a binary search tree data structure. It includes functions to insert nodes, search for a node, and traverse the tree using inorder, preorder, and postorder traversal. The main function provides a menu for the user to insert nodes, search for nodes, and view the different traversals. New nodes are created using a struct definition and memory is dynamically allocated for each new node.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

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

struct node *root = NULL;


struct node *create_node(int);
void insert(int);
int search(int);
void inorder(struct node *);
void postorder();
void preorder();
int get_data();

int main()
{
int userChoice;
int Active = 'Y';
int data;
struct node* result = NULL;

while (Active == 'Y' || Active == 'y')


{
printf("\n\n select option: \n");
printf("\n1. Insert");
printf("\n2. Search");
printf("\n\n Traversals :");
printf("\n\n3. Inorder ");
printf("\n4. Post Order ");
printf("\n5. Pre Oder ");
printf("\n6. Exit");

printf("\n\nEnter Your Choice: ");


scanf("%d", &userChoice);
printf("\n");

switch(userChoice)
{
case 1:
data = get_data();
insert(data);
break;

case 2:
data = get_data();
if (search(data) == 1)
{
printf("\nData was found!\n");
}
else
{
printf("\nData does not found!\n");
}
break;

case 3:
inorder(root);
break;

case 4:
postorder(root);
break;

case 5:
preorder(root);
break;

case 6:
printf("\n\nERROR\n");
break;

default:
printf("\n\tInvalid Choice\n");
break;
}

printf("\n__________\nPress key to continue? ");


fflush(stdin);
scanf(" %c", &Active);
}

return 0;
}

struct node *create_node(int data)


{
struct node *new_node = (struct node *)malloc(sizeof(struct node));

if (new_node == NULL)
{
printf("\nMemory for new node can't be allocated");
return NULL;
}

new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;

return new_node;
}

void insert(int data)


{
struct node *new_node = create_node(data);

if (new_node != NULL)
{

if (root == NULL)
{
root = new_node;
printf("\n* node having data %d was inserted\n", data);
return;
}

struct node *temp = root;


struct node *prev = NULL;

while (temp != NULL)


{
prev = temp;
if (data > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}

if (data > prev->data)


{
prev->right = new_node;
}
else
{
prev->left = new_node;
}

printf("\n* node having data %d was inserted\n", data);


}
}

int search(int key)


{
struct node *temp = root;

while (temp != NULL)


{
if (key == temp->data)
{
return 1;
}
else if (key > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
return 0;
}
// Traverse
void inorder(struct node *root)
{
if (root == NULL)
{
return;
}
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}

void preorder(struct node *root)


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

void postorder(struct node *root)


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

int get_data()
{
int data;
printf("\nEnter Data: ");
scanf("%d", &data);
return data;
}

You might also like