0% found this document useful (0 votes)
12 views5 pages

41

Uploaded by

abhipathak416
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

41

Uploaded by

abhipathak416
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4

#include<iostream>
using namespace std;
struct Node {
int data;
Node *left, *right;
};

// Create a new node


Node* create(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->left = newNode->right = nullptr;
return newNode;
}

// Insert a node in BST


Node* insert(Node* root, int value) {
if (!root) return create(value);
if (value < root->data)
root->left = insert(root->left, value);
else
root->right = insert(root->right, value);
return root;
}

// Find height (longest path)


int height(Node* root) {
if (!root) return 0;
int left = height(root->left);
int right = height(root->right);
return max(left, right) + 1;
}

// Find minimum value in BST


int findmin(Node* root) {
if (!root) return -1; // Tree is empty
while (root->left)
root = root->left;
return root->data;
}

// Convert tree to mirror image


Node* mirror(Node* root) {
if (!root) return nullptr;
mirror(root->left);
mirror(root->right);
swap(root->left, root->right);
return root;
}
// Search a key in BST
bool search(Node* root, int key) {
if (!root) return false;
if (key == root->data) return true;
if (key < root->data)
return search(root->left, key);
else
return search(root->right, key);
}

// 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";
}

} while (choice != 6);

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

def chaining(val, phone):


if hashtable[val] != -1:
if type(hashtable[val]) != list:
temp = hashtable[val]
hashtable[val] = [temp]
hashtable[val] = hashtable[val] + [phone]
else:
hashtable[val] = phone

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)

You might also like