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

Exercise 8

The document provides two implementations of a Binary Search Tree (BST) using linked lists in C. The first implementation includes functions for creating nodes, inserting values, and performing inorder traversal, while the second implementation adds options for creating a BST and traversing it in inorder, preorder, and postorder. Sample outputs demonstrate the functionality of both implementations.

Uploaded by

kodurusailalitya
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)
14 views6 pages

Exercise 8

The document provides two implementations of a Binary Search Tree (BST) using linked lists in C. The first implementation includes functions for creating nodes, inserting values, and performing inorder traversal, while the second implementation adds options for creating a BST and traversing it in inorder, preorder, and postorder. Sample outputs demonstrate the functionality of both implementations.

Uploaded by

kodurusailalitya
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/ 6

Exercise 8: Binary Search Tree

i.i) Implementing a BST using Linked List.

Programme:

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

// Define the structure for a node in the BST


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

// Function to create a new node


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

// Function to insert a new node into the BST


struct TreeNode* insertNode(struct TreeNode* root, int value) {
if (root == NULL) {
root = createNode(value);
} else if (value <= root->data) {
root->left = insertNode(root->left, value);
} else {
root->right = insertNode(root->right, value);
}
return root;
}

// Function to traverse the BST in inorder (left, root, right) manner


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

int main() {
struct TreeNode* root = NULL; // Initializing an empty tree

// Inserting some nodes into the BST


root = insertNode(root, 50);
root = insertNode(root, 30);
root = insertNode(root, 20);
root = insertNode(root, 40);
root = insertNode(root, 70);
root = insertNode(root, 60);
root = insertNode(root, 80);

// Printing the inorder traversal of the BST


printf("Inorder traversal of the BST: ");
inorderTraversal(root);
printf("\n");

return 0;
}

out put:
Inorder traversal of the BST: 20 30 40 50 60 70 80

i.ii) Traversing of BST.


Programme:

#include <stdio.h>
struct tnode
{
int data;
struct tnode *right;
struct tnode *left;
};
struct tnode *CreateBST(struct tnode *, int);
void Inorder(struct tnode *);
void Preorder(struct tnode *);
void Postorder(struct tnode *);
int main()
{
struct tnode *root = NULL;
int choice, item, n, i;
do
{
printf("\n\nBinary Search Tree Operations\n");
printf("\n1. Creation of BST");
printf("\n2. Traverse in Inorder");
printf("\n3. Traverse in Preorder");
printf("\n4. Traverse in Postorder");
printf("\n5. Exit\n");
printf("\nEnter Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
root = NULL;
printf("\n\nBST for How Many Nodes ? ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
printf("\nEnter data for node %d : ", i);
scanf("%d",&item);
root = CreateBST(root,item);
}
printf("\nBST with %d nodes is ready to Use!!\n", n);
break;
case 2:
printf("\nBST Traversal in INORDER \n");
Inorder(root);
break;
case 3:
printf("\nBST Traversal in PREORDER \n");
Preorder(root);
break;
case 4:
printf("\nBST Traversal in POSTORDER \n");
Postorder(root);
break;
case 5:
printf("\n\n Terminating \n\n");
break;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
} while(choice != 5);
return 0;
}
struct tnode *CreateBST(struct tnode *root, int item)
{
if(root == NULL)
{
root = (struct tnode *)malloc(sizeof(struct tnode));
root->left = root->right = NULL;
root->data = item;
return root;
}
else
{
if(item < root->data )
root->left = CreateBST(root->left,item);
else if(item > root->data )
root->right = CreateBST(root->right,item);
else
printf(" Duplicate Element !! Not Allowed !!!");
return(root);
}
}
void Inorder(struct tnode *root)
{
if( root != NULL)
{
Inorder(root->left);
printf(" %d ",root->data);
Inorder(root->right);
}
}
void Preorder(struct tnode *root)
{
if( root != NULL)
{
printf(" %d ",root->data);
Preorder(root->left);
Preorder(root->right);
}
}
void Postorder(struct tnode *root)
{
if( root != NULL)
{
Postorder(root->left);
Postorder(root->right);
printf(" %d ",root->data);
}
}

out put:
Binary Search Tree Operations

1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit

Enter Choice : 1

BST for How Many Nodes ? 4

Enter data for node 1 : 30

Enter data for node 2 : 79

Enter data for node 3 : 23


Enter data for node 4 : 40

BST with 4 nodes is ready to Use!!

You might also like