Sample Program Using a BST


//  this program uses a BST to create an address book. the BST holds Person objects.
//  the address book entries are read from a file into the BST. then the user
//  is prompted to look people up. if a person is found, their entry is printed.
//  if a person is not found, the user is prompted to see whether the person should
//  be added to the address book. at the end of the program the user is prompted to
//  see if they want to see the whole address book. if so it is printed using
//  inorder traversal

#include <iostream.h>
#include <fstream.h>
#include <string>
#include "BST.h"
#include "Person.h"

void buildAddrBook(BST& addrbook);
void addEntry(BST& addrbook);
void dispPerson(treeItemType&);

int main()
{
   BST addrbook;
   Person Entry;
   string nameLook;
   char again;
   int ok;

//  create address book from input file
   buildAddrBook(addrbook);
   do
   {

//  prompt user for name to look up
      cout << "Address/Phone Lookup:\n" << "Enter name: ";
      getline(cin,nameLook);
      addrbook.SearchTreeRetrieve(nameLook,Entry,ok);
      if (ok)
      {

//  print the address book entry
         cout << endl << "Address Book Entry\n";
         cout << "__________________"<< endl;
         Entry.print();
         cout << endl;
      }
      else
      {

//  if the person is not in the book, see if they should be added
         cout << "\n" << nameLook << " not found in address book\n";
         cout << "\nWould you like to add this person? [y or n] => ";
         cin >> again;
         cin.ignore();
         if (again == 'Y' || again == 'y')
            addEntry(addrbook);
      }
      cout << "\nAnother Lookup? [y or n] => ";
      cin >> again;
      cin.ignore();
   }
   while (again == 'y' || again == 'Y');

//  prompt the user to see if they want to see the entire address book before exiting
   cout << "\nPrint the address book? [y or n] => ";
   cin >> again;
   if (again == 'Y' || again == 'y')
      addrbook.InorderTraverse(dispPerson);
}

//  read entries from file and insert them into the address book
void buildAddrBook(BST& addrbook)
{
   Person hold;
   string name;
   string street;
   string town;
   string phone;
   ifstream addresses;
   int ok = 1;

   addresses.open("addrbook.dat");
   getline(addresses,name);
   while (addresses && ok)
   {
      getline(addresses,street);
      getline(addresses,town);
      getline(addresses,phone);
      hold.setPerson(name,phone,street,town);
      addrbook.SearchTreeInsert(hold,ok);
      getline(addresses,name);
   }
}

//  add a new entry to the address book
void addEntry(BST& addrbook)
{
   int ok;
   Person hold;
   string name;
   string street;
   string town;
   string phone;

   cout << "\nEnter name: ";
   getline(cin,name);
   cout << "Enter street address: ";
   getline(cin,street);
   cout << "Enter town, state, and zip: ";
   getline(cin,town);
   cout << "Enter phone number: ";
   getline(cin,phone);
   hold.setPerson(name,phone,street,town);
   addrbook.SearchTreeInsert(hold,ok);
   if (!ok)
      cout << "\nERROR: Entry not added\n";
}

//  function to print out a Person -- used by inorder traversal
void dispPerson(treeItemType& person)
{
   cout << endl;
   person.print();
}

//  header file for Person class

#ifndef PERSON_H
#define PERSON_H

#include <string>

class Person
{
public:
   Person() { }
   Person(string& nn,string& ph,string& st,string& tt)
                   { theName = nn; phone = ph; street = st; town = tt; }
   void setPerson(string& nn,string& ph,string& st,string& tt)
      { theName = nn; phone = ph; street = st; town = tt; }
   string name() { return theName; }
   string Key() { return theName; }
   void print() { cout << endl << theName << endl << street << endl << town
                           << endl << phone << endl; }
private:
   string theName;
   string phone;
   string street;
   string town;
};

#endif

Input File

Sara Asness
1700 Old York Rd
Philadelphia, PA 19144
215-224-3106
Vikram Seth
2339 Telegraph Ave
Berkeley, CA 90312
415-444-5555
Dora Schatz
1602 Glenloch St
Philadelphia, PA 19152
215-338-3106
Yong-fong Lee
19 Redwood Ave
Santa Clara, CA 91337
508-333-4567

Output


Address/Phone Lookup:
Enter name: Sara Asness

Address Book Entry
__________________

Sara Asness
1700 Old York Rd
Philadelphia, PA 19144
215-224-3106


Another Lookup? [y or n] => y
Address/Phone Lookup:
Enter name: Vikram Seth

Address Book Entry
__________________

Vikram Seth
2339 Telegraph Ave
Berkeley, CA 90312
415-444-5555


Another Lookup? [y or n] => y
Address/Phone Lookup:
Enter name: Jackie Robinson

Jackie Robinson not found in address book

Would you like to add this person? [y or n] => y

Enter name: Jackie Robinson
Enter street address: 192 Seventh Ave
Enter town, state, and zip: Brooklyn, NY 11215
Enter phone number: 718-888-3344

Another Lookup? [y or n] => y
Address/Phone Lookup:
Enter name: Mom

Mom not found in address book

Would you like to add this person? [y or n] => y

Enter name: Mom
Enter street address: 2836 Lehigh Ave
Enter town, state, and zip: Philadelphia, PA 19136
Enter phone number: 215-635-8888

Another Lookup? [y or n] => n

Print the address book? [y or n] => y


Dora Schatz
1602 Glenloch St
Philadelphia, PA 19152
215-338-3106


Jackie Robinson
192 Seventh Ave
Brooklyn, NY 11215
718-888-3344


Mom
2836 Lehigh Ave
Philadelphia, PA 19136
215-635-8888


Sara Asness
1700 Old York Rd
Philadelphia, PA 19144
215-224-3106


Vikram Seth
2339 Telegraph Ave
Berkeley, CA 90312
415-444-5555


Yong-fong Lee
19 Redwood Ave
Santa Clara, CA 91337
508-333-4567


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002