//**************************************************************
// customer_exception.h
// ************************************************************
#ifndef CUSTOMER_EXCEPTION_H_
#define CUSTOMER_EXCEPTION_H_
#include<stdexcept>
#include<string>
class customerException :public std::logic_error
{
public:
customerException(const std::string &message=""):
logic_error(message.c_str())
{}
};
#endif
//************************************************************************ // customer_tree.cpp search binary tree storing customer items //************************************************************************ #include<cassert> #include<cstddef> #include"customer_tree.h" #include"book_tree.h" customerTree::customerTree() { root=NULL; amount=0; } customerTree::customerTree(const customerItem &_item) { root=new customerNode(_item,NULL,NULL); assert(root!=NULL); amount=1; } customerTree::~customerTree() { destory(root); } bool customerTree::isEmpty()const { return (root==NULL); } void customerTree::insert(const customerItem &newitem)throw(customerException) { try { insertItem(root,newitem); } catch(customerException &cExcept) { throw; } } void customerTree::remove(const string &_ID)throw(customerException) { try { removeItem(root,_ID); } catch(customerException &cexcept) { throw; } } customerItem &customerTree::search(const string &_ID)throw(customerException) { try{ return retrieveTree(root,_ID); } catch(customerException &cexcept) { throw; } } void customerTree::traverse(FunctionType visit) { traverseItem(root,visit); } void customerTree::read(ifstream &fin,int n,bookTree &_tree) { readItem(root,fin,n,_tree); } void customerTree::write(ofstream &fout) { writeItem(root,fout); } void customerTree::writeItem(customerNode *&treePtr,ofstream &fout) { if(treePtr!=NULL) { writeItem(treePtr->leftchild,fout); fout<<treePtr->item; writeItem(treePtr->rightchild,fout); } } void customerTree::readItem(customerNode *& treePtr,ifstream &fin,int n,bookTree &_tree) { if(n>0) { treePtr=new customerNode(); readItem(treePtr->leftchild,fin,n/2,_tree); treePtr->item.readCitem(fin,_tree); amount++; readItem(treePtr->rightchild,fin,(n-1)/2,_tree); } } void customerTree::insertItem(customerNode *&treePtr,const customerItem &newitem)throw(customerException) { if(treePtr==NULL) { treePtr= new customerNode(newitem,NULL,NULL); if(treePtr==NULL) throw customerException("customerException :allocate memory failed in insert function !\a\a\a\a\a"); amount++; } else if(treePtr->item.getID()>newitem.getID()) insertItem(treePtr->leftchild,newitem); else insertItem(treePtr->rightchild,newitem); } void customerTree::removeItem(customerNode *&treePtr,const string &_ID)throw(customerException) { if(treePtr==NULL) throw customerException("customerException :delete failed in remove function !\a\a\a\a\a"); else if(treePtr->item.getID()>_ID) removeItem(treePtr->leftchild,_ID); else if(treePtr->item.getID()==_ID) { removeNode(treePtr); amount--; } else removeItem(treePtr->rightchild,_ID); } void customerTree::removeNode(customerNode *&treePtr) { customerNode *delPtr; customerItem tempItem; if(treePtr->leftchild==NULL &&treePtr->rightchild==NULL) { delete treePtr; treePtr=NULL; } else if(treePtr->leftchild==NULL) { delPtr=treePtr; treePtr=treePtr->rightchild; delPtr->rightchild=NULL; delete delPtr; } else if(treePtr->rightchild==NULL) { delPtr=treePtr; treePtr=treePtr->leftchild; delPtr->leftchild=NULL; delete delPtr; } else { processLeft(treePtr->rightchild,tempItem); treePtr->item=tempItem; } } void customerTree::processLeft(customerNode *&treePtr,customerItem &_item) { if(treePtr->leftchild==NULL) { _item=treePtr->item; customerNode *delPtr=treePtr; treePtr=treePtr->rightchild; delPtr->rightchild=NULL; delete delPtr; } else processLeft(treePtr->leftchild,_item); } void customerTree::destory(customerNode *&treePtr) { if(treePtr!=NULL) { destory(treePtr->leftchild); destory(treePtr->rightchild); delete treePtr; treePtr=NULL; } } customerItem &customerTree::retrieveTree(customerNode *&treePtr,const string &_ID) throw(customerException) { if(treePtr==NULL) throw customerException("customerException :seek the key customer failed in search function !\a\a\a\a\a"); else if(treePtr->item.getID()>_ID) retrieveTree(treePtr->leftchild,_ID); else if(treePtr->item.getID()==_ID) return treePtr->item; else retrieveTree(treePtr->rightchild,_ID); } void customerTree::traverseItem(customerNode *&treePtr,FunctionType visit) { if(treePtr!=NULL) { traverseItem(treePtr->leftchild,visit); visit(treePtr->item); traverseItem(treePtr->rightchild,visit); } }
//**********************************************************
// book_tree.h implemention file---book_tree.cpp
//**********************************************************
#ifndef BOOK_TREE_H_
#define BOOK_TREE_H_
#include<fstream>
#include<iostream>
#include<cstddef>
#include<string>
#include"book_node.h"
#include"book_exception.h"
using namespace std;
typedef void (*Function)(const bookItem &_item);
class bookTree
{
private:
bookNode *root;
int amount;
public:
bookTree();
bookTree(const bookItem &_item);
~bookTree();
bool isEmpty()const;
int getAmount()const{return amount;}
void insert(const bookItem &newitem)throw(bookException);
void remove(const string &_ISBN)throw(bookException);
void search(const string &_key,char & _type)const;
bookItem & search(const string &_ISBN)throw(bookException);
void traverse(Function visit);
void write(ofstream &fout);
void read(ifstream &fin,int n);
protected:
void insertItem(bookNode*& treePtr,const bookItem &newItem)throw(bookException);
void removeItem(bookNode *& treePtr,const string &_ISBN)throw(bookException);
void removeNode(bookNode*& treePtr);
void processLeft(bookNode*& treePtr,bookItem &_item);
void destory(bookNode * &_treePtr);
void retrieveInorde(bookNode *const & treePtr,const string &_key,char &_type)const;
bookItem & retrieveTree(bookNode * & treePtr,const string &_ISBN)throw(bookException);
void traverseItem(bookNode * & treePtr,Function visit);
void writeItem(bookNode * & treePtr,ofstream &fout);
void readItem(bookNode *& treePtr,ifstream &fin,int n);
};
#endif
//**************************************************************
// book_tree.cpp search binary tree storing book items //**************************************************************
#include<cassert>
#include<cstdlib>
#include<cstddef>
#include"book_tree.h"
bookTree::bookTree()
{
root=NULL;
amount=0;
}
bookTree::bookTree(const bookItem &_item)
{
root=new bookNode(_item,NULL,NULL);
assert(root!=NULL);
amount=1;
}
bookTree::~bookTree()
{
destory(root);
}
bool bookTree::isEmpty()const
{
return (root==NULL);
}
void bookTree::insert(const bookItem &newitem)throw(bookException)
{
try{
insertItem(root,newitem);
}catch(bookException &bexvept)
{
throw;
}
}
void bookTree::remove(const string &_ISBN)throw(bookException)
{
try
{
removeItem(root,_ISBN);
}
catch(bookException &bexcept)
{
throw;
}
}
void bookTree::search(const string &_key,char & _type)const
{
retrieveInorde(root,_key,_type);
}
bookItem & bookTree::search(const string &_ISBN)throw(bookException)
{
try{
return(retrieveTree(root,_ISBN));
}
catch(bookException &cexcept)
{
throw;
}
}
void bookTree::traverse(Function visit)
{
traverseItem(root,visit);
}
void bookTree::write(ofstream &fout)
{
writeItem(root,fout);
}
void bookTree::read(ifstream &fin,int n)
{
readItem(root,fin,n);
}
void bookTree::insertItem(bookNode *&treePtr,const bookItem &newitem)
throw(bookException)
{
if(treePtr==NULL)
{
treePtr=new bookNode(newitem,NULL,NULL);
if(treePtr==NULL)
throw bookException("bookException :can't allocate memory in insert function !\a\a\a\a\a");
amount++;
}
else if((treePtr->item.getISBN())>newitem.getISBN())
insertItem(treePtr->leftchild,newitem);
else
insertItem(treePtr->rightchild,newitem);
}
void bookTree::removeItem(bookNode *& treePtr,const string &_ISBN)throw(bookException)
{
if(treePtr==NULL)
throw bookException("bookException :delete the key book failed in remove function !\a\a\a\a\a");
else if(treePtr->item.getISBN()>_ISBN)
removeItem(treePtr->leftchild,_ISBN);
else if(treePtr->item.getISBN()==_ISBN)
{
removeNode(treePtr);
amount--;
}
else
removeItem(treePtr->rightchild,_ISBN);
}
void bookTree::removeNode(bookNode *& treePtr)
{
bookNode *delPtr;
bookItem tempItem;
if(treePtr->leftchild==NULL&&treePtr->rightchild==NULL)
{
delete treePtr;
treePtr=NULL;
}
else if(treePtr->leftchild==NULL)
{
delPtr=treePtr;
treePtr=treePtr->rightchild;
delPtr->rightchild=NULL;
delete delPtr;
}
else if(treePtr->rightchild==NULL)
{
delPtr=treePtr;
treePtr=treePtr->leftchild;
delPtr->leftchild=NULL;
delete delPtr;
}
else
{
processLeft(treePtr->rightchild,tempItem);
treePtr->item=tempItem;
}
}
void bookTree::processLeft(bookNode *&treePtr,bookItem &_item)
{
if(treePtr->leftchild==NULL)
{
_item=treePtr->item;
bookNode *tempPtr=treePtr;
treePtr=treePtr->rightchild;
tempPtr->rightchild=NULL;
delete tempPtr;
}
else
processLeft(treePtr->leftchild,_item);
}
void bookTree::destory(bookNode *&_treePtr)
{
if(_treePtr!=NULL)
{
destory(_treePtr->leftchild);
destory(_treePtr->rightchild);
delete _treePtr;
_treePtr=NULL;
amount--;
}
}
void bookTree::retrieveInorde(bookNode *const & treePtr,const string &_key,char & _type)const
{
if(treePtr!=NULL)
{
if(_type=='T'||_type=='t')
{
retrieveInorde(treePtr->leftchild,_key,_type);
if(treePtr->item.getTitle()==_key)
{
treePtr->item.display();
}
retrieveInorde(treePtr->rightchild,_key,_type);
}
else if(_type=='A'||_type=='a')
{
retrieveInorde(treePtr->leftchild,_key,_type);
if(treePtr->item.getAuthor()==_key)
{
treePtr->item.display();
}
retrieveInorde(treePtr->rightchild,_key,_type);
}
else if(_type=='I'||_type=='i')
{
retrieveInorde(treePtr->leftchild,_key,_type);
if(treePtr->item.getISBN()==_key)
{
treePtr->item.display();
}
retrieveInorde(treePtr->rightchild,_key,_type);
}
else if(_type=='P'||_type=='p')
{
retrieveInorde(treePtr->leftchild,_key,_type);
if(treePtr->item.getPublisher()==_key)
{
treePtr->item.display();
}
retrieveInorde(treePtr->rightchild,_key,_type);
}
else if(_type=='C'||_type=='c')
{
retrieveInorde(treePtr->leftchild,_key,_type);
if(treePtr->item.getCategory()==_key)
{
treePtr->item.display();
}
retrieveInorde(treePtr->rightchild,_key,_type);
}
}
}
bookItem &bookTree::retrieveTree(bookNode *&treePtr,const string &_ISBN)
throw(bookException)
{
if(treePtr==NULL)
throw bookException("bookException :seek the key book failed in search function !\a\a\a\a\a");
else if(treePtr->item.getISBN()==_ISBN)
return treePtr->item;
else if(treePtr->item.getISBN()>_ISBN)
retrieveTree(treePtr->leftchild,_ISBN);
else
retrieveTree(treePtr->rightchild,_ISBN);
}
void bookTree::traverseItem(bookNode *&treePtr,Function visit)
{
if(treePtr!=NULL)
{
traverseItem(treePtr->leftchild,visit);
visit(treePtr->item);
traverseItem(treePtr->rightchild,visit);
}
}
void bookTree::writeItem(bookNode *&treePtr,ofstream &fout)
{
if(treePtr!=NULL)
{
writeItem(treePtr->leftchild,fout);
fout<<treePtr->item;
writeItem(treePtr->rightchild,fout);
}
}
void bookTree::readItem(bookNode *&treePtr ,ifstream &fin,int n)
{
if(n>0)
{
treePtr=new bookNode();
readItem(treePtr->leftchild,fin,n/2);
fin>>treePtr->item;
amount++;
readItem(treePtr->rightchild,fin,(n-1)/2);
}
}
//************************************************************************ // customer_tree.cpp search binary tree storing customer items //************************************************************************ #include<cassert> #include<cstddef> #include"customer_tree.h" #include"book_tree.h" customerTree::customerTree() { root=NULL; amount=0; } customerTree::customerTree(const customerItem &_item) { root=new customerNode(_item,NULL,NULL); assert(root!=NULL); amount=1; } customerTree::~customerTree() { destory(root); } bool customerTree::isEmpty()const { return (root==NULL); } void customerTree::insert(const customerItem &newitem)throw(customerException) { try { insertItem(root,newitem); } catch(customerException &cExcept) { throw; } } void customerTree::remove(const string &_ID)throw(customerException) { try { removeItem(root,_ID); } catch(customerException &cexcept) { throw; } } customerItem &customerTree::search(const string &_ID)throw(customerException) { try{ return retrieveTree(root,_ID); } catch(customerException &cexcept) { throw; } } void customerTree::traverse(FunctionType visit) { traverseItem(root,visit); } void customerTree::read(ifstream &fin,int n,bookTree &_tree) { readItem(root,fin,n,_tree); } void customerTree::write(ofstream &fout) { writeItem(root,fout); } void customerTree::writeItem(customerNode *&treePtr,ofstream &fout) { if(treePtr!=NULL) { writeItem(treePtr->leftchild,fout); fout<<treePtr->item; writeItem(treePtr->rightchild,fout); } } void customerTree::readItem(customerNode *& treePtr,ifstream &fin,int n,bookTree &_tree) { if(n>0) { treePtr=new customerNode(); readItem(treePtr->leftchild,fin,n/2,_tree); treePtr->item.readCitem(fin,_tree); amount++; readItem(treePtr->rightchild,fin,(n-1)/2,_tree); } } void customerTree::insertItem(customerNode *&treePtr,const customerItem &newitem)throw(customerException) { if(treePtr==NULL) { treePtr= new customerNode(newitem,NULL,NULL); if(treePtr==NULL) throw customerException("customerException :allocate memory failed in insert function !\a\a\a\a\a"); amount++; } else if(treePtr->item.getID()>newitem.getID()) insertItem(treePtr->leftchild,newitem); else insertItem(treePtr->rightchild,newitem); } void customerTree::removeItem(customerNode *&treePtr,const string &_ID)throw(customerException) { if(treePtr==NULL) throw customerException("customerException :delete failed in remove function !\a\a\a\a\a"); else if(treePtr->item.getID()>_ID) removeItem(treePtr->leftchild,_ID); else if(treePtr->item.getID()==_ID) { removeNode(treePtr); amount--; } else removeItem(treePtr->rightchild,_ID); } void customerTree::removeNode(customerNode *&treePtr) { customerNode *delPtr; customerItem tempItem; if(treePtr->leftchild==NULL &&treePtr->rightchild==NULL) { delete treePtr; treePtr=NULL; } else if(treePtr->leftchild==NULL) { delPtr=treePtr; treePtr=treePtr->rightchild; delPtr->rightchild=NULL; delete delPtr; } else if(treePtr->rightchild==NULL) { delPtr=treePtr; treePtr=treePtr->leftchild; delPtr->leftchild=NULL; delete delPtr; } else { processLeft(treePtr->rightchild,tempItem); treePtr->item=tempItem; } } void customerTree::processLeft(customerNode *&treePtr,customerItem &_item) { if(treePtr->leftchild==NULL) { _item=treePtr->item; customerNode *delPtr=treePtr; treePtr=treePtr->rightchild; delPtr->rightchild=NULL; delete delPtr; } else processLeft(treePtr->leftchild,_item); } void customerTree::destory(customerNode *&treePtr) { if(treePtr!=NULL) { destory(treePtr->leftchild); destory(treePtr->rightchild); delete treePtr; treePtr=NULL; } } customerItem &customerTree::retrieveTree(customerNode *&treePtr,const string &_ID) throw(customerException) { if(treePtr==NULL) throw customerException("customerException :seek the key customer failed in search function !\a\a\a\a\a"); else if(treePtr->item.getID()>_ID) retrieveTree(treePtr->leftchild,_ID); else if(treePtr->item.getID()==_ID) return treePtr->item; else retrieveTree(treePtr->rightchild,_ID); } void customerTree::traverseItem(customerNode *&treePtr,FunctionType visit) { if(treePtr!=NULL) { traverseItem(treePtr->leftchild,visit); visit(treePtr->item); traverseItem(treePtr->rightchild,visit); } }