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

Binary Search TreeTEJAS

The document outlines the implementation of a Binary Search Tree (BST) using a linked list in C. It describes the properties of a BST, the algorithms for searching, inserting, and deleting nodes, and provides a sample program demonstrating these operations. The program includes functions for creating nodes, inserting elements, and searching for specific values, along with example output results.
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)
12 views4 pages

Binary Search TreeTEJAS

The document outlines the implementation of a Binary Search Tree (BST) using a linked list in C. It describes the properties of a BST, the algorithms for searching, inserting, and deleting nodes, and provides a sample program demonstrating these operations. The program includes functions for creating nodes, inserting elements, and searching for specific values, along with example output results.
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/ 4

NAME:TEJAS MALI PRN:221041014

Aim: To implement Binary Search Tree ADT using Linked List.


Theory:
A binary search tree is a tree data structure that allows the user to store elements in a sorted
manner. It is called a binary tree because each node can have a maximum of two children and is
called a search tree because we can search for a number in O(log(n)) time.
The properties of a regular binary tree are:
1. All nodes in the left subtree have a value less than the root node
2. All nodes in the right subtree have a value more than the root node
3. Both subtrees of each node are also binary search trees, i.e. they have the above two
properties
Operations to Perform on Binary Search Tree in C
Three basic operations can be performed on a binary search tree:
Search Operation
In Search, we have to find a specific element in the data structure. This searching operation
becomes simpler in binary search trees because here elements are stored in sorted order.
Algorithm for searching an element in a binary tree is as follows:
1. Compare the element to be searched with the root node of the tree.
2. If the value of the element to be searched is equal to the value of the root node, return the
root node.
3. If the value does not match, check whether the value is less than the root element or not if
it is then traverse the left subtree.
4. If larger than the root element, traverse the right subtree.
5. If the element is not found in the whole tree, return NULL.
Insert Operation
Inserting an element in a binary search tree is always done at the leaf node. To perform insertion
in a binary search tree, we start our search operation from the root node, if the element to be
inserted is less than the root value or the root node, then we search for an empty location in the
left subtree, else, we search for the empty location in the right subtree.
Algorithm for inserting an element in a binary tree is as follows:
NAME:TEJAS MALI PRN:221041014

if node == NULL
return createNode(data) if
(data < node->data)

node->left = insert(node->left, data)


else if (data > node->data)
node->right = insert(node->right, data)
return node
Deletion Operation
In the deletion operation, we have to delete a node from the binary search tree in a way that does
not violate its properties. Deletion can occur in three possible cases:
1. Node to be deleted is the leaf node
This is the simplest case of deleting a node in a binary search tree. Here, we will replace the leaf
node with NULL and free the allocated space.
2. Node to be deleted has a single child node
In this case, we will replace the target node with its child and then delete that replaced child
node. This means that the child node will now contain the value to be deleted. So, we will just
replace the child node with NULL and free up the allocated space.
3. The node to be deleted has two children
This case is complex as compared to the previous two. Here we follow certain steps to delete the
node:
• We will find the inorder successor of the target node, which has to be deleted
• Now, replace this node with the successor until the target node is at the leaf
• Replace the target node with NULL and free up the allocated space

Program:
#include <stdio.h>
#include <stdlib.h> struct
node {
int key;
NAME:TEJAS MALI PRN:221041014

struct node *left, *right;


};
struct node* newNode(int item)
{
struct node* temp
= (struct node*)malloc(sizeof(struct
node)); temp->key = item; temp->left = temp-
>right = NULL; return temp;
}
struct node* insert(struct node* node, int key)
{
if (node == NULL) return
newNode(key); if (key < node->key)
node->left = insert(node->left, key); else if
(key > node->key) node->right =
insert(node->right, key); return node;
}
struct node* search(struct node* root, int key)
{
if (root == NULL || root->key == key)
return root;
if (root->key < key) return search(root-
>right, key); return search(root->left, key);
} int
main()
{
struct node* root = NULL;
NAME:TEJAS MALI PRN:221041014

root = insert(root, 50);


insert(root, 30); insert(root, 20);
insert(root, 40); insert(root, 70);
insert(root, 60); insert(root, 80);
int key = 6; if (search(root, key) ==
NULL) printf("%d not
found\n", key);
else
printf("%d found\n", key);
key = 60;
if (search(root, key) == NULL)
printf("%d not found\n", key);
else
printf("%d found\n", key);
return 0;
}
Output
6 not found
60 found

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy