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

expt7 manual sheets

The document outlines an experiment focused on creating and manipulating a binary search tree (BST) using a doubly linked list structure in C. It details the objectives, theory behind structures and doubly linked lists, as well as the operations for creating, inserting, searching, and traversing the BST. Additionally, it provides a skeleton for the main function and algorithms for various operations within the BST.

Uploaded by

onlyfansfile
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)
9 views

expt7 manual sheets

The document outlines an experiment focused on creating and manipulating a binary search tree (BST) using a doubly linked list structure in C. It details the objectives, theory behind structures and doubly linked lists, as well as the operations for creating, inserting, searching, and traversing the BST. Additionally, it provides a skeleton for the main function and algorithms for various operations within the BST.

Uploaded by

onlyfansfile
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/ 13

Experiment 7

Program Statement:

1. Consider an admission scenario where student takes admission into an engineering college
depending on his CET rank. Once student takes an admission he will be given a roll number.
a. Construct a binary search tree by considering student roll number as key.
b. Search for the given roll number in the constructed tree.
c. Perform preorder, postorder and inorder traversal on the tree

Objective of the experiment:


1. To create a structure of a tree using doubly linked list concept that encapsulates related data
fields.
2. To create a binary search tree structure using DLL to store multiple instances of the defined
structure.
3. To design a program with modular functions, such as separate functions for insert,
search, traversals (Inorder, Preorder and Postorder) of a tree and manipulation of data
etc., to improve code organization and readability.
4. To create a well-structured and functional C program using structure to represent BST

Theory:

Introduction to Structures in C was given in TW2.So the


concept of doubly linked is explained with structure of C.
The syntax for declaring a structure in C is as follows:
struct Structure Name
{
// Member variables or fields
DataType1 member1;
DataType2 member2;
...
};

Here, Structure Name is the name given to the structure, and DataType1, DataType2, etc.,
represent the data types of the individual members or fields within the structure.

Doubly Linked list :

Doubly linked list is a complex type of linked list in which a node contains a pointer to the
previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists
of three parts: node data, pointer to the next node in sequence (next pointer), pointer to the previous
node (previous pointer). A sample node in a doubly linked list is shown in the figure.

In C, structure of a node in doubly linked list can be given as :

struct node
{
struct node *prev;
int data;
struct node *next;
}

The prev part of the first node and the next part of the last node will always contain null indicating
end in each direction. In a singly linked list, we could traverse only in one direction, because each
node contains address of the next node and it doesn't have any record of its previous nodes.
However, doubly linked list overcome this limitation of singly linked list. Due to the fact that, each
node of the list contains the address of its previous node, we can find all the details about the
previous node as well by using the previous address stored inside the previous part of each node.
A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown
in the following image.

Operations on doubly linked list


Node Creation

Definition of a node in the Binary Search Tree


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

Example of a Structure for Doubly Linked list

In this example, we have defined a structure named with three members: an integer for data, and a
self-referential structure for left and right node.

DLL is used to construct Binary tree.

What is Binary Search Tree?

Binary Search Tree is a node-based binary tree data structure which has the following properties:
 The left subtree of a node contains only nodes with keys lesser than the node’s key.
 The right subtree of a node contains only nodes with keys greater than the node’s key.
 The left and right subtree each must also be a binary search tree.
A binary search tree (BST) adds these two characteristics:

1. Each node has a maximum of up to two children.

2. For each node, the values of its left descendent nodes are less than that of the current node, which
in turn is less than the right descendent nodes (if any).

Basic operations on a BST


 Create: creates an empty tree.

 Insert: insert a node in the tree.

 Search: Searches for a node in the tree.

 Delete: deletes a node from the tree.

 Inorder: in-order traversal of the tree.

 Preorder: pre-order traversal of the tree.

 Postorder: post-order traversal of the tree.

