数据结构树的基本操作_树的各类基本操作(数据结构)

#include "iostream"

/*

输入:ABC DE G F

ABC DE G F

*/

#include "bits/stdc++.h"

using namespace std;

typedef struct bitnode

{

char data;

bitnode *lchild,*rchild;

} *bintree;

bintree creatree(bintree &root)//先序创建树

{

char a=getchar();

if(a==' ')

{

root=NULL;

return root;

}

else

{

root=(bintree)malloc(sizeof(bitnode));

root->data=a;

creatree(root->lchild);

creatree(root->rchild);

return root;

}

}

void PreOrderTraverse(bintree root)//InOrderTraverse中序 PostOrderTraverse 倒序

{

if(root==NULL)

{

return ;

}

else

{

PreOrderTraverse(root->lchild);

PreOrderTraverse(root->rchild);

cout<data<

}

}

int CountDegreeOne(bintree root)//度为一节点的个数

{

if(root==NULL)

return 0;

else

{

int a=CountDegreeOne(root->lchild);

int b=CountDegreeOne(root->rchild);

if(root->lchild&&!root->rchild||root->rchild&&!root->lchild)

return a+b+1;

else

return a+b;

}

}

int DeepTree(bintree root)//树的深度

{

if(root==NULL)

return 0;

else

{

int a=DeepTree(root->lchild);

int b=DeepTree(root->rchild);

if(a>=b)

return a+1;

else

return b+1;

}

}

int same(bintree root,bintree root1)//判断两棵树是否相同

{

if(root&&!root1||root1&&!root||!root&&!root1)

{

return 0;

}

else

{

if(root->data==root1->data)

{

int a=same(root->lchild,root1->lchild);

int b=same(root->rchild,root1->rchild);

if(a==b==1)

return 1;

}

else

return 0;

}

}

int main()

{

bintree root;

bintree root1;

creatree(root);

getchar();

creatree(root1);

PreOrderTraverse(root);

cout<

cout<

if(same(root,root1))

cout<

else

cout<

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值