Added all the codes from previous testing branch, made minor modifications to README file, and added git-commit-logs.

This commit is contained in:
K 2024-08-12 18:28:39 +05:30
parent 9e9ad1b42d
commit 388009b728
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
15 changed files with 2585 additions and 0 deletions

188
Practical-A1.py Normal file
View File

@ -0,0 +1,188 @@
'''
Problem Statement: Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client's telephone number. Make use of two collision handling techniques and compare them using number of comparisons required to find a set of telephone numbers.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
'''
# BEGINNING OF CODE
class HashEntry:
# Blueprint for entry in hash table
def __init__(self):
# Initializing empty values
self.name = None
self.num = -1
def insert(self, name, num):
# Insert value
self.name = name
self.num = num
def collision(self):
# Handle collision
return self.name is not None
def hashOne(size, val):
# Calculate first hashing function
return val % size
def hashTwo(val):
# Calculate second hashing function
return 7 - (val % 7)
def finalHash(val, size, i):
# Determine final position of value in hash table (after first and second hash is caluclated)
return (hashOne(size, val) + i * hashTwo(val)) % size
def stringToInt(strn):
# ASCII value of each character
sum = 0
for i in strn:
sum += ord(i)
return sum
class LinearProbing:
# Function to handle linear probing
def __init__(self, size):
self.size = size
self.HashTable = []
for _ in range(size):
self.HashTable.append(HashEntry())
def insert(self):
inputStr = input("Enter telephone NUMBER and NAME of client (separated by space):\t")
inputVal = inputStr.split()
if len(inputVal) != 2:
print("\n==========\nPlease enter both telephone number and name.\n==========")
return
num, name = inputVal
name2 = stringToInt(name)
pos = hashOne(self.size, name2)
i = 1
while self.HashTable[pos].collision():
pos = (pos + i) % self.size
i += 1
self.HashTable[pos].insert(name, num)
print("\n==========\nInserted\n==========")
def search(self):
name = input("Enter name of the client:\t")
name2 = stringToInt(name)
pos = hashOne(self.size, name2)
i = 1
while self.HashTable[pos].name != name:
pos = (pos + i) % self.size
i += 1
if i == self.size + 1:
break
else:
print("\n==========\nTelephone number of the client", name, "is", self.HashTable[pos].num, "\n==========")
return
print("\n==========\nClient not found.\n==========")
def display(self):
j = 0
print("Pos", "Name", "Value", sep="\t|\t")
print("-----", "-----", "-----", sep="\t+\t")
for i in self.HashTable:
print(j, i.name, i.num, sep="\t|\t")
j += 1
class DoubleHashing:
# Function to handle double hashing
def __init__(self, size):
self.size = size
self.HashTable = []
for _ in range(size):
self.HashTable.append(HashEntry())
def insert(self):
inputStr = input("Enter telephone NUMBER and NAME of client (separated by space):\t")
inputVal = inputStr.split()
if len(inputVal) != 2:
print("\n==========\nPlease enter both telephone number and name.\n==========")
return
num, name = inputVal
name2 = stringToInt(name)
i = 0
while True:
pos = finalHash(name2, self.size, i)
if self.HashTable[pos].collision():
i += 1
else:
break
self.HashTable[pos].insert(name, num)
print("\n==========\nInserted\n==========")
def search(self):
name = input("Enter name of the client:\t")
name2 = stringToInt(name)
i = 0
while True:
pos = finalHash(name2, self.size, i)
if self.HashTable[pos].name != name:
i += 1
else:
break
if i == self.size:
break
print("\n==========\nTelephone number of client", name, "is", self.HashTable[pos].num, "\n==========")
def display(self):
j = 0
print("Pos", "Name", "Value", sep="\t|\t")
print("-----", "-----", "-----", sep="\t+\t")
for i in self.HashTable:
print(j, i.name, i.num, sep="\t|\t")
j += 1
def main():
# Main function with options
tableSize = int(input("Enter size of hash table:\t"))
method = None
while True:
print("----- MAIN MENU -----")
print("1 -> Linear Probing")
print("2 -> Double Hashing")
print("3 -> Exit")
optn = int(input("Choose an option (1-3):\t"))
if optn == 1:
method = LinearProbing(tableSize)
elif optn == 2:
method = DoubleHashing(tableSize)
elif optn == 3:
print("\n\n## END OF CODE\n")
exit(1)
else:
print("Please choose a valid option (1-3).")
continue
while True:
print("\n----- CHOOSE OPERATION (HASH TABLE) -----")
print("1 -> Insert")
print("2 -> Search")
print("3 -> Display")
print("4 -> Return to previous menu")
optn = int(input("Choose an option (1-4):\t"))
if optn == 1:
method.insert()
elif optn == 2:
method.search()
elif optn == 3:
method.display()
elif optn == 4:
break
else:
print("Please choose a valid option (1-4).")
continue
# Calling main function
main()
# END OF CODE

168
Practical-A4.py Normal file
View File

@ -0,0 +1,168 @@
'''
Problem Statement: To create ADT that implement the "set" concept.
a. Add (newElement) -Place a value into the set
b. Remove (element) Remove the value
c. Contains (element) Return true if element is in collection
d. Size () Return number of values in collection Iterator () Return an iterator used to loop over collection
e. Intersection of two sets
f. Union of two sets
g. Difference between two sets
h. Subset
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
'''
# BEGINNING OF CODE
setOne=[]
setTwo=[]
def addVal(Set):
# Function to add value to set
val = int(input("Value to add:\t"))
if (val in Set): # Checking if value already exists in set
print(f"{val} already exists in the set.")
else: # Adding value if does not exist
Set.append(val)
print(f"Set is:\t{Set}")
def delVal(Set):
# Function to delete value from set
val = int(input("Value to remove:\t"))
if(val not in Set): # Checking if value is not there in set
print(f"{val} is not present in the set.")
else: # Deleting value if it exists in set
Set.remove(val)
print(f"Set is:\t{Set}")
def searchVal(Set):
# Function to search value in set
val = int(input("Value to search:\t"))
if(val in Set): # Check if value is present in set
print(f"{val} is present in the set.")
else: # Print if value not present in set
print(f"{val} is not present in the set.")
def size(Set):
# Function to print size (length) of set
print(f"Size of set is:\t{len(Set)}")
def iterator(setA):
a = iter(setA) # iter is a built-in function
for i in range(0,len(setA)-1):
print(next(a),"->",end=' ')
print(next(a))
def intersection(setA, setB):
# Function to perform intersection of two sets
intersectionSet = []
for i in setA:
if i in setB:
intersectionSet.append(i)
print(f"Intersection is:\t{intersectionSet}")
def union(setA, setB):
# Function to perform union of two sets
unionSet = []
for i in setA:
unionSet.append(i)
for j in setB:
if j not in setA:
unionSet.append(j)
print(f"Union is:\t{unionSet}")
def difference(setA, setB):
# Function to perform difference of two sets
differenceSet = []
for i in setA:
if i not in setB:
differenceSet.append(i)
print(f"Difference is:\t{differenceSet}")
def subsetCheck(setA, setB):
# Function to check if two sets are subsets, called in subset()
for i in setB:
if i not in setA:
return False
return True
def subset(setA, setB):
# Function to print if two sets are subsets
if subsetCheck(setA,setB):
print("Set two is a subset of set one.")
else:
print("Set two is not a subset of set one.")
def main():
# Function for main menu
while (True):
print("--- MAIN MENU ---")
print("1 -> Add value to set")
print("2 -> Remove value from set")
print("3 -> Search value in set")
print("4 -> Show size of set")
print("5 -> Iterate")
print("6 -> Intersection of two sets")
print("7 -> Union of two sets")
print("8 -> Difference of two sets")
print("9 -> Subset of two sets")
print("10 -> Exit")
optn = int(input("Choose an option (1-10):\t"))
if (optn == 1):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
total = int(input("Total values to add:\t"))
for i in range(total):
if (setSel == 1):
addVal(setOne)
elif (setSel == 2):
addVal(setTwo)
else:
print("\nPlease choose a valid option.\n")
elif (optn == 2):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
if (setSel == 1):
delVal(setOne)
elif (setSel == 2):
delVal(setTwo)
else:
print("\nPlease choose a valid option.\n")
elif (optn == 3):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
if (setSel == 1):
searchVal(setOne)
elif (setSel == 2):
searchVal(setTwo)
else:
print("\nPlease choose a valid option.\n")
elif (optn == 4):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
if (setSel == 1):
size(setOne)
elif (setSel == 2):
size(setTwo)
else:
print("\nPlease choose a valid option.\n")
elif (optn == 5):
setSel = int(input("Which set to operate on?\n1. Set one\n2. Set two\nSet 1/2:\t"))
a = None
if (setSel == 1):
iterator(setOne)
elif (setSel == 2):
iterator(setTwo)
else:
print("\nPlease choose a valid option.\n")
elif (optn == 6):
intersection(setOne, setTwo)
elif (optn == 7):
union(setOne, setTwo)
elif (optn == 8):
difference(setOne, setTwo)
elif (optn == 9):
subset(setOne, setTwo)
elif (optn == 10):
print("\n\n## END OF CODE\n\n")
exit(1)
else:
print("Please choose a valid option (1-10).")
main() # Calling the main function
# END OF CODE

257
Practical-B11.cpp Normal file
View File

@ -0,0 +1,257 @@
#include <iostream>
using namespace std;
class node {
public:
string word;
string meaning;
node* left;
node* right;
node(string x, string y) {
word = x;
meaning = y;
left = NULL;
right = NULL;
}
friend class Dictionary;
};
class Dictionary {
public:
node* root, * q; // q is parent here
Dictionary() {
root = NULL;
q = NULL;
}
void insert(node*, string, string);
void display_asc(node*);
void display_desc(node*);
void comparisons(node*, string);
void updateWord(node*, string);
void deleteWord(node*&, string);
node* min_node(node*);
};
void Dictionary::insert(node* p, string key, string keyMeaning) {
if (key < p->word) {
if (p->left != NULL)
insert(p->left, key, keyMeaning);
else
p->left = new node(key, keyMeaning);
}
else if (key > p->word) {
if (p->right != NULL)
insert(p->right, key, keyMeaning);
else
p->right = new node(key, keyMeaning);
}
}
void Dictionary::display_asc(node* p) { //inorder
if (p->left != NULL)
display_asc(p->left);
cout << "\n" << p->word << " \t" << p->meaning;
if (p->right != NULL)
display_asc(p->right);
}
void Dictionary::display_desc(node* p) {
if (p->right != NULL)
display_desc(p->right);
cout << "\n" << p->word << " \t" << p->meaning;
if (p->left != NULL)
display_desc(p->left);
}
void Dictionary::comparisons(node* p, string key) {
static int count = 0;
while (p != NULL) {
if (key < p->word) {
count++;
p = p->left;
}
else if (key > p->word) {
count++;
p = p->right;
}
else if (key == p->word) {
count++;
cout << "Number of comparisons to find the word: " << count;
return;
}
}
cout << "\nWord not found!";
}
node* Dictionary::min_node(node* p) {
while (p->left != NULL) {
q = p;
p = p->left;
}
return p;
}
void Dictionary::deleteWord(node*& p, string key) {
node* s;
while (p != NULL) { //searching for word
if (key < p->word) {
q = p;
p = p->left;
}
else if (key > p->word) {
q = p;
p = p->right;
}
else if (key == p->word) { //word found
if (p->left == NULL && p->right == NULL) { //no child
if (q->left == p) {
delete p;
q->left = NULL;
return;
}
if (q->right == p) {
delete p;
q->right = NULL;
return;
}
}
if (p->right != NULL && p->left == NULL) { //right child only
if (q->right == p) {
q->right = p->right;
delete p;
return;
}
else if (q->left == p) {
q->left = p->right;
delete p;
return;
}
}
else if (p->left != NULL && p->right == NULL) { //left child only
if (q->right == p) {
q->right = p->left;
delete p;
return;
}
else if (q->left == p) {
q->left = p->left;
delete p;
return;
}
}
else if (p->left != NULL && p->right != NULL) {
s = min_node(p->right);
p->word = s->word;
p->meaning = s->meaning;
deleteWord(p->right, s->word);
return;
}
}
}
cout << "\nWord NOT found!";
}
void Dictionary::updateWord(node* p, string key) {
while (p != NULL) {
if (key < p->word)
p = p->left;
else if (key > p->word)
p = p->right;
else if (key == p->word) {
cout << "\nEnter its new meaning: ";
cin >> p->meaning;
return;
}
}
cout << "\nWord not found!";
}
int main() {
int choice, n;
string newWord, searchWord, newMeaning;
Dictionary d1;
menu:
cout << "\n\nDICTIONARY: ";
cout << "\n\n1. Insert new words";
cout << "\n2. Display the dictionary in Ascending order";
cout << "\n3. Display the dictionary in Descending order";
cout << "\n4. Search and update a word";
cout << "\n5. Delete a word";
cout << "\n6. Compare";
cout << "\n7. EXIT";
cout << "\n\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "\nEnter the number of words to insert: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "\nEnter the word to be inserted: ";
cin >> newWord;
cout << "\nEnter its meaning: ";
cin >> newMeaning;
if (d1.root == NULL)
d1.root = new node(newWord, newMeaning);
else
d1.insert(d1.root, newWord, newMeaning);
}
break;
case 2:
d1.display_asc(d1.root);
break;
case 3:
d1.display_desc(d1.root);
break;
case 4:
cout << "\nEnter the word to search: ";
cin >> searchWord;
d1.updateWord(d1.root, searchWord);
break;
case 5:
cout << "\nEnter the word to delete: ";
cin >> searchWord;
d1.deleteWord(d1.root, searchWord);
break;
case 6:
cout << "\nEnter the word to find comparisons: ";
cin >> searchWord;
d1.comparisons(d1.root, searchWord);
break;
case 7:
cout << "END OF THE CODE .";
break;
default:
cout << "\nInvalid input!!!...Enter a Valid Input.";
}
if (choice != 8)
goto menu;
return 0;
}

119
Practical-B5.cpp Normal file
View File

@ -0,0 +1,119 @@
// WARNING: THIS CODE HAS NOT BEEN TESTED.
// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE.
// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS,
// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY.
/*
Problem Statement: A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
*/
// BEGINNING OF CODE
#include <iostream>
#include <string.h>
using namespace std;
struct node // Node Declaration
{
string label;
//char label[10];
int ch_count;
struct node *child[10];
} * root;
class GT // Class Declaration
{
public:
void create_tree();
void display(node *r1);
GT()
{
root = NULL;
}
};
void GT::create_tree()
{
int tbooks, tchapters, i, j, k;
root = new node;
cout << "Enter name of book : ";
cin.get();
getline(cin, root->label);
cout << "Enter number of chapters in book : ";
cin >> tchapters;
root->ch_count = tchapters;
for (i = 0; i < tchapters; i++)
{
root->child[i] = new node;
cout << "Enter the name of Chapter " << i + 1 << " : ";
cin.get();
getline(cin, root->child[i]->label);
cout << "Enter number of sections in Chapter : " << root->child[i]->label << " : ";
cin >> root->child[i]->ch_count;
for (j = 0; j < root->child[i]->ch_count; j++)
{
root->child[i]->child[j] = new node;
cout << "Enter Name of Section " << j + 1 << " : ";
cin.get();
getline(cin, root->child[i]->child[j]->label);
}
}
}
void GT::display(node *r1)
{
int i, j, k, tchapters;
if (r1 != NULL)
{
cout << "\n-----Book Hierarchy---";
cout << "\n Book title : " << r1->label;
tchapters = r1->ch_count;
for (i = 0; i < tchapters; i++)
{
cout << "\nChapter " << i + 1;
cout << " : " << r1->child[i]->label;
cout << "\nSections : ";
for (j = 0; j < r1->child[i]->ch_count; j++)
{
cout << "\n"<< r1->child[i]->child[j]->label;
}
}
}
cout << endl;
}
int main()
{
int choice;
GT gt;
while (1)
{
cout << "-----------------" << endl;
cout << "Book Tree Creation" << endl;
cout << "-----------------" << endl;
cout << "1.Create" << endl;
cout << "2.Display" << endl;
cout << "3.Quit" << endl;
cout << "Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
gt.create_tree();
case 2:
gt.display(root);
break;
case 3:
cout << "Thanks for using this program!!!";
exit(1);
default:
cout << "Wrong choice!!!" << endl;
}
}
return 0;
}
// END OF CODE
// EXPERIMENTAL CODE

146
Practical-B7.cpp Normal file
View File

@ -0,0 +1,146 @@
// WARNING: THIS CODE HAS NOT BEEN TESTED.
// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE.
// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS,
// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY.
/*
Problem Statement: Construct an expression tree from the given prefix expression eg. +--a*bc/def and
traverse it using postordertraversal(non recursive) and then delete the entire tree.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
*/
// BEGINNING OF CODE
#include <iostream>
#include <string.h>
using namespace std;
struct node
{
char data;
node *left;
node *right;
};
class tree
{
char prefix[20];
public:
node *top;
void expression(char[]);
void display(node *);
void non_rec_postorder(node *);
void del(node *);
};
class stack1
{
node *data[30];
int top;
public:
stack1()
{
top = -1;
}
int empty()
{
if (top == -1)
return 1;
return 0;
}
void push(node *p)
{
data[++top] = p;
}
node *pop()
{
return (data[top--]);
}
};
void tree::expression(char prefix[])
{
char c;
stack1 s;
node *t1, *t2;
int len, i;
len = strlen(prefix);
for (i = len - 1; i >= 0; i--)
{
top = new node;
top->left = NULL;
top->right = NULL;
if (isalpha(prefix[i]))
{
top->data = prefix[i];
s.push(top);
}
else if (prefix[i] == '+' || prefix[i] == '*' || prefix[i] == '-' || prefix[i] == '/')
{
t2 = s.pop();
t1 = s.pop();
top->data = prefix[i];
top->left = t2;
top->right = t1;
s.push(top);
}
}
top = s.pop();
}
void tree::display(node *root)
{
if (root != NULL)
{
cout << root->data;
display(root->left);
display(root->right);
}
}
void tree::non_rec_postorder(node *top)
{
stack1 s1, s2; /*stack s1 is being used for flag . A NULL data implies that the right subtree has not been visited */
node *T = top;
cout << "\n";
s1.push(T);
while (!s1.empty())
{
T = s1.pop();
s2.push(T);
if (T->left != NULL)
s1.push(T->left);
if (T->right != NULL)
s1.push(T->right);
}
while (!s2.empty())
{
top = s2.pop();
cout << top->data;
}
}
void tree::del(node *node)
{
if (node == NULL)
return;
/* first delete both subtrees */
del(node->left);
del(node->right);
/* then delete the node */
cout <<endl<<"Deleting node : " << node->data<<endl;
free(node);
}
int main()
{
char expr[20];
tree t;
cout <<"Enter prefix Expression : ";
cin >> expr;
cout << expr;
t.expression(expr);
//t.display(t.top);
//cout<<endl;
t.non_rec_postorder(t.top);
t.del(t.top);
// t.display(t.top);
}
// END OF CODE
// EXPERIMENTAL CODE

132
Practical-C13 (List).cpp Normal file
View File

@ -0,0 +1,132 @@
// WARNING: THIS CODE HAS NOT BEEN TESTED.
// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE.
// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS,
// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY.
/*
Problem Statement: Represent a given graph using ADJACENCY LIST to perform DFS and using adjacency list to perform BFS. Use the map of the area around the college as the graph. Identify the prominent land marks as nodes and perform DFS and BFS on that.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
*/
// BEGINNING OF CODE
#include <iostream>
using namespace std;
#define MAX 10
#define TRUE 1
#define FALSE 0
// declaring an adjacency list for storing the graph
class lgra
{
private:
struct node1
{
int vertex;
struct node1 *next;
};
node1 *head[MAX];
int visited[MAX];
public:
//static int nodecount;
lgra();
void create();
void dfs(int);
};
//constructor
lgra::lgra()
{
int v1;
for (v1 = 0; v1 < MAX; v1++)
visited[v1] = FALSE;
for (v1 = 0; v1 < MAX; v1++)
head[v1] = NULL;
}
void lgra::create()
{
int v1, v2;
char ans;
node1 *N, *first;
cout << "Enter the vertices no. beginning with 0 : ";
do
{
cout << "\nEnter the Edge of a graph : \n";
cin >> v1 >> v2;
if (v1 >= MAX || v2 >= MAX)
cout << "Invalid Vertex Value!!\n";
else
{
//creating link from v1 to v2
N = new node1;
if (N == NULL)
cout << "Insufficient Memory!!\n";
N->vertex = v2;
N->next = NULL;
first = head[v1];
if (first == NULL)
head[v1] = N;
else
{
while (first->next != NULL)
first = first->next;
first->next = N;
}
//creating link from v2 to v1
N = new node1;
if (N == NULL)
cout << "Insufficient Memory!!\n";
N->vertex = v1;
N->next = NULL;
first = head[v2];
if (first == NULL)
head[v2] = N;
else
{
while (first->next != NULL)
first = first->next;
first->next = N;
}
}
cout << "\n Want to add more edges?(y/n) : ";
cin >> ans;
} while (ans == 'y');
}
//dfs function
void lgra::dfs(int v1)
{
node1 *first;
cout << endl
<< v1;
visited[v1] = TRUE;
first = head[v1];
while (first != NULL)
if (visited[first->vertex] == FALSE)
dfs(first->vertex);
else
first = first->next;
}
int main()
{
int v1;
lgra g;
g.create();
cout << endl << "Enter the vertex from where you want to traverse : ";
cin >> v1;
if (v1 >= MAX)
cout << "Invalid Vertex!!\n";
else
{
cout << "The Dfs of the graph : ";
g.dfs(v1);
}
return 0;
}
// END OF CODE
// EXPERIMENTAL CODE

View File

