0% found this document useful (0 votes)
13 views20 pages

Program 8,9,10

The document describes a C++ program to implement a binary search tree (BST). The program defines a BST class with functions to insert nodes into the tree, remove nodes from the tree, and perform inorder, preorder and postorder traversals of the tree. The BST stores automobile data objects with attributes like type, company and year. The main function demonstrates inserting some sample automobile data and printing the results of different traversals.

Uploaded by

Ansh Balgotra
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)
13 views20 pages

Program 8,9,10

The document describes a C++ program to implement a binary search tree (BST). The program defines a BST class with functions to insert nodes into the tree, remove nodes from the tree, and perform inorder, preorder and postorder traversals of the tree. The BST stores automobile data objects with attributes like type, company and year. The main function demonstrates inserting some sample automobile data and printing the results of different traversals.

Uploaded by

Ansh Balgotra
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/ 20

PROGRAM-9

AIM: Create a binary tree and perform tree traversals


(inorder,preorder and postorder) using the concept of
recursion.
THEORY:
CODE:
#include<bits/stdc++.h>
using namespace std;
struct tree{ int data;
tree *left, *right;
tree(int x){
data=x;
left=right=NULL;}
};
void input(tree* node, vector<tree*>& q){
if(q.front()->left==NULL)
q.front()->left=node;
else{q.front()->right=node;
q.erase(q.begin());
}
q.push_back(node); }
void inorder(tree*& head){
if(head==NULL) return;
inorder(head->left);
cout<<head->data<<" - ";
inorder(head->right);
}
void preorder(tree*& head){
if(head==NULL) return;
cout<<head->data<<" - ";
preorder(head->left);
preorder(head->right);
}
void postorder(tree*& head){
if(head==NULL) return;
postorder(head->left);
postorder(head->right);
cout<<head->data<<" - ";
}
int main(){
cout<<"ANSH BALGOTRA"<<endl;
int n,k;
vector<tree*> queue;
cout<<"Enter number of nodes in binary tree : ";
cin>>n;
cout<<"Enter elements : ";
cin>>k;
tree* head=new tree(k);
queue.push_back(head);
for(int i=1;i<n;i++){
cin>>k;
tree* node=new tree(k);
input(node,queue);
}
cout<<"\nInorder traversal : ";
inorder(head);
cout<<"END\nPreorder traversal : ";
preorder(head);
cout<<"END\nPostorder traversal : ";
postorder(head);
cout<<"END";
return 0;
}
OUTPUT:
PROGRAM-10
AIM: Implement insertion, deletion and traversals (in order,
preorder,postorder) on binary search tree with the
information in the tree about the details of an automobile
(type,company,year of make).
THEORY:
CODE:
#include <iostream>
#include <string>
struct Automobile {
std::string type;
std::string company;
int year;
Automobile(const std::string& t, const std::string& c, int y)
: type(t), company(c), year(y) {}
};
struct TreeNode {
Automobile data;
TreeNode* left;
TreeNode* right;
TreeNode(const Automobile& d) : data(d), left(nullptr),
right(nullptr) {}
};
class BST {
public:
BST() : root(nullptr) {}
void insert(const Automobile& data) {
root = insertRecursive(root, data); }
void remove(const std::string& type) {
root = removeRecursive(root, type);
}
void inorderTraversal() const {
inorderTraversalRecursive(root);
}
void preorderTraversal() const {
preorderTraversalRecursive(root);
}
void postorderTraversal() const {
postorderTraversalRecursive(root);
}
private:
TreeNode* root;
TreeNode* insertRecursive(TreeNode* node, const
Automobile& data) {
if (node == nullptr) {
return new TreeNode(data);}
if (data.type < node->data.type) {
node->left = insertRecursive(node->left, data);
} else if (data.type > node->data.type) {
node->right = insertRecursive(node->right, data);
}
return node;}
TreeNode* removeRecursive(TreeNode* node, const
std::string& type) {
if (node == nullptr) {
return node;}
if (type < node->data.type) {
node->left = removeRecursive(node->left, type);
} else if (type > node->data.type) {
node->right = removeRecursive(node->right, type);
} else {
if (node->left == nullptr) {
TreeNode* temp = node->right;
delete node;
return temp;
} else if (node->right == nullptr) {
TreeNode* temp = node->left;
delete node;
return temp; }
TreeNode* temp = minValueNode(node->right);
node->data = temp->data;
node->right = removeRecursive(node->right, temp-
>data.type);
}
return node;}
void inorderTraversalRecursive(const TreeNode* node)
const {
if (node != nullptr) {
inorderTraversalRecursive(node->left);
display(node->data);
inorderTraversalRecursive(node->right); }
}
void preorderTraversalRecursive(const TreeNode* node)
const {
if (node != nullptr) {
display(node->data);
preorderTraversalRecursive(node->left);
preorderTraversalRecursive(node->right);}
}
void postorderTraversalRecursive(const TreeNode* node)
const {
if (node != nullptr) {
postorderTraversalRecursive(node->left);
postorderTraversalRecursive(node->right);
display(node->data); }
}
TreeNode* minValueNode(TreeNode* node) {
TreeNode* current = node;
while (current->left != nullptr) {
current = current->left;}
return current;}

void display(const Automobile& data) const {


std::cout << "Type: " << data.type << ", Company: " <<
data.company << ", Year: " << data.year << std::endl;
}};
int main() {
BST bst;
bst.insert(Automobile("Sedan", "Toyota", 2020));
bst.insert(Automobile("SUV", "Honda", 2019));
bst.insert(Automobile("Hatchback", "Ford", 2022));
bst.insert(Automobile("Truck", "Chevrolet", 2021));
std::cout << "In-order Traversal:" << std::endl;
bst.inorderTraversal();
std::cout << "\nPre-order Traversal:" << std::endl;
bst.preorderTraversal();
std::cout << "\nPost-order Traversal:" << std::endl;
bst.postorderTraversal();
bst.remove("SUV");
std::cout << "\nIn-order Traversal after removing SUV:" <<
std::endl;
bst.inorderTraversal();
return 0;}
OUTPUT:
PROGRAM-8
AIM: Create a linked list with nodes having information about
a student and perform
a. Insert a new node at specified position.
b. Delete a node with the roll number of student specified.
c. Reversal of that linked list.
(Using linked list).
THEORY:
CODE:
#include <iostream>
using namespace std;
class Student {
public:
int roll_number;
std::string name;
Student* next;
Student(int roll, const std::string& student_name) :
roll_number(roll), name(student_name), next(nullptr) {}
};
class StudentLinkedList {
private:
Student* head;
public:
StudentLinkedList() : head(nullptr) {}
void insert_at_position(int roll_number, const std::string&
name, int position) {
Student* new_student = new Student(roll_number,
name);
if (position == 1) {
new_student->next = head;
head = new_student;
Return; }
Student* current = head;
for (int i = 1; i < position - 1 && current != nullptr; ++i) {
current = current->next; }
if (current == nullptr) {
std::cout << "Position out of bounds.\n";
return; }
new_student->next = current->next;
current->next = new_student;}
void delete_node(int roll_number) {
Student* current = head;
Student* prev = nullptr;
if (current != nullptr && current->roll_number ==
roll_number) {
head = current->next;
delete current;
return; }
while (current != nullptr && current->roll_number !=
roll_number) {
prev = current;
current = current->next;}
if (current == nullptr) {
std::cout << "Student with Roll Number " <<
roll_number << " not found.\n";
return;}
prev->next = current->next;
delete current; }
void reverse_list() {
Student* prev = nullptr;
Student* current = head;
Student* next_node = nullptr;
while (current != nullptr) {
next_node = current->next;
current->next = prev;
prev = current;
current = next_node; }
head = prev;}

void display_list() {
Student* current = head;
while (current != nullptr) {
std::cout << "Roll Number: " << current->roll_number
<< ", Name: " << current->name << "\n";
current = current->next;}
}
~StudentLinkedList() {
Student* current = head;
Student* next_node;
while (current != nullptr) {
next_node = current->next;
delete current;
current = next_node; }
}
};
int main() {
cout<<"ANSH BALGOTRA"<<endl;
StudentLinkedList linked_list;
linked_list.insert_at_position(101, "John", 1);
linked_list.insert_at_position(102, "Alice", 2);
linked_list.insert_at_position(103, "Bob", 3);
std::cout << "Initial Linked List:\n";
linked_list.display_list();
std::cout << "\nInserting a new student at position 2:\n";
linked_list.insert_at_position(104, "Charlie", 2);
linked_list.display_list();
std::cout << "\nDeleting student with Roll Number 102:\n";
linked_list.delete_node(102);
linked_list.display_list();
std::cout << "\nReversing the linked list:\n";
linked_list.reverse_list();
linked_list.display_list();
return 0;}
OUTPUT:

You might also like