expt7 manual sheets
expt7 manual sheets
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
Theory:
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 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.
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.
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.
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:
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).
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:
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:
Structure definition:
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:
break;
case 3:
break;
case 4:
//BST preorder traversal
break;
case 5:
case 0:
break;
return 0;
}
Output: