首页 > 其他 > 详细

二叉树 代码

时间:2018-02-12 10:52:25      阅读:307      评论:0      收藏:0      [点我收藏+]

#include<bits_stdc++.h>
using namespace std;
typedef struct btnode
{
char data;
btnode *lc,*rc;
}bitn,*btree;
void creattree(btree &t)
{
char c;
cin>>c;
if(c==‘#‘)
t=NULL;
else
{
t=(btree)malloc(sizeof(bitn));
t->data=c;
creattree(t->lc);
creattree(t->rc);
}
}
int isempty(btree t)
{
if(t)
return 0;
else
return 1;
}
int depth(btree t)
{
int i,j;
if(!t)
return 0;
if(t->lc)
i=depth(t->lc);
else
i=0;
if(t->rc)
j=depth(t->rc);
else
j=0;
return i>j?i+1:j+1;
}
void preorder(btree t)
{
if(!t)
return;
cout<<t->data<<endl;
preorder(t->lc);
preorder(t->rc);
}
int main()
{
btree t;
creattree(t);
int d;
d=depth(t);
cout<<"depth is "<<d<<endl;
preorder(t);
return 0;
}

二叉树 代码

原文:https://www.cnblogs.com/wander-clouds/p/8443708.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!