东北林业锐格实验————二叉树
7080
孩子兄弟表示法.
算叶子结点个数
#include <bits/stdc++.h>
using namespace std;
typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;
void create(tree &t)
{ char ch;
cin>>ch;
if(ch=='@')
t=NULL;
else
{ t=new node;
t->data=ch;
create(t->lchild);
create(t->rchlid);}
}
int leaf(tree &t)
{
if(t==NULL)
return 0;
if (t->lchild == NULL)
{return 1+leaf(t->rchlid);}
else
return leaf(t->lchild)+leaf(t->rchlid);
}
int main()
{ tree t=new node;
create(t);
cout << leaf(t);
return 0;
}
7079
#include <bits/stdc++.h>
using namespace std;
typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;
void create(tree &t)
{ char ch;
cin>>ch;
if(ch=='@')
t=NULL;
else
{ t=new node;
t->data=ch;
create(t->lchild);
create(t->rchlid);}
}
void midout(tree &t)
{
if(t)
{
midout(t->lchild);
cout<<t->data;
midout(t->rchlid);
}
}
int main()
{ tree t=new node;
char data[10000];
create(t);
midout(t);
return 0;
}
7078
和上个题一样,代码直接复制粘贴都可以的
7077
和上面的不一样,重点看一下
#include <bits/stdc++.h>
using namespace std;
#define Maxsize 10000
typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;
void create(tree &t)
{ char ch;
cin>>ch;
if(ch=='@')
t=NULL;
else
{ t=new node;
t->data=ch;
create(t->lchild);
create(t->rchlid);}
}
int main()
{ tree t=new node;
char data[10000];
create(t);
queue<tree> q;
q.push(t);
while(!q.empty())
{
tree temp;
temp=q.front();
q.pop();
cout<<temp->data;
if(temp->lchild)
q.push(temp->lchild);
if(temp->rchlid)
q.push(temp->rchlid);
}
return 0;
}
7076
为什么用之前那个就不行啊,是有什么差别吗,
#include <bits/stdc++.h>
using namespace std;
#define Maxsize 10000
typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;
void create(tree &t)
{ char ch;
cin>>ch;
if(ch=='@')
t=NULL;
else
{ t=new node;
t->data=ch;
create(t->lchild);
create(t->rchlid);}
}
void leafnum(tree &t,int &num)
{
if(t)
{
if (t->lchild==NULL&&t->rchlid==NULL)
num++;
else{
leafnum(t->lchild,num);
leafnum(t->rchlid,num);
}
}
}
int main()
{ tree t=new node;
int num=0;
create(t);
leafnum(t,num);
cout<<num;
return 0;
}
7075
#include <bits/stdc++.h>
using namespace std;
#define Maxsize 10000
typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;
void create(tree &t)
{ char ch;
cin>>ch;
if(ch=='@')
t=NULL;
else
{ t=new node;
t->data=ch;
create(t->lchild);
create(t->rchlid);}
}
void backout(tree &t)
{
if(t)
{ backout(t->lchild);
backout(t->rchlid);
cout<<t->data;
}
}
int main()
{ tree t=new node;
create(t);
backout(t);
return 0;
}
7074
#include <bits/stdc++.h>
using namespace std;
#define Maxsize 10000
typedef struct node{
char data;
node* lchild,* rchlid;
}node,*tree;
void create(tree &t)
{ char ch;
cin>>ch;
if(ch=='@')
t=NULL;
else
{ t=new node;
t->data=ch;
create(t->lchild);
create(t->rchlid);}
}
int depth(tree t)
{
int m=0,n=0;
if(t==NULL)
return 0;
else{
m=depth(t->lchild);
n=depth(t->rchlid);
if(m>n)
return m+1;
else
return n+1;
}
}
int main()
{ tree t=new node;
create(t);
cout<<depth(t);
return 0;
}