Data Structure c++ - hashing (Binary Search Tree)
Client_Address_data.txt
Ramos Rafael 220 SW 3rd Street 447-4533 Enirata Jorge 6200 Miami Avenue 446-6666 Sexy Lady 400 Street East 555-1212 Smith John 444 SW 2nd Ave 420-6868 Winner Mary 3669 W Citrus Trace Drive 434-1717 Valdez Jorge 11885 SW 13th Court 476-8899 Valdes Abel 3353 Davie Blvd 587-1043 Smith Mary 444 SW 2nd Ave 420-6808 Edn Susan 8881 SW 49th Street 746-6998 Harper Fred 6815 NW 12th Street 572-3326 Tempero Douglas 436 NW 81st Street 462-1478 Utnik Richard 182 SW 51st Street 797-9905 Utich Michael 412 6th Street 556-8200 Lehrer Paul 9660 Sunrise Drive 485-6000 Leger George 811 SW 31st Street 390-7915 Harper Mary 6815 NW 12th Street 572-3326 Quass Claude North Ocean Drive 564-0674 Quinn Medicinewoman 4729 SW 42 Street 316-4455 Harper Jean 6815 NW 12th Street 572-3326 Harper James 6815 NW 12th Street 572-3326 Harper Babara 6815 NW 12th Street 572-3326 Edmundson Donald 321 Sunset Drive 745-6666 Ramos Miryam Post Gardens Way 393-6627 Cleary Kay 6250 SW 6th St 584-1023 Cleary Adele 3548 Envioronment Blvd 677-1056 MacDonald John 5601 Dixie Hwy 772-6712 Ilter Mehmet 2173 Bay Court 349-3128 Ilter Kermit 2163 Bay Court 349-3228 Ilter Maha 2163 Bay Court 349-3328 Macci Vicky 3535 Oceana Drive 446-8663 Tann Kyle 3027 N. Oakland Forest Drive 583-3362 Fox Smart 5600 Brian Street East 731-8685 Tanguay Albert 4806 NW 36st Street 731-7686 Tanguay Andre 555 University Drive 741-9728 Bullard Mark 770 SE 2nd Ave 393-7029 Bulldog Fred 555 W. Ocean Drive 737-0824 Bundle Joy 8352 Trent Ct 479-0349 Wickes Helen 1831 NE 38th Street 561-1228 Open Wide 5676 Street Avenue 543-0009 Wickham Fred 353 SW 18th Avenue 763-5252 Dunn Carrey 554 St. James Street 663-8900 Dandy Bread 1500 Publix Drive 555-1212 Nay John Eastlake Way 385-9286 Niwn Ni 6626 20th Street 888-0010 Young David 2121 NE 11th Ave 333-8483 Able Scott 9 NE 5th Court 456-0009 Abby Mary 3562 8th Street 492-4545 Young Andy 111 Cow Pen Road 448-8586 Young Celcil 6324 NW 29th Avenue 792-6327 Yau Kouassi 7801 NW 44th Ct 473-5456 Zangwill Andy 5990 Sunrise Lakes Blvd 746-9427 Jackson Jessie 2620 NW 21st Street 760-7511 Jackson Joanna 220 SW 20st Street 761-7211 Jackson Missy 20000 NW 21st Street 760-7111 Zangrille Juilus 2261 NE 67th Street 938-8919 Xyplex Networks 555 Seabreeze Blvd 713-8156 Xu Xinyun 4195 SW 67th Avenue 467-6556 Good Boy 1000 Heaven Street 111-1111 Kaiser Moe 1905 NW 46st Street 862-1156 Pretty Girl 19 Stay Home Avenue 234-5409 Tom Cat 111 Fish Avenue 333-3333 Kaiser Ronald 1905 NW 46st Street 422-5151 Kaiser Richard 1905 NW 46st Street 434-1106
hashing_BST.cpp
hashing_BST.cpp
//*****************************************************************************************************
// P R O G R A M H E A D E R
//
// Name:
// ID:
// Instructor: Dr. Bullard
// Class: Data Structures
// Term: Summer 2014
// Assignment #7 Hashing
// Due Date: August 1, 2014
// Due Time: 10:00 PM
// Points: 100
//
// Description:
// (1) Handling collision, (2) using hashing with linear functions, (3) implement a default constructor,
// destructor, (4) Hashing is enables access to table objects. Program will maintain the names (first and last names)
// addresses and phone numbers in an address book by using a hash table.
//
//
//
//
//
//****************************************************************************************************************
#include
<
iostream
>
#include
<
string
>
#include
<
fstream
>
using
namespace
std
;
class
BST_Node
//node in a BST
{
public
:
string firstname
,
lastname
,
address
,
phone_number
,
NewLastName
,
NewFirstName
,
value
;
BST_Node
*
lchild
,
*
rchild
;
//left and right children pointers
};
class
Clients_Info_BST
//Binary Search Tree
{
public
:
Clients_Info_BST
();
~
Clients_Info_BST
();
void
Insert
(
const
string
&
);
void
Insert_Aux
(
BST_Node
*
&
loc_ptr
,
const
string
);
void
Remove
(
const
string
&
);
void
Remove_Aux
(
BST_Node
*
loc_ptr
,
const
string
&
);
void
Update
(
const
string
&
s
);
void
Update_Aux
(
BST_Node
*
&
loc_ptr
,
string s
);
void
Print_Tree_Aux
(
BST_Node
*
);
void
Print
();
BST_Node
*
Search_Aux
(
BST_Node
*
loc_ptr
,
string object
);
BST_Node
*
Search
(
const
string
&
s
);
BST_Node
*
inorder_succ
(
BST_Node
*
);
//other member functions you may need.
private
:
BST_Node
*
root
;
//---state information
};
/*
*Function Name: Clients_Info_BST
Precondition: state of class has not be initialized
Postcondition: state has been initialized
Description: default constructor sets root to 0.*/
Clients_Info_BST
::
Clients_Info_BST
()
//store the data in the hash table
{
root
=
0
;
};
/*Function Name:~~Clients_Info_BST
Precondition: checks to see if root is not equal to 0.
Postcondition: left child and right child are going to get destroyed
Description: Destructor destroys left child first then right child then it
deletes root*/
Clients_Info_BST
::~
Clients_Info_BST
()
//destructor
{
if
(
root
!=
0
)
{
delete
root
->
lchild
;
delete
root
->
rchild
;
}
delete
root
;
}
/*Function Name:Remove
Precondition: function gets called with a string
Postcondition: function calls Remove_Aux
Description: string gets removed*/
void
Clients_Info_BST
::
Remove
(
const
string
&
s
)
{
Remove_Aux
(
root
,
s
);
};
/*Function Name:Remove_Aux
Precondition: function gets called with a BST_Node * ptr and a string
Postcondition: function checks to see if loc_ptr is 0 and if the last name is
greater than or less than
Description: string gets removed with direct recursion*/
void
Clients_Info_BST
::
Remove_Aux
(
BST_Node
*
loc_ptr
,
const
string
&
s
)
{
if
(
loc_ptr
==
0
)
cout
<<
"object not in tree\n"
;
else
if
(
loc_ptr
->
lastname
>
s
)
Remove_Aux
(
loc_ptr
->
lchild
,
s
);
else
if
(
loc_ptr
->
lastname
<
s
)
Remove_Aux
(
loc_ptr
->
rchild
,
s
);
else
{
BST_Node
*
ptr
;
if
(
loc_ptr
->
lchild
==
0
)
{
ptr
=
loc_ptr
->
rchild
;
delete
loc_ptr
;
loc_ptr
=
ptr
;
}
else
if
(
loc_ptr
->
rchild
==
0
)
{
ptr
=
loc_ptr
->
lchild
;
delete
loc_ptr
;
loc_ptr
=
ptr
;
}
else
{
ptr
=
inorder_succ
(
loc_ptr
);
loc_ptr
->
lastname
=
ptr
->
lastname
;
Remove_Aux
(
loc_ptr
->
rchild
,
ptr
->
lastname
);
}
}
}
/*Function Name:inorder_sucessor
Precondition: function gets called with BST *loc_ptr
Postcondition: ptr is set to the right child
Description: loop keeps going left until it reaches the last child*/
BST_Node
*
Clients_Info_BST
::
inorder_succ
(
BST_Node
*
loc_ptr
)
{
BST_Node
*
ptr
=
loc_ptr
->
rchild
;
while
(
ptr
->
lchild
!=
0
)
{
ptr
=
ptr
->
lchild
;
}
return
ptr
;
}
/*Function Name: Print
Precondition: Print gets called
Postcondition: print calls print_tree_aux on the root of the tree
Description: Data gets printed to screen*/
void
Clients_Info_BST
::
Print
()
{
Print_Tree_Aux
(
root
);
};
/*Function Name: Print
Precondition: Print gets called
Postcondition: print calls Print_Tree_Aux on the root of the tree
Description: Data gets printed to screen using direct recursion*/
void
Clients_Info_BST
::
Print_Tree_Aux
(
BST_Node
*
loc_ptr
)
{
if
(
loc_ptr
!=
0
)
{
Print_Tree_Aux
(
loc_ptr
->
lchild
);
cout
<<
loc_ptr
->
lastname
<<
" "
<<
loc_ptr
->
firstname
<<
" "
<<
loc_ptr
->
address
<<
" "
<<
loc_ptr
->
phone_number
<<
endl
;
Print_Tree_Aux
(
loc_ptr
->
rchild
);
}
}
/*Function Name:Insert(const string & s)
Precondition: A string gets passed in and a new node is created
Postcondition: Inserted into correct location
Description: string gets chopped into pieces by last name, first name and phone number*/
void
Clients_Info_BST
::
Insert
(
const
string
&
s
)
{
BST_Node
*
p
=
new
BST_Node
;
int
last
=
s
.
find
(
" "
,
0
);
p
->
lastname
=
s
.
substr
(
0
,
last
);
if
(
p
->
lastname
==
"1"
||
p
->
lastname
==
"2"
||
p
->
lastname
==
"3"
||
p
->
lastname
==
"4"
||
p
->
lastname
==
"5"
||
p
->
lastname
==
"6"
||
p
->
lastname
==
"7"
)
{
Update_Aux
(
root
,
s
);
}
else
{
Insert_Aux
(
root
,
s
);
}
}
/*Function Name:Insert_Aux(BST_Node * & loc_ptr, string object)
Precondition: A string gets passed in and a new node is created
Postcondition: Inserted into correct location
Description: string gets chopped into pieces by last name, first name and phone number. function uses
direct recursion*/
void
Clients_Info_BST
::
Insert_Aux
(
BST_Node
*
&
loc_ptr
,
string object
)
{
BST_Node
*
p
=
new
BST_Node
;
int
last
=
object
.
find
(
" "
,
0
);
p
->
lastname
=
object
.
substr
(
0
,
last
);
int
first
=
object
.
find
(
" "
,
last
+
1
);
p
->
firstname
=
object
.
substr
(
last
+
1
,
first
-
last
);
p
->
phone_number
=
object
.
substr
(
object
.
length
()
-
8
,
8
);
p
->
address
=
object
.
substr
(
first
+
1
,
object
.
length
()
-
9
-
first
);
if
(
loc_ptr
==
0
)
{
loc_ptr
=
new
BST_Node
;
loc_ptr
->
lchild
=
loc_ptr
->
rchild
=
0
;
loc_ptr
->
lastname
=
p
->
lastname
;
loc_ptr
->
firstname
=
p
->
firstname
;
loc_ptr
->
address
=
p
->
address
;
loc_ptr
->
phone_number
=
p
->
phone_number
;
}
else
if
(
loc_ptr
->
lastname
>
object
)
Insert_Aux
(
loc_ptr
->
lchild
,
object
);
else
if
(
loc_ptr
->
lastname
<
object
)
Insert_Aux
(
loc_ptr
->
rchild
,
object
);
else
cout
<<
"the object is already in the tree\n"
;
};
/*Function Name:Update(const string & s)
Precondition: A string gets passed in and a new node is created
Postcondition: function calls update_Aux
Description: string gets chopped into pieces by last name, first name and phone number. function uses
indirect recursion*/
void
Clients_Info_BST
::
Update
(
const
string
&
s
)
{
Update_Aux
(
root
,
s
);
};
/*Function Name:Update_Aux(BST_Node * & loc_ptr, string s)
Precondition: A string gets passed in and a new node is created
Postcondition: function calls update_Aux
Description: string gets chopped into pieces by last name, first name and phone number. function uses
indirect recursion*/
void
Clients_Info_BST
::
Update_Aux
(
BST_Node
*
&
loc_ptr
,
string s
)
{
BST_Node
*
p
=
new
BST_Node
;
int
val
=
s
.
find
(
" "
,
0
);
p
->
value
=
s
.
substr
(
0
,
val
);
int
newlast
=
s
.
find
(
" "
,
val
+
1
);
p
->
NewLastName
=
s
.
substr
(
val
+
1
,
newlast
-
val
);
int
newfirst
=
s
.
find
(
" "
,
newlast
+
1
);
p
->
NewFirstName
=
s
.
substr
(
newlast
+
1
,
newfirst
-
newlast
);
int
last
=
s
.
find
(
" "
,
newfirst
+
1
);
p
->
lastname
=
s
.
substr
(
newfirst
+
1
,
last
-
newfirst
);
int
first
=
s
.
find
(
" "
,
last
+
1
);
p
->
firstname
=
s
.
substr
(
first
+
1
,
first
-
last
);
p
->
phone_number
=
s
.
substr
(
s
.
length
()
-
8
,
8
);
p
->
address
=
s
.
substr
(
first
+
1
,
s
.
length
()
-
9
-
first
);
if
(
p
->
value
==
"1"
)
{
loc_ptr
->
lastname
=
p
->
NewLastName
;
loc_ptr
->
firstname
=
p
->
NewFirstName
;
loc_ptr
->
address
=
p
->
address
;
loc_ptr
->
phone_number
=
p
->
phone_number
;
}
if
(
p
->
value
==
"2"
)
{
loc_ptr
->
lastname
=
p
->
NewLastName
;
loc_ptr
->
firstname
=
p
->
NewFirstName
;
loc_ptr
->
address
=
p
->
address
;
}
if
(
p
->
value
==
"3"
)
{
loc_ptr
->
address
=
p
->
address
;
loc_ptr
->
phone_number
=
p
->
phone_number
;
}
if
(
p
->
value
==
"4"
)
{
loc_ptr
->
lastname
=
p
->
NewLastName
;
loc_ptr
->
firstname
=
p
->
NewFirstName
;
loc_ptr
->
phone_number
=
p
->
phone_number
;
}
if
(
p
->
value
==
"5"
)
{
loc_ptr
->
lastname
=
p
->
NewLastName
;
loc_ptr
->
firstname
=
p
->
NewFirstName
;
}
if
(
p
->
value
==
"6"
)
{
loc_ptr
->
address
=
p
->
address
;
}
if
(
p
->
value
==
"7"
)
{
loc_ptr
->
phone_number
=
p
->
phone_number
;
}
}
/*Function Name: BST_Node *Search
Precondition: Searches for client using the string
Postcondition: Search_Aux gets called string gets returned
Description: function does indirect recursion*/
BST_Node
*
Clients_Info_BST
::
Search
(
const
string
&
s
)
{
return
Search_Aux
(
root
,
s
);
}
/*Function Name: BST_Node *Search
Precondition: Searches for client using the string
Postcondition: Search_Aux calls itself and if the string is found
it gets returned.
Description: function does direct recursion*/
BST_Node
*
Clients_Info_BST
::
Search_Aux
(
BST_Node
*
loc_ptr
,
string s
)
{
BST_Node
*
p
=
new
BST_Node
;
int
last
=
s
.
find
(
" "
,
0
);
p
->
lastname
=
s
.
substr
(
0
,
last
);
int
first
=
s
.
find
(
" "
,
last
+
1
);