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

Exe8 1 2

Xxvvgfddss

Uploaded by

srikakarla1
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)
20 views

Exe8 1 2

Xxvvgfddss

Uploaded by

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

Exercise 8: Binary Search Tree

i) Implementing a BST using Linked List.


ii) Traversing of BST.

I) AIM: Implementing a BST using Linked List.


Binary search tree follows all properties of binary tree and its left child contains values less than the
parent node and the right child contains values greater than the parent node. This hierarchical structure
allows for efficient Searching, Insertion, and Deletion operations on the data stored in the tree.

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

// Structure for a node in the BST


struct node {
int data;
struct node* left;
struct node* right;
};
// Function to create a new node
struct node* newNode(int data)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Function to insert a node into the BST
struct node* insert(struct node* root, int data) {
// If the tree is empty, return a new node
if (root == NULL) {
return newNode(data);
}
// Recursively insert the node based on its value
if (data < root->data) {
root->left = insert(root->left, data);
}
else if (data > root->data) {
root->right = insert(root->right, data);
}

// If data is already present, do nothing (optional)


// You can modify this to handle duplicates differently.
return root;
}

// Function to search for a key in the BST


struct node* search(struct node* root, int key) {
// If the tree is empty or the current node matches the key, return
it
if (root == NULL || root->data == key) {
return root;
}
// Recursively search for the key in the left or right subtree
if (key < root->data) {
return search(root->left, key);
}
else {
return search(root->right, key);
}
}
// Function to perform in-order traversal (prints nodes in ascending
order)
void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

int main() {
struct node* root = NULL;
struct node* found=NULL;
clrscr();
// Insert some nodes
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

// Print the in-order traversal


printf("Inorder traversal: ");
inorder(root);
printf("\n");

// Search for a key


found = search(root, 40);
if (found) {
printf("Key %d found\n", found->data);
}
else {
printf("Key not found\n");
}

return 0;
}
ii) AIM: Traversing of BST.

Traversing a tree means visiting and outputting the value of each node in a particular order. The
traversing methods are Inorder, Preorder, and Post order tree traversal methods.

 For Inorder, traverse from the left subtree to the root then to the right subtree.
 Inorder => Left, Root, Right.

 For Preorder, traverse from the root to the left subtree then to the right subtree.

 Preorder => Root, Left, Right.

 For Post order, traverse from the left subtree to the right subtree then to the root.
Post order => Left, Right, Root.

#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
// Define the node structure for the BST
struct node {
int data;
struct node *left;
struct node *right;
};
// Function to create a new node
struct node* newNode(int data) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Function to insert a node into the BST
struct node* insert(struct node* root, int data) {
// If the tree is empty, create a new node and make it the root
if (root == NULL) {
return newNode(data);
}

// Recursively insert the node in the correct subtree


if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
// Return the (unchanged) root node
return root;
}

// Function to perform in-order traversal of the BST


void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void preorder(struct node* root) {
if (root != NULL) {
printf("%d ", root->data); // Visit root first
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data); // Visit root last
}
}

int main() {
struct node* root = NULL;
clrscr();

// Insert some elements into the BST


root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
printf("BINARY SEARCH TREE TRAVERSAL\n\n");
// Print the in-order traversal (sorted order)
printf("In-order traversal: ");
inorder(root);
printf("\n");
printf("Pre-order traversal: ");
preorder(root);
printf("\n");
printf("Post-order traversal: ");
postorder(root);
printf("\n");
getch();
return 0;
}

You might also like