DS Experiment
DS Experiment
public:
HashMap(){
// Initial capacity of hash array
capacity = 20;
size = 0;
arr = new HashNode<K, V> *[capacity];
// Initialise all elements of array as NULL
for (int i = 0; i < capacity; i++)
arr[i] = NULL;
// dummy node with value and key -1
dummy = new HashNode<K, V>(-1, -1);
}
// This implements hash function to find index
// for a key
int hashCode(K key) { return key % capacity; }
// Function to add key value pair
void insertNode(K key, V value){
HashNode<K, V> *temp = new HashNode<K, V>(key,
value);
// Apply hash function to find index for given key
int hashIndex = hashCode(key);
// find next free space
while (arr[hashIndex] != NULL && arr[hashIndex]->key !=
key && arr[hashIndex]->key != -1){
hashIndex++;
hashIndex %= capacity;
}
// if new node to be inserted
// increase the current size
if (arr[hashIndex] == NULL || arr[hashIndex]->key == -1)
size++;
arr[hashIndex] = temp;
}
// Function to delete a key value pair
V deleteNode(int key) {
// Apply hash function
// to find index for given key
int hashIndex = hashCode(key);
// finding the node with given key
while (arr[hashIndex] != NULL){
// if node found
if (arr[hashIndex]->key == key){
HashNode<K, V> *temp = arr[hashIndex];
// Insert dummy node here for further use
arr[hashIndex] = dummy;
// Reduce size
size--;
return temp->value;
}
hashIndex++;
hashIndex %= capacity;
}
// If not found return null
return NULL;
}
// Function to search the value for a given key
V get(int key){
// Apply hash function to find index for given key
int hashIndex = hashCode(key);
int counter = 0;
// finding the node with given key
while (arr[hashIndex] != NULL){ // int
counter =0; // BUG!
if (counter++ > capacity) // to avoid infinite loop
return NULL;
// if node found return its value
if (arr[hashIndex]->key == key)
return arr[hashIndex]->value;
hashIndex++;
hashIndex %= capacity;
}
// If not found return null
return NULL;
}
// Return current size
int sizeofMap() { return size; }
// Return true if size is 0
bool isEmpty() { return size == 0; }
// Function to display the stored key value pairs
void display(){
for (int i = 0; i < capacity; i++){
if (arr[i] != NULL && arr[i]->key != -1)
cout << "key = " << arr[i]->key
<< " value = " << arr[i]->value
<< endl;
}
}
};
// Driver method to test map class
int main(){
HashMap<int, int> *h = new HashMap<int, int>;
h->insertNode(1, 1);
h->insertNode(2, 2);
h->insertNode(2, 3);
h->display();
cout << h->sizeofMap() << endl;
cout << h->deleteNode(2) << endl;
cout << h->sizeofMap() << endl;
cout << h->isEmpty() << endl;
cout << h->get(2);
return 0;
}
Output:
key = 1 value = 1
key = 2 value = 3
2
3
1
0
0
public:
// Constructor to initialize the tree
BinaryTree() : root(nullptr) {}
// Function to insert a node in the binary tree
void insertNode(T value){
Node<T> *newNode = new Node<T>(value);
if (root == nullptr){
root = newNode;
return;
}
queue<Node<T> *> q;
q.push(root);
while (!q.empty()){
Node<T> *current = q.front();
q.pop();
if (current->left == nullptr){
current->left = newNode;
return;
}
else{
q.push(current->left);
}
if (current->right == nullptr){
current->right = newNode;
return;
}
else{
q.push(current->right);
}
}
}
// Function to delete a node from the tree
void deleteNode(T value){
root = deleteRecursive(root, value);
}
// Function to search for a value in the tree
bool search(T value){
return searchRecursive(root, value);
}
// Function to perform inorder traversal of the tree
void inorder(){
inorderRecursive(root);
cout << endl;
}
// Function to perform preorder traversal of the tree
void preorder(){
preorderRecursive(root);
cout << endl;
}
// Function to perform postorder traversal of the tree
void postorder(){
postorderRecursive(root);
cout << endl;
}
// Function to perform level order traversal of the tree
void levelOrder(){
if (root == nullptr)
return;
queue<Node<T> *> q;
q.push(root);
while (!q.empty()){
Node<T> *current = q.front();
q.pop();
cout << current->data << " ";
if (current->left != nullptr)
q.push(current->left);
if (current->right != nullptr)
q.push(current->right);
}
cout << endl;
}
};
int main(){
BinaryTree<int> tree;
// Insert the nodes into the tree
tree.insertNode(1);
tree.insertNode(2);
tree.insertNode(3);
tree.insertNode(4);
tree.insertNode(5);
tree.insertNode(6);
cout << "Inorder traversal: ";
tree.inorder();
cout << "Preorder traversal: ";
tree.preorder();
cout << "Postorder traversal: ";
tree.postorder();
cout << "Level order traversal: ";
tree.levelOrder();
cout << "Searching for 7: " << (tree.search(7) ? "Found" :
"Not Found") << endl;
cout << "Searching for 6: " << (tree.search(6) ? "Found" :
"Not Found") << endl;
tree.deleteNode(3);
cout << "Inorder traversal after removing 3: ";
tree.inorder();
return 0;
}
OUTPUT:
[aiml2341@Aids ds]$ Inorder traversal: 4 2 5 1 6 3
Preorder traversal: 1 2 4 5 3 6
Postorder traversal: 4 5 2 6 3 1
Level order traversal: 1 2 3 4 5 6
Searching for 7: Not Found
Searching for 6: Found
Inorder traversal after removing 3: 4 2 5 1 6
OUTPUT:
[aiml2315@Aids ds]$ Inorder traversal of the given Binary
Search Tree is: 20 30 40 50 60 70 80
After deletion of 20: 30 40 50 60 70 80
After insertion of 25: 25 30 40 50 60 70 80
Node 25 found in the BST.