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

DSA Lab Practicals Final

The document contains implementations of various data structures in Python and C++, including a Hash Table, Set ADT, Binary Search Tree, Expression Tree, and General Tree. Each implementation includes class definitions, methods for insertion and traversal, and example usage with outputs demonstrating their functionality. The author of the document is Suraj Surve, and the roll number is SA61.

Uploaded by

Suraj Surve
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DSA Lab Practicals Final

The document contains implementations of various data structures in Python and C++, including a Hash Table, Set ADT, Binary Search Tree, Expression Tree, and General Tree. Each implementation includes class definitions, methods for insertion and traversal, and example usage with outputs demonstrating their functionality. The author of the document is Suraj Surve, and the roll number is SA61.

Uploaded by

Suraj Surve
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Practical 1: Hash Table Implementation (Python)

Name: Suraj Surve

Roll No: SA61

Code:
class HashTable:
def __init__(self, size):
self.size = size
self.table = [None] * size

def hash_function(self, key):


return key % self.size

def insert(self, key, value):


index = self.hash_function(key)
while self.table[index] is not None:
index = (index + 1) % self.size
self.table[index] = (key, value)

def search(self, key):


index = self.hash_function(key)
start_index = index
while self.table[index] is not None:
if self.table[index][0] == key:
return self.table[index][1]
index = (index + 1) % self.size
if index == start_index:
break
return None

# Example usage
ht = HashTable(10)
ht.insert(12, "Alice")
ht.insert(22, "Bob")
ht.insert(32, "Charlie")

print("Search 12:", ht.search(12))


print("Search 22:", ht.search(22))
print("Search 99:", ht.search(99))
Output:
Search 12: Alice
Search 22: Bob
Search 99: None
Practical 2: Set ADT Implementation (Python)
Name: Suraj Surve

Roll No: SA61

Code:
class SetADT:
def __init__(self):
self.set_data = set()

def add(self, value):


self.set_data.add(value)

def remove(self, value):


self.set_data.discard(value)

def union(self, other_set):


return self.set_data | other_set.set_data

def intersection(self, other_set):


return self.set_data & other_set.set_data

def difference(self, other_set):


return self.set_data - other_set.set_data

# Example usage
set1 = SetADT()
set2 = SetADT()

set1.add(1)
set1.add(2)
set1.add(3)
set2.add(3)
set2.add(4)
set2.add(5)

print("Set 1:", set1.set_data)


print("Set 2:", set2.set_data)
print("Union:", set1.union(set2))
print("Intersection:", set1.intersection(set2))
print("Difference:", set1.difference(set2))
Output:
Set 1: {1, 2, 3}
Set 2: {3, 4, 5}
Union: {1, 2, 3, 4, 5}
Intersection: {3}
Difference: {1, 2}
Practical 3: Binary Search Tree Implementation (C++)
Name: Suraj Surve

Roll No: SA61

Code:
#include <iostream>
using namespace std;

class Node {
public:
int value;
Node* left;
Node* right;
Node(int key) {
value = key;
left = right = NULL;
}
};

class BST {
public:
Node* root;
BST() { root = NULL; }

void insert(int key) {


root = insertRec(root, key);
}

Node* insertRec(Node* root, int key) {


if (root == NULL) return new Node(key);
if (key < root->value)
root->left = insertRec(root->left, key);
else
root->right = insertRec(root->right, key);
return root;
}

void inorder(Node* root) {


if (root) {
inorder(root->left);
cout << root->value << " ";
inorder(root->right);
}
}
};

int main() {
BST tree;
tree.insert(50);
tree.insert(30);
tree.insert(70);
tree.insert(20);
tree.insert(40);
tree.insert(60);
tree.insert(80);

cout << "Inorder traversal of the BST:\n";


tree.inorder(tree.root);
return 0;
}

Output:
Inorder traversal of the BST:
20 30 40 50 60 70 80
Practical 4: Expression Tree from Prefix Expression (C++)
Name: Suraj Surve

Roll No: SA61

Code:
#include <iostream>
#include <stack>
using namespace std;

class Node {
public:
char value;
Node* left;
Node* right;
Node(char val) {
value = val;
left = right = NULL;
}
};

Node* constructExpressionTree(string prefix) {


stack<Node*> s;
for (int i = prefix.length() - 1; i >= 0; i--) {
Node* node = new Node(prefix[i]);
if (isalnum(prefix[i])) {
s.push(node);
} else {
node->left = s.top(); s.pop();
node->right = s.top(); s.pop();
s.push(node);
}
}
return s.top();
}

void postorderTraversal(Node* root) {


if (root) {
postorderTraversal(root->left);
postorderTraversal(root->right);
cout << root->value << " ";
}
}

int main() {
string expression = "+--a*bc/def";
Node* root = constructExpressionTree(expression);
cout << "Postorder traversal of the expression tree:\n";
postorderTraversal(root);
return 0;
}

Output:
Postorder traversal of the expression tree:
ab*c-de/f-+
Practical 5: General Tree Implementation (C++)
Name: Suraj Surve

Roll No: SA61

Code:
#include <iostream>
#include <vector>
using namespace std;

class GeneralTreeNode {
public:
string label;
vector<GeneralTreeNode*> children;

GeneralTreeNode(string lbl) {
label = lbl;
}

void addChild(GeneralTreeNode* child) {


children.push_back(child);
}
};

void printTree(GeneralTreeNode* root, int level = 0) {


if (root) {
cout << string(level * 4, ' ') << root->label << endl;
for (auto child : root->children) {
printTree(child, level + 1);
}
}
}

int main() {
GeneralTreeNode* root = new GeneralTreeNode("Book");
GeneralTreeNode* chapter1 = new GeneralTreeNode("Chapter 1");
GeneralTreeNode* section1 = new GeneralTreeNode("Section 1.1");
GeneralTreeNode* section2 = new GeneralTreeNode("Section 1.2");

chapter1->addChild(section1);
chapter1->addChild(section2);
root->addChild(chapter1);
cout << "Tree Structure:\n";
printTree(root);

return 0;
}

Output:
Tree Structure:
Book
Chapter 1
Section 1.1
Section 1.2

You might also like