@ -0,0 +1,93 @@
// WARNING: THIS CODE HAS NOT BEEN TESTED.
// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE.
// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS,
// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY.
/*
Problem Statement: Represent a given graph using ADJACENCY MATRIX to perform DFS and using adjacency list to perform BFS. Use the map of the area around the college as the graph. Identify the prominent land marks as nodes and perform DFS and BFS on that.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
*/
// BEGINNING OF CODE
#include <iostream>
#include <stdlib.h>
using namespace std;
int cost[10][10], i, j, k, n, qu[10], front, rear, v, visit[10], visited[10];
int stk[10], top, visit1[10], visited1[10];
int main()
{
int m;
cout << "Enter number of vertices : ";
cin >> n;
cout << "Enter number of edges : ";
cin >> m;
cout << "\nEDGES :\n";
for (k = 1; k <= m; k++)
{
cin >> i >> j;
cost[i][j] = 1;
cost[j][i] = 1;
}
//display function
cout << "The adjacency matrix of the graph is : " << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << " " << cost[i][j];
}
cout << endl;
}
cout << "Enter initial vertex : ";
cin >> v;
cout << "The BFS of the Graph is\n";
cout << v<<endl;
visited[v] = 1;
k = 1;
while (k < n)
{
for (j = 1; j <= n; j++)
if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1)
{
visit[j] = 1;
qu[rear++] = j;
}
v = qu[front++];
cout << v << " ";
k++;
visit[v] = 0;
visited[v] = 1;
}
cout <<endl<<"Enter initial vertex : ";
cin >> v;
cout << "The DFS of the Graph is\n";
cout << v<<endl;
visited[v] = 1;
k = 1;
while (k < n)
{
for (j = n; j >= 1; j--)
if (cost[v][j] != 0 && visited1[j] != 1 && visit1[j] != 1)
{
visit1[j] = 1;
stk[top] = j;
top++;
}
v = stk[--top];
cout << v << " ";
k++;
visit1[v] = 0;
visited1[v] = 1;
}
return 0;
}
// END OF CODE
// EXPERIMENTAL CODE

134
Practical-C15.cpp Normal file
View File

@ -0,0 +1,134 @@
// WARNING: THIS CODE HAS NOT BEEN TESTED.
// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE.
// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS,
// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY.
/*
Problem Statement: You have a business with several offices; you want to lease phone lines to connect them up with each other; and the phone company charges different amounts of money to connect different pairs of cities. You want a set of lines that connects all your offices with a minimum total cost. Solve the problem by suggesting appropriate data structures.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
*/
// BEGINNING OF CODE
#include<iostream>
using namespace std;
class Office
{
int n;
int a[10][10];
string office[10];
public:
void input();
void display();
void Prims();
};
void Office::input()
{
cout<<"\nEnter no. of offices: ";
cin>>n;
cout<<"\nEnter the names of offices: ";
for(int i=0 ; i<n ; i++)
cin >> office[i];
cout<<"\nEnter the cost to connect the offices: ";
for(int i=0 ; i<n ; i++)
for(int j=i ; j<n ; j++)
{
if(i==j)
{
a[i][j] = 0;
continue;
}
cout<<"\nEnter the cost to connect " << office[i] <<" and " << office[j]<< " : ";
cin >> a[i][j];
a[j][i] = a[i][j];
}
}
void Office::display()
{
for(int i=0 ; i<n ; i++)
{
cout<<"\n";
for(int j=0 ; j<n ; j++)
{
cout<<a[i][j] << "\t";
}
}
}
void Office::Prims()
{
int visit[n], minCost=0, count=1, minIndex, cost=0;
for(int i=0 ; i<n ; i++)
visit[i] = 0;
cout<<"\n\nShortest path: ";
visit[0]=1;
cout<<office[0] << " -> ";
while(1)
{
minCost = 10000;
for(int i=0 ; i<n ; i++)
{
for(int j=0 ; j<n ; j++)
{
if(visit[i]==1 && a[i][j]!=0 && a[i][j]< minCost && visit[j]==0)
{
minCost = a[i][j];
minIndex = j;
}
}
}
visit[minIndex]=1;
cout<<office[minIndex] << " -> ";
cost = cost + minCost;
count++;
if(count==n)
break;
}
cout<<"\nMinimum cost: "<<cost;
}
int main()
{
Office o1;
int choice;
MENU:
cout<<"\n\nMINIMUM SPANNING TREE";
cout<<"\n1. Input data";
cout<<"\n2. Display data";
cout<<"\n3. Calculate minimum cost";
cout<<"\n4. Exit";
cout<<"\nEnter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
o1.input();
break;
case 2:
o1.display();
break;
case 3:
o1.Prims();
break;
case 4:
return 0;
default:
cout<<"\nInvalid choice.Try again!";
}
if(choice != 5)
goto MENU;
return 0;
}
// END OF CODE
// EXPERIMENTAL CODE

View File

@ -0,0 +1,153 @@
// THIS CODE HAS NOT BEEN TESTED.
#include <iomanip>
#include <iostream>
#include <cstdint>
using namespace std;
#define MAX_NODES 10
class node {
public:
int data;
node* left;
node* right;
node(int data = 0) {
this->data = data;
left = NULL;
right = NULL;
}
};
class OBST {
public:
int keys[MAX_NODES] = {0}; // for storing keys
int p[MAX_NODES] = {0}; // for storing frequencies
int q[MAX_NODES] = {0}; // for storing frequencies
int n; // count of nodes
int c[MAX_NODES][MAX_NODES] = {0}; // cost matrix
int w[MAX_NODES][MAX_NODES] = {0}; // cost matrix
int r[MAX_NODES][MAX_NODES] = {0}; // cost matrix
node* root;
// initialize the values
OBST(int count, int keys_arr[], int p_arr[], int q_arr[]) {
root = NULL;
n = count;
for (int i = 0; i <= n; i++) {
this->keys[i] = keys_arr[i];
this->p[i] = p_arr[i];
this->q[i] = q_arr[i];
}
}
// display matrix
void display_matrix(int mat[MAX_NODES][MAX_NODES]) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i <= j) {
cout << setw(2) << mat[i][j] << " ";
} else {
cout << " ";
}
}
cout << endl;
}
}
int get_w(int i, int j) {
if (i >= j) {
return q[i];
}
return w[i][j - 1] + p[j] + q[j];
}
// generate cost matrix
void generate_cost_table() {
// initialize diagonal
for (int i = 0; i <= n; i++) {
c[i][i] = 0;
r[i][i] = 0;
w[i][i] = q[i];
}
// for j-i = 0 to n
for (int diff = 1; diff <= n; diff++) {
int i, j;
i = 0;
j = i + diff;
// for each pair with j-i = diff
while (j <= n) {
int min_cost = INT32_MAX, min_root;
// getting minimum cost and root for c[i,j]
for (int k = i + 1; k <= j; k++) {
int cost = c[i][k - 1] + c[k][j];
if (cost < min_cost) {
min_cost = cost;
min_root = k;
}
}
// updating the c matrix
w[i][j] = get_w(i, j);
c[i][j] = min_cost + w[i][j];
r[i][j] = min_root;
// cout<<i<<j<<endl;
// cout << "w=" << w[i][j] << endl;
// cout << "c=" << c[i][j] << endl;
// cout << "r=" << r[i][j] << endl;
i++;
j++;
}
}
cout << "Matrix C" << endl;
display_matrix(c);
cout << "Matrix W" << endl;
display_matrix(w);
cout << "Matrix R" << endl;
display_matrix(r);
}
// get node for [i,j]
node* get_node(int i, int j) {
if (i == j) {
return NULL;
}
int rij = r[i][j];
node* temp = new node(keys[rij]);
// left node will be [i,r-1]
temp->left = get_node(i, rij - 1);
// right node will be [r,j]
temp->right = get_node(rij, j);
return temp;
}
void pre_order(node* temp) {
if (temp != NULL) {
cout << temp->data << " ";
pre_order(temp->left);
pre_order(temp->right);
}
}
void generate_obst() {
root = get_node(0, n);
cout << "PreOrder Traversal Is" << endl;
pre_order(root);
cout << endl;
}
};
int main() {
int k[] = {0, 10, 20, 30, 40};
int p[] = {0, 3, 3, 1, 1};
int q[] = {2, 3, 1, 1, 1};
OBST o(4, k, p, q);
o.generate_cost_table();
o.generate_obst();
return 0;
}

