第五章

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
template
struct BiNode
{
T data;
BiNode *lchild, *rchild;
};
template class BiTree { public: BiTree( ){root=NULL;} BiTree
(BiNode *root
);
~BiTree( );
void PreOrder
(BiNode root
);
void InOrder
(BiNode root
);
void PostOrder
(BiNode
template
void BiTree::PreOrder(BiNode root)
{
if (root NULL) return;
else {
cout<data;
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
template
void BiTree::InOrder (BiNode *root)
{
if (root
NULL) return;
else {
InOrder(root->lchild);
cout<data;
InOrder(root->rchild);
}
}
template
void BiTree::PostOrder(BiNode root)
{
if (root==NULL) return;
else {
PostOrder(root->lchild);
PostOrder(root->rchild);
cout<data;
}
}
template
void BiTree::LevelOrder(BiNode root)
{const int MaxSize = 100;
int front = 0;
int rear = 0; //采用顺序队列,并假定不会发生上溢
BiNode
Q[MaxSize];
BiNode
q;
if (rootNULL) return;
template
BiTree ::BiTree
(BiNode *root
)
{
Creat
(root);
}
template
void BiTree ::Creat
(BiNode *root
)
{
cin>>ch;
if
(ch
’# ’
) root=NULL;
else {
root=new BiN
template
BiTree ::BiTree()
{
root =Creat();
}
template
BiNode
BiTree ::Creat()
{ BiNode
root;
T ch;
cin>>ch;
if
(ch==’# ’
) root=NULL;
else {
root=new BiNode;
root->data=ch;
root-> lchild=Creat();
root-> rchild=Cre
template
BiTree::~BiTree(void)
{
Release(root);
}
template void BiTree::Release(BiNode
root)
{
if (root != NULL){
Release(root->lchild); //释放左子树
Release(root->rchild); //释放右子树
delete root;
}
}
void InOrder (BiNode *root)
{
if (rootNULL) return;
else {
InOrder(root->lchild);
cout<data;
InOrder(root->rchild);
}
}
int n=0;
template
void Count(BiNode *root) //n为全局量并已初始化为0
{
if (root) {
Count(root->lchild);
n++;
Count(root->rchild);
}
}
template
void PreOrderleaf(BiNode *root)
{
if (root) {
if (!root->lchild && !root->rchild)
cout<data;
PreOrderleaf(root->lchild);
PreOrderleaf(root->rchild);
}
}
template
int Depth(BiNode *root)
{
if (root
NULL) return 0;
else {
int hl=Depth(root->lchild);
int hr=Depth(root ->rchild);
return hl>hr?hl+1:hr+1;
}
}
template
TNode *Search(TNode *root, T x, int i)
{
if (root->data= =x) {
j=1;
p=root->firstchild;
while (p!=NULL && j<i)
{
j++;
p=p->rightsib;
}
if § return p;
else return NULL;
}
Search(
template
struct TNode
{
T data;
TNode *firstchild, *rightsib;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值