数据结构菜单驱动的演示程序.docx
- 文档编号:30189654
- 上传时间:2023-08-05
- 格式:DOCX
- 页数:18
- 大小:16.97KB
数据结构菜单驱动的演示程序.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
Binary_node
//构造函数
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
Binary_tree
~Binary_tree();//析构函数
protected:
Binary_node
//辅助成员函数
voidrecursive_preorder(Binary_node
voidrecursive_inorder(Binary_node
voidrecursive_postorder(Binary_node
intrecursive_size(Binary_node
intrecursive_leaf_count(Binary_node
voidrecursive_clear(Binary_node
intrecursive_height(Binary_node
Binary_node
};
//------------------------------------------------------------
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
{
clear();
root=recursive_copy(original.root);
return;
}
template
Binary_tree
:
operator=(constBinary_tree
{
Binary_tree
clear();
root=new_copy.root;
new_copy.root=NULL;
return*this;
}
template
Binary_tree
:
~Binary_tree()
{
clear();
}
//辅助成员函数---------------------------------------------
template
voidBinary_tree
:
recursive_preorder(Binary_node
{
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
{
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
{
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
{
if(sub_root==NULL)return0;
return1+recursive_size(sub_root->left)+recursive_size(sub_root->right);
}
template
intBinary_tree
:
recursive_leaf_count(Binary_node
{
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
{
Binary_node
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
{
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
:
recursive_copy(Binary_node
{
if(sub_root==NULL)returnNULL;
Binary_node
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
Error_codesearch_and_insert(Binary_node
Error_codesearch_and_destroy(Binary_node
Error_coderemove_root(Binary_node
};
//----------------------------------------------------------------------------------------------------------
template
Error_codeSearch_tree
:
tree_search(Record&target)const
{
Error_coderesult=success;
Binary_node
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_for_node(Binary_node
{
if(sub_root==NULL||sub_root->data==target)
returnsub_root;
elseif(sub_root->data returnsearch_for_node(sub_root->right,target); else returnsearch_for_node(sub_root->left,target); } template Error_codeSearch_tree : search_and_insert(Binary_node { if(sub_root==NULL) { sub_root=newBinary_node returnsuccess; } elseif(new_data 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 { if(sub_root==NULL||sub_root->data==target) returnremove_root(sub_root); elseif(target returnsearch_and_destroy(sub_root->left,target); else returnsearch_and_destroy(sub_root->right,target); } template Error_codeSearch_tree : remove_root(Binary_node { if(sub_root==NULL)returnnot_present; Binary_node 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 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 //辅助的输入,输出函数 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 { charx; switch(c) { case'? ': help(); break; case's': cout<<"Thesizeofthetreeis: "< break; case'i': cout<<"Enternewcharactertoinsert: "; x=get_char(); test_t
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 菜单 驱动 演示 程序