View File

@ -0,0 +1,144 @@
// THIS CODE HAS NOT BEEN TESTED.
#include <iomanip>
#include <iostream>
#include <cstdint>
using namespace std;
#define MAX_NODES 10
struct cost_pair {
int cost;
int root = 0;
};
class node {
public:
int data;
node* left;
node* right;
node(int data = 0) {
this->data = data;
left = NULL;
right = NULL;
}
};
class OBST {
public:
int keys[MAX_NODES]; // for storing keys
int freq[MAX_NODES]; // for storing frequencies
int n; // count of nodes
cost_pair c[MAX_NODES][MAX_NODES]; // cost matrix
node* root;
// initialize the values
OBST(int count, int keys_arr[MAX_NODES], int freq_arr[MAX_NODES]) {
root = NULL;
n = count;
for (int i = 0; i <= n; i++) {
this->keys[i] = keys_arr[i];
this->freq[i] = freq_arr[i];
}
}
// function to return weight of (i,j)
int get_w(int i, int j) {
int w = 0;
for (int ind = i + 1; ind <= j; ind++) {
w += freq[ind];
}
return w;
}
// display c matrix
void display_c() {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i <= j) {
cout << setw(2) << c[i][j].cost << "-" << c[i][j].root
<< " ";
} else {
cout << " - ";
}
}
cout << endl;
}
}
// generate cost matrix
void generate_cost_table() {
// initialize diagonal to 0
for (int i = 0; i <= n; i++) {
c[i][i].cost = 0;
c[i][i].root = 0;
}
// for j-i = 0 to n
for (int diff = 1; diff <= n; diff++) {
int i, j;
i = 0;
j = i + diff;
// for each pair with j-i = diff
while (j <= n) {
int min_cost = INT32_MAX, min_root;
// getting minimum cost and root for c[i,j]
for (int k = i + 1; k <= j; k++) {
int cost = c[i][k - 1].cost + c[k][j].cost;
if (cost < min_cost) {
min_cost = cost;
min_root = k;
}
}
// updating the c matrix
c[i][j].cost = min_cost + get_w(i, j);
c[i][j].root = min_root;
// cout << "c" << i << j << endl;
i++;
j++;
}
}
display_c();
}
//get node for [i,j]
node* get_node(int i, int j) {
if (i == j) {
return NULL;
}
int rij = c[i][j].root;
node* temp = new node(keys[rij]);
// left node will be [i,r-1]
temp->left = get_node(i, rij - 1);
// right node will be [r,j]
temp->right = get_node(rij, j);
return temp;
}
void pre_order(node* temp) {
if (temp != NULL) {
cout << temp->data << " ";
pre_order(temp->left);
pre_order(temp->right);
}
}
void generate_obst() {
root = get_node(0, n);
pre_order(root);
cout << endl;
}
};
int main() {
int k[] = {0, 10, 20, 30, 40};
int f[] = {0, 4, 2, 6, 3};
OBST o(4, k, f);
o.generate_cost_table();
o.generate_obst();
return 0;
}

View File

@ -0,0 +1,280 @@
/*
THIS CODE IS NOT NEEDED TBH.
Problem Statement: A Dictionary stores keywords & its meanings. Provide facility for adding new keywords, deleting keywords, updating values of any entry. Provide facility to display whole data sorted in ascending/ Descending order. Also find how many maximum comparisons may require for finding any keyword. Use Height balance tree and find the complexity for finding a keyword.
Code from DataStructuresAndAlgorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
*/
// BEGINNING OF CODE
#include <iostream>
using namespace std;
class node {
public:
string key;
string value;
node* left;
node* right;
node(string key = "", string value = "") {
this->key = key;
this->value = value;
this->left = NULL;
this->right = NULL;
}
};
class AVL {
node* root = NULL;
int height(node* n) {
if (n == NULL) {
return 0;
}
return 1 + max(height(n->left), height(n->right));
}
int balanceFactor(node* n) {
if (n == NULL) {
return 0;
}
return (height(n->left) - height(n->right));
}
node* rightRotate(node* y) {
// get changing nodes
node* x = y->left;
node* t2 = x->right;
// change the nodes
x->right = y;
y->left = t2;
// return
return x;
}
node* leftRotate(node* x) {
// get changing nodes
node* y = x->right;
node* t2 = y->left;
// change the nodes
y->left = x;
x->right = t2;
// return
return y;
}
// recursive insertion function which will be called by insert function
node* insertion(node* temp, string key, string value) {
if (temp == NULL) {
node* nn = new node(key, value);
return nn;
}
if (key < temp->key) {
temp->left = insertion(temp->left, key, value);
}
if (key > temp->key) {
temp->right = insertion(temp->right, key, value);
}
int bf = balanceFactor(temp);
// LL Rotation
if (bf > 1 && balanceFactor(root->left) >= 0) {
return rightRotate(temp);
}
// RR Rotation
if (bf < -1 && balanceFactor(root->right) <= 0) {
return leftRotate(temp);
}
// LR Rotation
if (bf > 1 && balanceFactor(root->left) < 0) {
temp->left = leftRotate(temp->left);
return rightRotate(temp);
}
// RL Rotation
if (bf < -1 && balanceFactor(root->right) > 0) {
temp->right = rightRotate(temp->right);
return leftRotate(temp);
}
return temp;
}
void ascending(node* n) {
if (n != NULL) {
ascending(n->left);
cout << n->key << ":" << n->value << endl;
ascending(n->right);
}
}
void descending(node* n) {
if (n != NULL) {
descending(n->right);
cout << n->key << ":" << n->value << endl;
descending(n->left);
}
}
bool isPresent(string key) {
node* temp = root;
while (temp != NULL) {
if (temp->key == key) {
return true;
} else if (temp->key < key) {
temp = temp->right;
} else {
temp = temp->left;
}
}
return false;
}
node* minNode(node* root) {
node* n = root;
while (n->left != NULL) {
n = n->left;
}
return n;
}
node* remove(node*& temp_root, string key) {
// step1 bst deletion
if (temp_root == NULL) {
return NULL;
}
if (key < temp_root->key) {
temp_root->left = remove(temp_root->left, key);
} else if (key > temp_root->key) {
temp_root->right = remove(temp_root->right, key);
} else {
node* temp1 = temp_root;
// one and no child
if (temp_root->left == NULL || temp_root->right == NULL) {
if (temp_root->left == NULL) {
temp_root = temp_root->right;
} else {
temp_root = temp_root->left;
}
delete temp1;
} else {
// two child
node* temp2 = minNode(temp_root->right);
temp_root->key = temp2->key;
temp_root->value = temp2->value;
temp_root->right = remove(temp_root->right, temp2->key);
}
}
// check if root is NULL
if (temp_root == NULL) return temp_root;
// step2 avl balancing
int bf = balanceFactor(temp_root);
// LL Rotation
// if (bf > 1 && key < temp_root->left->key) {
if (bf > 1 && balanceFactor(root->left) >= 0) {
return rightRotate(temp_root);
}
// RR Rotation
// if (bf < -1 && key > temp_root->right->key) {
if (bf < -1 && balanceFactor(root->right) <= 0) {
return leftRotate(temp_root);
}
// LR Rotation
// if (bf > 1 && key > temp_root->left->key) {
if (bf > 1 && balanceFactor(root->left) < 0) {
temp_root->left = leftRotate(temp_root->left);
return rightRotate(temp_root);
}
// RL Rotation
// if (bf < -1 && key < temp_root->right->key) {
if (bf < -1 && balanceFactor(root->right) > 0) {
temp_root->right = rightRotate(temp_root->right);
return leftRotate(temp_root);
}
return temp_root;
}
public:
string search(string key) {
node* temp = root;
while (temp != NULL) {
if (temp->key == key) {
return temp->value;
} else if (temp->key < key) {
temp = temp->right;
} else {
temp = temp->left;
}
}
return "\0";
}
bool insert(string key, string value) {
if (isPresent(key)) {
return false;
} else {
root = insertion(root, key, value);
return true;
}
}
bool update(string key, string value) {
node* temp = root;
while (temp != NULL) {
if (temp->key == key) {
temp->value = value;
return true;
} else if (temp->key < key) {
temp = temp->right;
} else {
temp = temp->left;
}
}
return false;
}
bool remove(string key) {
if (!isPresent(key)) {
return false;
} else {
root = remove(root, key);
return true;
}
}
void ascending() {
if (root == NULL) {
cout << "Tree is Empty" << endl;
return;
}
cout << "Ascending Traversal is" << endl;
ascending(root);
}
void descending() {
if (root == NULL) {
cout << "Tree is Empty" << endl;
return;
}
cout << "Descending Traversal is" << endl;
descending(root);
}
};
int main() {
// implement AVL tree
AVL a;
cout << a.insert("1", "a") << endl;
cout << a.insert("2", "b") << endl;
cout << a.insert("3", "c") << endl;
cout << a.insert("4", "d") << endl;
a.descending();
cout << a.remove("1") << endl;
a.ascending();
return 0;
}

