c++链式结构二叉树
下面是一个用二叉树排序的c++程序,关于对,书名和作者的排序删除和显示,最后一个“void displayBooks(treeADT &aTree)”没有实现,那位强人帮忙写一下,并帮忙改一下程序,其他地方还有错误
typedef char string30[31];
typedef struct bookNode{
string30 title;
int noInStock;
bookNode*next;
};
typedef struct bookNode *bookNodePtr;
struct listADT{
bookNodePtr head;
};
typedef struct authorNode{
string30 entry;
listADT books;
authorNode*left;
authorNode*right;
};
typedef struct authorNode*authorNodePtr;
struct treeADT{
authorNodePtr root;
};
#include <stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<malloc.h>
#include<iostream>
using namespace std;
void displayMenu();
void initialiseControl(treeADT&);
void addControl(treeADT &);
void deleteControl(treeADT &);
void inOrder (authorNodePtr);
void addBookControl (treeADT &);
void deleteBookControl (treeADT &);
void displayBooks (treeADT &);
void main()
{
treeADT oneTree;
char choice;
oneTree.root = NULL;
do{
displayMenu();
cout<<"Make your selection > ";
choice = toupper(getch());
switch(choice){
case'1': initialiseControl(oneTree);
break;
case'2': addControl(oneTree);
break;
case'3': deleteControl(oneTree);
break;
case'4': inOrder(oneTree.root);
break;
case'5': addBookControl(oneTree);
break;
case'6': deleteBookControl(oneTree);
break;
case'7': displayBooks(oneTree);
break;
}
}while(choice !='Q');
}
void displayMenu()
{
cout<<"\n\t\tSoftware Develpoment: Linked Data Structure.";
cout<<"\n\t\t Assessment Task 5 - application";
cout<<"\n\n\t\t\t1. Initialise tree.";
cout<<"\n\t\t\t2. Add author.";
cout<<"\n\t\t\t3. Delete author.";
cout<<"\n\t\t\t4. Display authors.";
cout<<"\n\t\t\t5. Add book.";
cout<<"\n\t\t\t6. Delete books.";
cout<<"\n\t\t\t7. Display books.";
cout<<"\n\t\t\tQ. QUIT.";
}
void initialiseControl(treeADT& aTree)
{
if(aTree.root==NULL)
cout<<"Initialise success!"<<endl;
else
if(aTree.root->left==NULL&&aTree.root->right==NULL)
delete aTree.root;
else
{
if(aTree.root->left!=NULL)
initialiseControl(aTree.root->left);
if(aTree.root->right!=NULL)
initialise(aTree->right);
}
}
void addControl(treeADT &aTree)
{
authorNodePtr tempPtr;
cout<<"\n\n\t\tEnter a name > ";
scanf ("%s",newValue);
if (isEmpty(aTree))
{
addControl(newValue,aTree.root);
}
else
{
addToTree(newValue,aTree.root);
}
}
void deleteControl(treeADT &aTree)
{
string30 searchValue;
if (isEmpty(aTree))
{
cout<<"*** empty tree ***";
system("pause");
}
else
{
cout<<"\n\t\tEnter the node to be deleted > ";
fflush(stdin);
gets(searchValue);
searchDelete(searchValue,aTree.root);
}
}
void inOrder(authorNodePtr treePtr)
{
if ( treeptr != 0 )
{
inOrder( treeptr->left);
cout<<treeptr->entry<< endl;
inOrder( treeptr->right );
}
}
void addBookControl (treeADT &aTree)
{
bookNodePtr tempPtr;
authorNodePtr treePtr;
if(treeptr==0)
{
treeptr=new(node);
strcpy(treeptr->entry,tempPtr);
treeptr->left=NULL;
treeptr->right=NULL;
}
else if((strcmp(tempPtr,treeptr->entry))<0)
{
addToTree(tempPtr,treeptr->left);
}
else if(strcmp(tempPtr,treeptr->entry)>0)
{
addToTree(tempPtr,treeptr->right);
}
else
{
cout<<tempPtr<<" dup" << endl;
}
}
void deleteBookControl (treeADT &aTree)
{
bookNodePtr tempPtr;
authorNodePtr treePtr;
int found;
string30 searchVal;
if(strcmp(treeptr->entry,searchVal)==0)
if(treeptr->left==NULL&&treeptr->right==NULL)
{
treeptr=NULL;
}
else if(treeptr->left== NULL)
{
tempPtr=treeptr;
treeptr=treeptr->right;
}
else if(treeptr->right==NULL)
{
tempPtr=treeptr;
treeptr=treeptr->left;
}
else
{
tempPtr=treeptr->right;
}
while(tempPtr->left!=NULL)
{
tempPtr=tempPtr->left;
}
tempPtr->left=treeptr->left;
treeptr=tempPtr;
else if(strcmp(treeptr->entry,searchVal)>0)
deleteBookControl(searchVal,treeptr->left);
else searchDelete(searchVal,treeptr->right);
}
void displayBooks(treeADT &aTree)
{
bookNodePtr currPtr;
authorNodePtr treePtr;
/*
set tree pointer to call getAuthor
If tree pointer <> Null then
if empty list then
display "empty list" message
else
set current pointer to list head pointer
while current pointer <> Null Do
Display book title and stock
set current pointer to current node next pointer
endwhile
endif
endif
*/
}