




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





//Binary Search Tree Program
#include
#include
int Person::getPhonenumber() { return phonenumber; } string Person::getaddress() { return address; } void Person::setName(string newname) { name = newname; } void Person::setPhonenumber(int newphonenumber) { phonenumber = newphonenumber; } void Person::setaddress(string newaddress) { address = newaddress; } // Smaller elements go left // larger elements go right void BinarySearchTree::insert(Person p) { tree_node* t = new tree_node; tree_node* parent; t->data = p; t->left = NULL; t->right = NULL; parent = NULL; // is this a new tree? if (isEmpty()) root = t; else { //Note: ALL insertions are as leaf nodes tree_node* curr; curr = root; // Find the Node's parent while (curr) { parent = curr; if (t->data.getName() > curr->data.getName()) curr = curr->right; else curr = curr->left; } if (t->data.getName() < parent->data.getName()) parent->left = t; else parent->right = t; } } void BinarySearchTree::remove(string p) { //Locate the element bool found = false; if (isEmpty()) { cout << " This Tree is empty! " << endl; return; } tree_node* curr; tree_node* parent; curr = root;
//Node with 2 children // replace node with smallest value in right subtree if (curr->left != NULL && curr->right != NULL) { tree_node* chkr; chkr = curr->right; if ((chkr->left == NULL) && (chkr->right == NULL)) { curr = chkr; delete chkr; curr->right = NULL; } else // right child has children { //if the node's right child has a left child // Move all the way down left to locate smallest element if ((curr->right)->left != NULL) { tree_node* lcurr; tree_node* lcurrp; lcurrp = curr->right; lcurr = (curr->right)->left; while (lcurr->left != NULL) { lcurrp = lcurr; lcurr = lcurr->left; } curr->data = lcurr->data; delete lcurr; lcurrp->left = NULL; } else { tree_node* tmp; tmp = curr->right; curr->data = tmp->data; curr->right = tmp->right; delete tmp; } } return; } } void BinarySearchTree::print_inorder() { inorder(root); } void BinarySearchTree::inorder(tree_node* p) {
if (p != NULL) { if (p->left) inorder(p->left); cout << " " << p->data.getName() << "\t\t" << p->data.getPhonenumber() << "\t\t" << p->data.getaddress() << endl; if (p->right) inorder(p->right); }
else return;
}
//////////////////new///////////////////////// void BinarySearchTree::search(string key) { bool found = false; if (isEmpty()) { cout << " This Tree is empty! " << endl; return; } tree_node* curr; tree_node* parent; curr = root; while (curr != NULL) { if (curr->data.getName() == key) { found = true; cout << "The phone number for " << key << " is " << curr-
data.getPhonenumber() << endl; break; } else { parent = curr; if (key>curr->data.getName()) curr = curr->right; else curr = curr->left; } } if (!found) { cout << " Data not found! " << endl; return; } } void BinarySearchTree::changePhonenumber(string p, int newnumber) { bool found = false; if (isEmpty()) { cout << " This Tree is empty! " << endl; return; } tree_node* curr; tree_node* parent; curr = root; while (curr != NULL) { if (curr->data.getName() == p) { found = true; break; } else { parent = curr; if (p>curr->data.getName()) curr = curr->right;
cout << " All Contacts Information " << endl; cout << " ------------------------" << endl; cout << " NAME\t\tPHONE\t\tAddress" << endl; b.print_inorder(); break; case 3: cout << " Enter data to be deleted : "; //cout << "Deletion needs to be implemented." << endl; cin >> key; b.remove(key); break; case 4: cout << " Enter the name of the person whose number you wish to change: " << endl; cin >> name; cout << endl << " Enter the new phonenumber: " << endl; cin >> phonenumber; cout << endl; b.changePhonenumber(name, phonenumber); break; case 5: return 0;