291
Practical-D19.cpp Normal file
View File

@ -0,0 +1,291 @@
// WARNING: THIS CODE HAS NOT BEEN TESTED.
// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE.
// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS,
// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY.
/*
Problem Statement: A Dictionary stores keywords & its meanings. Provide facility for adding new keywords, deleting keywords, updating values of any entry. Provide facility to display whole data sorted in ascending/ Descending order. Also find how many maximum comparisons may require for finding any keyword. Use Height balance tree and find the complexity for finding a keyword.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
*/
// BEGINNING OF CODE
#include<iostream>
#include<cstring>
#include<cstdlib>
#define MAX 50
#define SIZE 20
using namespace std;
struct AVLnode
{
public:
char cWord[SIZE],cMeaning[MAX];
AVLnode *left,*right;
int iB_fac,iHt;
};
class AVLtree
{
public:
AVLnode *root;
AVLtree()
{
root=NULL;
}
int height(AVLnode*);
int bf(AVLnode*);
AVLnode* insert(AVLnode*,char[SIZE],char[MAX]);
AVLnode* rotate_left(AVLnode*);
AVLnode* rotate_right(AVLnode*);
AVLnode* LL(AVLnode*);
AVLnode* RR(AVLnode*);
AVLnode* LR(AVLnode*);
AVLnode* RL(AVLnode*);
AVLnode* delet(AVLnode*,char x[SIZE]);
void inorder(AVLnode*);
};
AVLnode *AVLtree::delet(AVLnode *curr,char x[SIZE])
{
AVLnode *temp;
if(curr==NULL)
return(0);
else
if(strcmp(x,curr->cWord)>0)
{
curr->right=delet(curr->right,x);
if(bf(curr)==2)
if(bf(curr->left)>=0)
curr=LL(curr);
else
curr=LR(curr);
}
else
if(strcmp(x,curr->cWord)<0)
{
curr->left=delet(curr->left,x);
if(bf(curr)==-2)
if(bf(curr->right)<=0)
curr=RR(curr);
else
curr=RL(curr);
}
else
{
if(curr->right!=NULL)
{
temp=curr->right;
while(temp->left!=NULL)
temp=temp->left;
strcpy(curr->cWord,temp->cWord);
curr->right=delet(curr->right,temp->cWord);
if(bf(curr)==2)
if(bf(curr->left)>=0)
curr=LL(curr);
else
curr=LR(curr);
}
else
return(curr->left);
}
curr->iHt=height(curr);
return(curr);
}
AVLnode* AVLtree :: insert(AVLnode*root,char newword[SIZE],char newmeaning[MAX])
{
if(root==NULL)
{
root=new AVLnode;
root->left=root->right=NULL;
strcpy(root->cWord,newword);
strcpy(root->cMeaning,newmeaning);
}
else if(strcmp(root->cWord,newword)!=0)
{
if(strcmp(root->cWord,newword)>0)
{
root->left=insert(root->left,newword,newmeaning);
if(bf(root)==2)
{
if (strcmp(root->left->cWord,newword)>0)
root=LL(root);
else
root=LR(root);
}
}
else if(strcmp(root->cWord,newword)<0)
{
root->right=insert(root->right,newword,newmeaning);
if(bf(root)==-2)
{
if(strcmp(root->right->cWord,newword)>0)
root=RR(root);
else
root=RL(root);
}
}
}
else
cout<<"\nRedundant AVLnode";
root->iHt=height(root);
return root;
}
int AVLtree :: height(AVLnode* curr)
{
int lh,rh;
if(curr==NULL)
return 0;
if(curr->right==NULL && curr->left==NULL)
return 0;
else
{
lh=lh+height(curr->left);
rh=rh+height(curr->right);
if(lh>rh)
return lh+1;
return rh+1;
}
}
int AVLtree :: bf(AVLnode* curr)
{
int lh,rh;
if(curr==NULL)
return 0;
else
{
if(curr->left==NULL)
lh=0;
else
lh=1+curr->left->iHt;
if(curr->right==NULL)
rh=0;
else
rh=1+curr->right->iHt;
return(lh-rh);
}
}
AVLnode* AVLtree :: rotate_right(AVLnode* curr)
{
AVLnode* temp;
temp=curr->left;
curr->left=temp->right;
temp->left=curr;
curr->iHt=height(curr);
temp->iHt=height(temp);
return temp;
}
AVLnode* AVLtree :: rotate_left(AVLnode* curr)
{
AVLnode* temp;
temp=curr->right;
curr->right=temp->left;
temp->left=curr;
curr->iHt=height(curr);
temp->iHt=height(temp);
return temp;
}
AVLnode* AVLtree :: RR(AVLnode* curr)
{
curr=rotate_left(curr);
return curr;
}
AVLnode* AVLtree :: LL(AVLnode* curr)
{
curr=rotate_right(curr);
return curr;
}
AVLnode* AVLtree :: RL(AVLnode* curr)
{
curr->right=rotate_right(curr->right);
curr=rotate_left(curr);
return curr;
}
AVLnode* AVLtree::LR(AVLnode* curr)
{
curr->left=rotate_left(curr->left);
curr=rotate_right(curr);
return curr;
}
void AVLtree :: inorder(AVLnode* curr)
{
if(curr!=NULL)
{
inorder(curr->left);
cout<<"\n\t"<<curr->cWord<<"\t"<<curr->cMeaning;
inorder(curr->right);
}
}
int main()
{
int iCh;
AVLtree a;
AVLnode *curr=NULL;
char cWd[SIZE],cMean[MAX];
cout<<"\n--------------------------------------";
cout<<"\n\tAVL TREE IMPLEMENTATION";
cout<<"\n--------------------------------------";
do
{ cout<<"\n--------------------------------";
cout<<"\n\t\tMENU";
cout<<"\n--------------------------------";
cout<<"\n1.Insert\n2.Inorder\n3.Delete\n4.Exit";
cout<<"\n--------------------------------";
cout<<"\nEnter your choice :";
cin>>iCh;
switch(iCh)
{
case 1: cout<<"\nEnter Word : ";
cin>>cWd;
/*for(int i;cMean[i]!='\0';i++)
{
if(cMean[i]>='A'&& cMean[i]<='Z')
{
cMean[i]=cMean[i]+32;
}
}
cout<<cMean;*/
cout<<"\nEnter Meaning : ";
cin.ignore();
cin.getline(cMean,MAX);
a.root=a.insert(a.root,cWd,cMean);
break;
case 2: cout<<"\n\tWORD\tMEANING";
a.inorder(a.root);
break;
case 3: cout<<"\nEnter the word to be deleted : ";
cin>>cWd;
curr=a.delet(a.root,cWd);
if(curr==NULL)
cout<<"\nWord not present!";
else
cout<<"\nWord deleted Successfully!";
curr=NULL;
break;
case 4: exit(0);
}
}while(iCh!=4);
return 0;
}
// END OF CODE
// EXPERIMENTAL CODE

