数据结构菜单驱动的演示程序.docx
《数据结构菜单驱动的演示程序.docx》由会员分享,可在线阅读,更多相关《数据结构菜单驱动的演示程序.docx(18页珍藏版)》请在冰豆网上搜索。
数据结构菜单驱动的演示程序
数据结构菜单驱动的演示程序(二叉树)
一、Head文件
1.Utility
#pragmaonce
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
usingnamespacestd;
enumError_code{success,fail,overflow,underflow,range_over,not_present};
2.Binary_tree
#pragmaonce
#include"utility.h"
template
structBinary_node
{
//数据成员
Entrydata;
Binary_node*left;
Binary_node*right;
//构造函数
Binary_node()
{
data=0;
left=NULL;
right=NULL;
}
Binary_node(constEntry&x)
{
data=x;
left=NULL;
right=NULL;
}
};
template
classBinary_tree
{
public:
Binary_tree();
boolempty()const;
voidpreorder(void(*visit)(Entry&));
voidinorder(void(*visit)(Entry&));
voidpostorder(void(*visit)(Entry&));
intsize()const;
intleaf_count()const;
voidclear();
intheight()const;
//安全成员函数
Binary_tree(constBinary_tree&original);//拷贝构造函数
Binary_tree&operator=(constBinary_tree&original);//字符重载
~Binary_tree();//析构函数
protected:
Binary_node*root;
//辅助成员函数
voidrecursive_preorder(Binary_node*sub_root,void(*visit)(Entry&));
voidrecursive_inorder(Binary_node*sub_root,void(*visit)(Entry&));
voidrecursive_postorder(Binary_node*sub_root,void(*visit)(Entry&));
intrecursive_size(Binary_node*sub_root)const;
intrecursive_leaf_count(Binary_node*sub_root)const;
voidrecursive_clear(Binary_node*&sub_root);
intrecursive_height(Binary_node*sub_root)const;
Binary_node*recursive_copy(Binary_node*sub_root);
};
//------------------------------------------------------------
template
Binary_tree:
:
Binary_tree()
{
root=NULL;
}
template
boolBinary_tree:
:
empty()const
{
returnroot==NULL;
}
template
voidBinary_tree:
:
preorder(void(*visit)(Entry&))
{
recursive_preorder(root,visit);
return;
}
template
voidBinary_tree:
:
inorder(void(*visit)(Entry&))
{
recursive_inorder(root,visit);
return;
}
template
voidBinary_tree:
:
postorder(void(*visit)(Entry&))
{
recursive_postorder(root,visit);
return;
}
template
intBinary_tree:
:
size()const
{
returnrecursive_size(root);
}
template
intBinary_tree:
:
leaf_count()const
{
returnrecursive_leaf_count(root);
}
template
voidBinary_tree:
:
clear()
{
recursive_clear(root);
return;
}
template
intBinary_tree:
:
height()const
{
returnrecursive_height(root);
}
template
Binary_tree:
:
Binary_tree(constBinary_tree&original)
{
clear();
root=recursive_copy(original.root);
return;
}
template
Binary_tree&Binary_tree:
:
operator=(constBinary_tree&original)
{
Binary_treenew_copy(original);
clear();
root=new_copy.root;
new_copy.root=NULL;
return*this;
}
template
Binary_tree:
:
~Binary_tree()
{
clear();
}
//辅助成员函数---------------------------------------------
template
voidBinary_tree:
:
recursive_preorder(Binary_node*sub_root,void(*visit)(Entry&))
{
if(sub_root!
=NULL)
{
(*visit)(sub_root->data);
recursive_preorder(sub_root->left,visit);
recursive_preorder(sub_root->right,visit);
}
return;
}
template
voidBinary_tree:
:
recursive_inorder(Binary_node*sub_root,void(*visit)(Entry&))
{
if(sub_root!
=NULL)
{
recursive_inorder(sub_root->left,visit);
(*visit)(sub_root->data);
recursive_inorder(sub_root->right,visit);
}
return;
}
template
voidBinary_tree:
:
recursive_postorder(Binary_node*sub_root,void(*visit)(Entry&))
{
if(sub_root!
=NULL)
{
recursive_postorder(sub_root->left,visit);
recursive_postorder(sub_root->right,visit);
(*visit)(sub_root->data);
}
return;
}
template
intBinary_tree:
:
recursive_size(Binary_node*sub_root)const
{
if(sub_root==NULL)return0;
return1+recursive_size(sub_root->left)+recursive_size(sub_root->right);
}
template
intBinary_tree:
:
recursive_leaf_count(Binary_node*sub_root)const
{
if(sub_root==NULL)return0;
if(sub_root->left==NULL&&sub_root->right==NULL)return1;
returnrecursive_leaf_count(sub_root->left)+
recursive_leaf_count(sub_root->right);
}
template
voidBinary_tree:
:
recursive_clear(Binary_node*&sub_root)
{
Binary_node*temp=sub_root;
if(sub_root==NULL)return;
recursive_clear(sub_root->left);
recursive_clear(sub_root->right);
sub_root=NULL;
deletetemp;
return;
}
template
intBinary_tree:
:
recursive_height(Binary_node*sub_root)const
{
if(sub_root==NULL)return-1;
intl=recursive_height(sub_root->left);
intr=recursive_height(sub_root->right);
if(l>r)returnl+1;
elsereturnr+1;
}
template
Binary_node*Binary_tree:
:
recursive_copy(Binary_node*sub_root)
{
if(sub_root==NULL)returnNULL;
Binary_node*temp=newBinary_node(sub_root->data);
temp->left=recursive_copy(sub_root->left);
temp->right=recursive_copy(sub_root->right);
returntemp;
}
3.Search_tree
#pragmaonce
#include"utility.h"
#include"Binary_tree.h"
template
classSearch_tree:
publicBinary_tree
{
public:
Error_codeinsert(constRecord&new_data);
Error_coderemove(constRecord&old_data);
Error_codetree_search(Record&target)const;
private:
//辅助函数
Binary_node*search_for_node(Binary_node*sub_root,constRecord&target)const;//寻找目标节点
Error_codesearch_and_insert(Binary_node*&sub_root,constRecord&new_data);
Error_codesearch_and_destroy(Binary_node*&sub_root,constRecord&target);
Error_coderemove_root(Binary_node*&sub_root);
};
//----------------------------------------------------------------------------------------------------------
template
Error_codeSearch_tree:
:
tree_search(Record&target)const
{
Error_coderesult=success;
Binary_node*found=search_for_node(root,target);
if(found==NULL)
result=not_present;
else
target=found->data;
returnresult;
}
template
Error_codeSearch_tree:
:
insert(constRecord&new_data)
{
returnsearch_and_insert(root,new_data);
}
template
Error_codeSearch_tree:
:
remove(constRecord&target)
{
returnsearch_and_destroy(root,target);
}
//辅助成员函数-----------------------------------------------------------------------------------------------------------
template
Binary_node*Search_tree:
:
search_for_node(Binary_node*sub_root,constRecord&target)const
{
if(sub_root==NULL||sub_root->data==target)
returnsub_root;
elseif(sub_root->datareturnsearch_for_node(sub_root->right,target);
else
returnsearch_for_node(sub_root->left,target);
}
template
Error_codeSearch_tree:
:
search_and_insert(Binary_node*&sub_root,constRecord&new_data)
{
if(sub_root==NULL)
{
sub_root=newBinary_node(new_data);
returnsuccess;
}
elseif(new_datadata)
returnsearch_and_insert(sub_root->left,new_data);
elseif(new_data>sub_root->data)
returnsearch_and_insert(sub_root->right,new_data);
else
returnfail;
}
template
Error_codeSearch_tree:
:
search_and_destroy(Binary_node*&sub_root,constRecord&target)
{
if(sub_root==NULL||sub_root->data==target)
returnremove_root(sub_root);
elseif(targetdata)
returnsearch_and_destroy(sub_root->left,target);
else
returnsearch_and_destroy(sub_root->right,target);
}
template
Error_codeSearch_tree:
:
remove_root(Binary_node*&sub_root)
{
if(sub_root==NULL)returnnot_present;
Binary_node*to_delete=sub_root;
if(sub_root->right==NULL)sub_root=sub_root->left;
elseif(sub_root->left==NULL)sub_root=sub_root->right;
else
{
to_delete=sub_root->left;
Binary_node*parent=sub_root;
while(to_delete->right!
=NULL)
{
parent=to_delete;
to_delete=to_delete->right;
}
sub_root->data=to_delete->data;
if(parent==sub_root)sub_root->left=to_delete->left;
elseparent->right=to_delete->left;
}
deleteto_delete;
returnsuccess;
}
4.Test_tree
#pragmaonce
#include"utility.h"
#include"Search_tree.h"
#include"Binary_tree.h"
voidhelp();
charget_command();
intdo_command(charc,Search_tree&test_tree);
//辅助的输入,输出函数
voidwrite_entry(char&x);
charget_char();
二、Cpp文件
1.Test_tree
#include"test_tree.h"
voidhelp()
{
cout<<"[S]izw[I]nsert[D]elete\n"
<<"[H]eight[C]lear[F]ind[L]eaves\n"
<<"[P]reorderp[O]storderi[N]order[?
]help\n"
<<"[Q]uit\n\n";
}
charget_command()
{
charc,d;
while
(1)
{
do
{
cin.get(c);
}while(c=='\n');//不输入值,则一直等待输入,直到c!
='\n'为止
do
{
cin.get(d);
}while(d!
='\n');//一直到d=='\n'为止
//以上两个输入组合,是输入严格定义在:
一个字符+回车
c=tolower(c);
if(strchr("sidhcflpon?
q",c)!
=NULL)returnc;
//查找字符串s中首次出现字符c的位置,返回首次出现c的位置的指针,如果s中不存在c则返回NULL。
else
cout<<"\nEnteriserror\n"
<<"Pleaseenteravalidcommandor?
forhelp!
!
"<}
}
//对各种不同的输入做不同的处理!
intdo_command(charc,Search_tree&test_tree)
{
charx;
switch(c)
{
case'?
':
help();
break;
case's':
cout<<"Thesizeofthetreeis:
"<break;
case'i':
cout<<"Enternewcharactertoinsert:
";
x=get_char();
test_t