41
41
#include<iostream>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
// MAIN FUNCTION
int main() {
Node* root = nullptr;
int choice, value;
do {
cout << "\n------ BINARY SEARCH TREE ------\n";
cout << "1. INSERT\n";
cout << "2. LONGEST PATH (HEIGHT)\n";
cout << "3. MINIMUM VALUE\n";
cout << "4. MIRROR TREE\n";
cout << "5. SEARCH\n";
cout << "6. EXIT\n";
cout << "ENTER YOUR CHOICE: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
root = insert(root, value);
break;
case 2:
cout << "Longest path (height) of tree: " << height(root) << endl;
break;
case 3:
value = findmin(root);
if (value == -1)
cout << "Tree is empty.\n";
else
cout << "Minimum value: " << value << endl;
break;
case 4:
root = mirror(root);
cout << "Tree has been mirrored.\n";
break;
case 5:
cout << "Enter value to search: ";
cin >> value;
cout << (search(root, value) ? "FOUND\n" : "NOT FOUND\n");
break;
case 6:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
return 0;
}
1
n=int(input("Enter the size of heap table: "))
hashtable=[-1]*n;
print("1.Linear Probing")
print("2.Chaning")
collision_method=int(input("Enter the colision method: "))
def hash_fun(phone):
return phone % n
def insert(phone):
val=hash_fun(phone)
if collision_method==1:
return linear_prob(val,phone)
else:
return chaining(val,phone)
def linear_prob(val,phone):
while hashtable[val]!= -1:
val=(val+1)% n
hashtable[val]=phone
return hashtable
return hashtable
def search(phone):
val=hash_fun(phone)
ori_val=val
while hashtable[val]!=-1:
if hashtable[val]==phone:
return val
val=(val+1)%n
if val == ori_val:
break
return -1
def delete(phone):
val=hash_fun(phone)
org_val=val
while hashtable[val]!=-1:
if hashtable[val]==phone:
delete_val=phone
hashtable[val]=-1
return f"Element deleted: {delete_val}"
if org_val==val:
break
return "Element Not Found"
while True:
a=int(input("Enter Choice \n1.Insert \n2.Delete \n3.Search \n4.Display\n"))
if a==1:
phone=int(input("Enter the Phone No: "))
g=insert(phone)
if a==2:
phone=int(input("Enter Element To Delete: "))
d=delete(phone)
print(d)
if a==3:
phone=int(input("Enter Number to Search: "))
s=search(phone)
if s!=-1:
print(f"Phone number {phone} found at index{s}")
else:
print(f"phone number {phone} Not found")
if a==4:
print("Hash Table",g)