c++链式结构二叉树

liupengfeifan 2011-01-09 05:59:01
下面是一个用二叉树排序的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
*/

}
...全文
147 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryfdizuo 2011-01-09
  • 打赏
  • 举报
回复
typedef struct authorNode{
string30 entry;
listADT books;
authorNode*left;
authorNode*right;
};
先遍历结点中的books列表,输出所有books信息:

void displayBooks(treeADT &aTree)
{
bookNodePtr currPtr;
authorNodePtr treePtr;

treePtr = &aTree;
currPtr = treePtr->books;

while(currPtr)
{
cout << currPtr->title << " " << currPtr->noInStock << endl;
currPtr = currPtr->next;
}
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