Docsity

bINARY sEARCH tREE pROGRAM, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
//Binary Search Tree Program
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <stdio.h>
using namespace std;
//typedef string ItemType;
class Person{
private:
string name;
int phonenumber;
string address;
public:
Person();
Person(string name, int phonenumber, string address);
string getName();
int getPhonenumber();
string getaddress();
void setName(string newname);
void setPhonenumber(int newphonenumber);
void setaddress(string newaddress);
};
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
//ItemType data;
Person data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root == NULL; }
void print_inorder();
void inorder(tree_node*);
void insert(Person);
void remove(string);
void search(string key);
void changePhonenumber(string key, int newnumber);
};
Person::Person()
{
}
Person::Person(string newname, int newphonenumber, string newaddress)
{
name = newname;
phonenumber = newphonenumber;
address = newaddress;
}
string Person::getName() {
return name;
pf3
pf4
pf5
pf8

Partial preview of the text

Download bINARY sEARCH tREE pROGRAM and more Study notes Data Structures and Algorithms in PDF only on Docsity!

//Binary Search Tree Program

#include #include

#include #include <stdio.h> using namespace std; //typedef string ItemType; class Person{ private: string name; int phonenumber; string address; public: Person(); Person(string name, int phonenumber, string address); string getName(); int getPhonenumber(); string getaddress(); void setName(string newname); void setPhonenumber(int newphonenumber); void setaddress(string newaddress); }; class BinarySearchTree { private: struct tree_node { tree_node* left; tree_node* right; //ItemType data; Person data; }; tree_node* root; public: BinarySearchTree() { root = NULL; } bool isEmpty() const { return root == NULL; } void print_inorder(); void inorder(tree_node*); void insert(Person); void remove(string); void search(string key); void changePhonenumber(string key, int newnumber); }; Person::Person() { } Person::Person(string newname, int newphonenumber, string newaddress) { name = newname; phonenumber = newphonenumber; address = newaddress; } string Person::getName() { return name;

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;