二叉树 代码

#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;
}

posted @ 2018-02-12 09:23  夜游星  阅读(156)  评论(0编辑  收藏  举报