163
Practical-F23.cpp Normal file
View File

@ -0,0 +1,163 @@
// WARNING: THIS CODE HAS NOT BEEN TESTED.
// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE.
// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS,
// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY.
/*
Problem Statement: Department maintains a student information. The file contains roll number, name, division and address. Allow user to add, delete information of student. Display information of particular employee. If record of student does not exist an appropriate message is displayed. If it is, then the system displays the student details. Use sequential file to main the data.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
*/
// BEGINNING OF CODE
#include<iostream>
#include<fstream>
using namespace std;
class student
{
public:
char name[10];
int roll;
void getdata()
{
cout<<"\n Enter the roll no and name:";
cin>>roll>>name;
}
void putdata()
{
cout<<"\n The roll no and name:";
cout<<roll<<" "<<name;
}
};
class file
{
fstream fp;
public:
void create()
{
char ans;
student s;
fp.open("stu.dat",ios::out);
do
{
s.getdata();
fp.write((char *)&s,sizeof(s));
cout<<"\n More?";
cin>>ans;
}while(ans=='Y'||ans=='y');
fp.close();
}
void append()
{
char ans;
student s;
fp.open("stu.dat",ios::app);
do
{
s.getdata();
fp.write((char *)&s,sizeof(s));
cout<<"\n More?";
cin>>ans;
}while(ans=='Y'||ans=='y');
fp.close();
}
void display()
{
student s;
fp.open("stu.dat",ios::in);
while(!fp.eof())
{
fp.read((char *)&s,sizeof(s));
if (!fp.eof())
s.putdata();
}
fp.close();
}
void search()
{
student s; int flag=0;
cout <<"\n Enter roll to be searched:";
int r; cin >> r;
fp.open("stu.dat",ios::in);
while(!fp.eof())
{
fp.read((char *)&s,sizeof(s));
if (!fp.eof() && s.roll==r)
{ flag=1; s.putdata(); break; }
}
if (flag==0) cout << "\n Not found";
fp.close();
}
void update()
{
student s; int flag=0;
cout <<"\n Enter roll to be updated:";
int r; cin >> r;
fp.open("stu.dat",ios::in|ios::out);
while(!fp.eof())
{
fp.read((char *)&s,sizeof(s));
if (!fp.eof() && s.roll==r)
{
flag=1;
cout <<"\n Enter new data\n";
s.getdata();
fp.seekp(-1*sizeof(s),ios::cur);
fp.write((char *)&s,sizeof(s));
break;
}
}
if (flag==0) cout << "\n Not found";
fp.close();
}
void delete1()
{
student s; int flag=0;
fstream fp1;
cout <<"\n Enter roll to be deleted:";
int r; cin >> r;
fp.open("stu.dat",ios::in);
fp1.open("temp.dat",ios::out);
while(fp.read((char *)&s,sizeof(s)))
{
if ( s.roll!=r)
{
flag=1;
fp1.write((char *)&s,sizeof(s));
}
}
if (flag==0) cout << "\n Not found";
fp.close();
fp1.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
}
};
int main()
{
file f; int choice;
do{
cout << "\n1 create \n 2 display \n 3 Search \n 4 Append";
cout << " \n 6 delete \n 7 Update";
cout << "\n Enter choice:"; cin >> choice;
switch(choice)
{
case 1:f.create(); break;
case 2:f.display(); break;
case 3:f.search(); break;
case 4:f.append(); break;
case 6:f.delete1();break;
case 7:f.update();break;
}
}while(choice< 8);
}
// END OF CODE
// EXPERIMENTAL CODE

View File

@ -1,3 +1,9 @@
# testing branch # testing branch
⚠️⚠️⚠️ This branch contains untested codes. ⚠️⚠️⚠️ ⚠️⚠️⚠️ This branch contains untested codes. ⚠️⚠️⚠️
## Choose your poison:
- [main branch (all the codes are tested and relatively small in size)](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/main/Codes)
- [longCodes (long codes with minimal description)](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/longCodes)
- [testing (untested codes)](https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/src/branch/testing/) **CURRENT BRANCH**

311
git-commit-logs.txt Normal file
View File