Create
Initially an empty tree without any nodes is created. The variable/identifier which must point to the
root node is initialized with a NULL value
Given a BST, the task is to insert a new node in this BST.
Example:

How to Insert a value in a Binary Search Tree:


A new key is always inserted at the leaf by maintaining the property of the binary search tree. We
start searching for a key from the root until we hit a leaf node. Once a leaf node is found, the new
node is added as a child of the leaf node. The below steps are followed while we try to insert a
node into a binary search tree:
 Check the value to be inserted (say X) with the value of the current node (say val) we are in:
 If X is less than val move to the left subtree.
 Otherwise, move to the right subtree.
 Once the leaf node is reached, insert X to its right or left based on the relation between X and
the leaf node’s value.
Algorithm to search for a key(roll no ) in a given Binary Search Tree:
Let’s say we want to search for the number X, We start at the root. Then:
 We compare the value to be searched with the value of the root.
 If it’s equal we are done with the search if it’s smaller we know that we need to go
to the left subtree because in a binary search tree all the elements in the left
subtree are smaller and all the elements in the right subtree are larger.
 Repeat the above step till no more traversal is possible
 If at any iteration, key is found, return True. Else False.

Illustration of searching in a BST:


Binary Search Tree (BST) Traversals – Inorder, Preorder, Post Order

Output:
Inorder Traversal: 10 20 30 100 150 200 300
Preorder Traversal: 100 20 10 30 200 150 300
Postorder Traversal: 10 30 20 150 300 200 100
Output:
Inorder Traversal: 8 12 20 22 25 30 40
Preorder Traversal: 22 12 8 20 30 25 40
Postorder Traversal: 8 20 12 25 40 30 22

Inorder Traversal:
Below is the idea to solve the problem:
At first traverse left subtree then visit the root and then traverse the right subtree.
Follow the below steps to implement the idea:
 Traverse left subtree
 Visit the root and print the data.
 Traverse the right subtree

Preorder Traversal:
Below is the idea to solve the problem:
At first visit the root then traverse left subtree and then traverse the right subtree.
Follow the below steps to implement the idea:
 Visit the root and print the data.
 Traverse left subtree
 Traverse the right subtree

Postorder Traversal:
Below is the idea to solve the problem:
At first traverse left subtree then traverse the right subtree and then visit the root.
Follow the below steps to implement the idea:
 Traverse left subtree
 Traverse the right subtree
 Visit the root and print the data.
Conclusion:

Binary search trees (BSTs) offer several advantages over other types of trees and hash tables in
terms of insertion, deletion, search time complexity, and memory efficiency. Here are the key
advantages of using a BST:

1. Ordered Data Structure:


2. Efficient Search:
3. Memory Efficiency:
4. Dynamic Operations:
5. Simplicity:

Important instructions for writing program:

Structure definition:

// Define a structure to represent a student

Define a structure for a binary tree node


struct Node {
int data; // roll no based on the CET rank
struct Node* left; // self-referential structure pointing for the left
node
struct Node* right; // self-referential structure pointing for the right
node
};

Note: additional data members may be added to the structure to represent additional
student information – if needed.
Skeleton of main function:

int main() {
struct Node* root = NULL;
int choice, rollno;
struct Node* result;

do {
printf("\nBinary Search Tree Operations:\n");//display operations for BST
printf("1. Insert a node\n");
printf("2. Search for a node\n");
printf("3. In-order traversal\n");
printf("4. Pre-order traversal\n");
printf("5. Post-order traversal\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
//insert a node into BST

break;

case 2:

//search for the key(roll no)

break;

case 3:

//BST inorder traversal

break;

case 4:
//BST preorder traversal
break;

case 5:

// BST postorderr traversal


break;

case 0:

// Exit the program

break;

default: //Invalid choice


printf("Invalid choice. Please enter a valid option.\n");
}

} while (choice != 0);

return 0;
}

Algorithm of important module


Implementation details of important modules:

Application of the program implemented:

Output:

You might also like