@ -0,0 +1,311 @@
commit ac2019a188e70a130482aa1b1d1e4c11c5329133
Author: Kshitij <REDACTED_EMAIL>
Date: Sun Apr 28 20:01:04 2024 +0530
fixed link
commit c7e47972c93518b0cda76fcd1f9d3a509fe06496
Author: Kshitij <REDACTED_EMAIL>
Date: Sun Apr 28 18:21:04 2024 +0530
moved codes from codes folder to root. added readme
commit f06d2c5d3fd33dca037392b55aae87403b9a774c
Author: Kshitij <REDACTED_EMAIL>
Date: Sun Apr 28 18:02:59 2024 +0530
moved untested d19 to testing branch
commit 7183d9687df0090b8c2f68a73a5c41312d4a8809
Author: Kshitij <REDACTED_EMAIL>
Date: Sun Apr 28 17:46:10 2024 +0530
merged testing branch d18 with main and moved untested ones to testing branch
commit 1c3492cfa66ed5d70cbeb5eea323ab4b2d61cff3
Author: Kshitij <REDACTED_EMAIL>
Date: Tue Apr 23 09:14:10 2024 +0530
created a new branch for untested codes.
commit df26d18472c14ce3ee354181c8df782e7bb43d50
Author: Kshitij <REDACTED_EMAIL>
Date: Sun Apr 21 01:14:06 2024 +0530
kalas says this should be b11, haven't checked it
commit 26cdf0645d22d97d3de68188ad4519e9298b9c1f
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Mon Apr 15 22:08:29 2024 +0530
readme updated
commit 37eb1bb2a249de5747c6a8534706da6b3623cdf8
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Mon Apr 15 22:05:50 2024 +0530
d18 writeup added
commit 782308552c9c06b6fcff6482ef5a4ad428de9bb7
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Mon Apr 15 17:25:14 2024 +0530
readme updated
commit c83a54c7b46edde4e9d508aea30324ef36f156cc
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Mon Apr 15 17:23:17 2024 +0530
d18 code added
commit 982d35f3f92a47f716d203a7d3a4605c22c093ce
Author: Kshitij <REDACTED_EMAIL>
Date: Fri Apr 12 10:23:11 2024 +0530
updated readme
commit 05f35427c6f0a73d4903cd9818a42cbfe01fcfe8
Author: Kshitij <REDACTED_EMAIL>
Date: Fri Apr 12 10:17:46 2024 +0530
added ppt for u2 and added unit name in folders
commit e10f27dbcc19b8e540b915513bcfa1befed2a8ed
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Sun Apr 7 10:28:54 2024 +0530
readme updated
commit 215de628d0bc8f67648c56a2f67d689fe5c1ced1
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Sun Apr 7 10:25:06 2024 +0530
c13 writeup added
commit 8b59de4e805eb9f0e1b22b22fbd639030089bfb4
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Wed Apr 3 19:38:57 2024 +0530
readme updated
commit 17fac60a5ba9564e0a0b8d3f1790015cee3017dc
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Wed Apr 3 19:36:33 2024 +0530
b11 writup added
commit b29764306e61e4f3492a52aa7649c1cffa9daa6d
Author: Kshitij <REDACTED_EMAIL>
Date: Thu Mar 14 22:17:58 2024 +0530
added question bank
commit c0b2f0f4295063bcbccc254e4d51ab6c7177f4ee
Author: Kshitij <REDACTED_EMAIL>
Date: Thu Mar 14 21:58:14 2024 +0530
changed names for ppts to avoid confusion
commit 84fca23ba6e0368a6a9aa0de8967d0c1d69fba78
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Thu Mar 14 18:36:36 2024 +0530
readme updated
commit d48b2b2b0aa23c5d1b04fb16b1aa53653780bd64
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Thu Mar 14 18:32:44 2024 +0530
Notes By Deore Ma'am Added
commit af5653289a1b2f37e7604c41875a3c3f0c673d92
Author: Kshitij <REDACTED_EMAIL>
Date: Mon Mar 11 08:27:17 2024 +0530
added erp link
commit 4f0ed9d48bb85662967200b1d8be5e7656a3e97c
Author: Kshitij <REDACTED_EMAIL>
Date: Sun Mar 10 19:29:56 2024 +0530
added class notes for unit 1 and 2 (handwritten by Kalaskar)
commit 13c448f06e1b9ed7b444e1f15f6c9d4c5c79829e
Author: Kshitij <REDACTED_EMAIL>
Date: Wed Mar 6 13:05:53 2024 +0530
updated readme
commit febbc775a76dc9b05b806bb15a761ad29122969a
Author: Kshitij <REDACTED_EMAIL>
Date: Wed Mar 6 13:04:43 2024 +0530
added write up for C15
commit acf72dcbea351a48f2cea5ddabe727a6d2df6b5f
Author: Kshitij <REDACTED_EMAIL>
Date: Tue Feb 27 18:46:27 2024 +0530
added disclaimer for erp assignments
commit feb014875a82bdad5cee4a3470625a77e0703b02
Author: Kshitij <REDACTED_EMAIL>
Date: Mon Feb 26 22:04:23 2024 +0530
added questions and updated readme
commit 067ba94352e66d4f5bb43dea9f5653be984c310d
Author: Kshitij <REDACTED_EMAIL>
Date: Mon Feb 26 21:49:55 2024 +0530
moved practical write-up to write-ups folder, added erp assignment 1 and updated readme
commit be62430a8b0bf5fd892f28ec5e893d8011d05206
Author: Kshitij <REDACTED_EMAIL>
Date: Sat Feb 24 01:15:24 2024 +0530
final changes to A4 code
commit 736f047a3075115833a199cf3e4b249b2e17630b
Author: Kshitij <REDACTED_EMAIL>
Date: Sat Feb 24 01:11:31 2024 +0530
updated readme
commit 21b3b3b1286805b494f8689bcf87ad1532e6f2bd
Author: Kshitij <REDACTED_EMAIL>
Date: Sat Feb 24 01:07:52 2024 +0530
added A4
commit 537edadc186bf33d1fa4b6c1587701228c80751a
Author: Kshitij <REDACTED_EMAIL>
Date: Sat Feb 24 01:05:11 2024 +0530
fixed A1 code
commit 3c720a40748f57d3bc92ed37421cf43819af1e3d
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Thu Feb 22 23:35:56 2024 +0530
readme updated
commit 962a5eca8c9ec423f1361f2a5c2d0d1a9e6db07b
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Thu Feb 22 23:34:02 2024 +0530
B7 added finally
commit c33f887e27ba2ca3885704f1f17d294f72882dd6
Author: Kshitij <REDACTED_EMAIL>
Date: Sat Feb 17 18:39:00 2024 +0530
LH10 added iterator
commit 27069b283c822fd013897989920ffeeb5236161f
Author: Kshitij <REDACTED_EMAIL>
Date: Fri Feb 16 17:57:07 2024 +0530
fixed the file extension for Practical-A4 (in README too) and updated the code
commit 2312eb22eb4a2a0c923a0bb5ac907e8640427f1a
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Fri Feb 16 00:46:05 2024 +0530
a1 readme updated
commit 898a9217638823824b95abaf66024c3ec4d60eda
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Fri Feb 16 00:43:13 2024 +0530
a1 writup added
commit 1bec1716dffe77cf2a35699f8d149630acd6a1d1
Author: Kshitij <REDACTED_EMAIL>
Date: Thu Feb 15 09:47:56 2024 +0530
updated A4 to python
commit 4a3c14afefcb5f61d3ee407e2d4270d9adaac0be
Author: Kshitij <REDACTED_EMAIL>
Date: Wed Feb 14 18:59:22 2024 +0530
updated readme
commit be57577b33c85d8ecd3edf8516ab7ca7aa57483e
Author: Kshitij <REDACTED_EMAIL>
Date: Wed Feb 14 18:56:07 2024 +0530
Added maximum codes. All of them are experimental
commit a8611df79655dd43d1b7a5eb00bd01918a51f360
Author: Kshitij <REDACTED_EMAIL>
Date: Wed Feb 14 17:48:31 2024 +0530
Moved assignments to assignments folder
commit 09814817a79242db374aa270afa532ef8b49dd6d
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Tue Feb 13 20:21:01 2024 +0530
readme updated
commit 8ab9fd47a62ecccdf693ea871892af7cdbdc2ace
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Tue Feb 13 20:19:30 2024 +0530
trees b5 added
commit 493380ae3442c61a1b57c7f4a8c03c13810cca60
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Tue Feb 13 19:10:42 2024 +0530
readme updated
commit c0aac9b595f57f0bfda6c5337d62190bc3f74c42
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Tue Feb 13 19:04:50 2024 +0530
assgn2 sets added
commit ba4aa9148a5a3683a69ed5330e58ae13aff40196
Author: Kshitij <REDACTED_EMAIL>
Date: Mon Feb 12 23:11:45 2024 +0530
Added links for lab manuals and notes
commit 62ce9f7b39c86383c79c2f50b6d5f38131376c4e
Author: Kshitij <REDACTED_EMAIL>
Date: Mon Feb 12 23:03:59 2024 +0530
Added lab manuals and unit 2 notes (trees)
commit c5ae5966f68fd92efd6147171c85b7359432abd6
Author: Kshitij <REDACTED_EMAIL>
Date: Mon Feb 5 00:47:04 2024 +0530
Updated README
commit a41b889c86414780702610bb2cdd71e1f51f93d0
Author: Kshitij <REDACTED_EMAIL>
Date: Mon Feb 5 00:45:49 2024 +0530
Added PYQs
commit b65371317d5ee6fdcb2a69354c82e6007fcef7db
Author: Kshitij <REDACTED_EMAIL>
Date: Sun Feb 4 20:36:34 2024 +0530
Added DISCLAIMER, LICENSE file, updated README, moved unit 1 notes to Notes folder
commit fb6be4c703a4f44282bbb9ac4eea3b125ec7eff2
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Mon Jan 29 19:14:47 2024 +0530
readme updated
commit 879adeaef5f1aa11564b230c7ddc7a82b0ced9ab
Author: TanmayMachkar <REDACTED_EMAIL>
Date: Mon Jan 29 19:13:50 2024 +0530
u1 added