使用前序+左右孩子标志#的方式输入;
输出依次为先序遍历、中序遍历、后序遍历、层次遍历、树的深度、树是否为完全二叉树。
#include
#include
using namespace std;
#define maxn 2000
#define error 1
#define ok 0
#define no 1
typedef int status;
char str[2000];
typedef struct node
{
char val;
node *lson, *rson;
}node,*lnode;//链式树
typedef lnode elem;
typedef struct qnode
{
elem data;
struct qnode *next;
}qnode,*queueptr;//链式队列
typedef struct
{
queueptr front;
queueptr rear;
}linkqueue;//链式队列的头和尾节点
status init_queue(linkqueue &q)
{
q.front=q.rear=(queueptr)malloc(sizeof(qnode));
q.rear->next=NULL;
return ok;
}
status push_queue(linkqueue &q,elem e)
{
q.rear->data=e;
queueptr s;
s=(queueptr)malloc(sizeof(qnode));
q.rear->next=s;
q.rear=s;
q.rear->next=NULL;
return ok;
}
queueptr pop_queue(linkqueue &q)
{
queueptr p=q.front;
queueptr t;
t=q.front->next;
q.front=t;
return p;
}
void levelorder(lnode node)
{
linkqueue q;
init_queue(q);
if(node!=NULL)
{
push_queue(q,node);
while(q.front!=q.rear)
{
queueptr p;
p=pop_queue(q);
printf("%c",p->data->val);
if(p->data->lson!=NULL)
push_queue(q,p->data->lson);
if(p->data->rson!=NULL)
push_queue(q,p->data->rson);
}
}
}//层次遍历
int complete(lnode T)
{
if(T==NULL)
return ok;
linkqueue q;
init_queue(q);
push_queue(q,T);
queueptr p;
while(q.front!=q.rear)
{
p=pop_queue(q);
if(p->data)
{
push_queue(q,p->data->lson);
push_queue(q,p->data->rson);
}
else
{
while(q.front!=q.rear)
{
p=pop_queue(q);
if(p->data)
return no;
}
}//找到一个空结点之后判断后面是否是非空元素,如果是,一定不是完全二叉树
}
return ok;
}
void create1(lnode* T,string s1,string s2)
{
*T = (lnode)calloc(1,sizeof(node));
(*T)->val=s1[0];
if(s1.size() == 1)
{
return;
}
int index;
for(index = 0; index < s2.size(); index++)
{
if(s2[index] == s1[0])
break;
}
int left_length = index;
int right_length = s1.size() - left_length - 1;
if(left_length > 0)
{
create1(&(*T)->lson, s1.substr(1, left_length), s2.substr(0, left_length));
}
if(right_length > 0)
{
create1(&(*T)->rson, s1.substr(1 + left_length, right_length), s2.substr(1 + left_length, right_length));
}
}//已知前序和中序建立二叉树
void create2(lnode &T,int &i,int n)
{
if(i==n)
return;
if(str[i]=='#')
{
T=NULL;
i++;
}
else
{
T=new node;
T->val=str[i];
i++;
create2(T->lson,i,n);
create2(T->rson,i,n);
}
}//已知先序遍历序建立二叉树
void preorder(lnode T)
{
if(T!=NULL)
{
printf("%c",T->val);
preorder(T->lson);
preorder(T->rson);
}
}//先序遍历
void inorder(lnode T)
{
if(T!=NULL)
{
inorder(T->lson);
printf("%c",T->val);
inorder(T->rson);
}
}//中序遍历
void postorder(lnode T)
{
if(T!=NULL)
{
postorder(T->lson);
postorder(T->rson);
printf("%c",T->val);
}
}//后序遍历
int getdepth(lnode T)
{
int ld,rd;
if(T==NULL)
return 0;
else
{
ld=getdepth(T->lson);
rd=getdepth(T->rson);
if(ld>rd)
return (ld+1);
else
return (rd+1);
}
}//求二叉树的深度
int main()
{
int depth;
while(scanf("%s",str)!=EOF)
{
lnode T=NULL;
int i=0;
create2(T,i,strlen(str));
/*printf("preorder:");
preorder(T);
printf("\ninorder:");
inorder(T);
printf("\npostorder:");
postorder(T);
printf("\nlevelorder:");
levelorder(T);
printf("\ndepth:");
depth=getdepth(T);
printf("%d\ncomplete:",depth);*/
if(complete(T)==ok)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
已知前序中序二叉树的建立
输入为二叉树的先序和中序遍历;
输出为二叉树的后序遍历。
#include
#include
using namespace std;
#define maxn 2000
#define error 1
#define ok 0
#define no 1
typedef int status;
char str[2000];
typedef struct node
{
char val;
node *lson, *rson;
}node,*lnode;//链式树
typedef lnode elem;
typedef struct qnode
{
elem data;
struct qnode *next;
}qnode,*queueptr;//链式队列
typedef struct
{
queueptr front;
queueptr rear;
}linkqueue;//链式队列的头和尾节点
status init_queue(linkqueue &q)
{
q.front=q.rear=(queueptr)malloc(sizeof(qnode));
q.rear->next=NULL;
return ok;
}
status push_queue(linkqueue &q,elem e)
{
q.rear->data=e;
queueptr s;
s=(queueptr)malloc(sizeof(qnode));
q.rear->next=s;
q.rear=s;
q.rear->next=NULL;
return ok;
}
queueptr pop_queue(linkqueue &q)
{
queueptr p=q.front;
queueptr t;
t=q.front->next;
q.front=t;
return p;
}
void levelorder(lnode node)
{
linkqueue q;
init_queue(q);
if(node!=NULL)
{
push_queue(q,node);
while(q.front!=q.rear)
{
queueptr p;
p=pop_queue(q);
printf("%c",p->data->val);
if(p->data->lson!=NULL)
push_queue(q,p->data->lson);
if(p->data->rson!=NULL)
push_queue(q,p->data->rson);
}
}
}//层次遍历
int complete(lnode T)
{
if(T==NULL)
return ok;
linkqueue q;
init_queue(q);
push_queue(q,T);
queueptr p;
while(q.front!=q.rear)
{
p=pop_queue(q);
if(p->data)
{
push_queue(q,p->data->lson);
push_queue(q,p->data->rson);
}
else
{
while(q.front!=q.rear)
{
p=pop_queue(q);
if(p->data)
return no;
}
}//找到一个空结点之后判断后面是否是非空元素,如果是,一定不是完全二叉树
}
return ok;
}
void create1(lnode* T,string s1,string s2)
{
*T = (lnode)calloc(1,sizeof(node));
(*T)->val=s1[0];
if(s1.size() == 1)
{
return;
}
int index;
for(index = 0; index < s2.size(); index++)
{
if(s2[index] == s1[0])
break;
}
int left_length = index;
int right_length = s1.size() - left_length - 1;
if(left_length > 0)
{
create1(&(*T)->lson, s1.substr(1, left_length), s2.substr(0, left_length));
}
if(right_length > 0)
{
create1(&(*T)->rson, s1.substr(1 + left_length, right_length), s2.substr(1 + left_length, right_length));
}
}//已知前序和中序建立二叉树
void create2(lnode &T,int &i,int n)
{
if(i==n)
return;
if(str[i]=='#')
{
T=NULL;
i++;
}
else
{
T=new node;
T->val=str[i];
i++;
create2(T->lson,i,n);
create2(T->rson,i,n);
}
}//已知先序遍历序建立二叉树
void preorder(lnode T)
{
if(T!=NULL)
{
printf("%c",T->val);
preorder(T->lson);
preorder(T->rson);
}
}//先序遍历
void inorder(lnode T)
{
if(T!=NULL)
{
inorder(T->lson);
printf("%c",T->val);
inorder(T->rson);
}
}//中序遍历
void postorder(lnode T)
{
if(T!=NULL)
{
postorder(T->lson);
postorder(T->rson);
printf("%c",T->val);
}
}//后序遍历
int getdepth(lnode T)
{
int ld,rd;
if(T==NULL)
return 0;
else
{
ld=getdepth(T->lson);
rd=getdepth(T->rson);
if(ld>rd)
return (ld+1);
else
return (rd+1);
}
}//求二叉树的深度
int main()
{
int depth;
string str_1, str_2;
lnode T=NULL;
while(cin>>str_1>>str_2)
{
create1(&T,str_1,str_2);
postorder(T);
printf(" ");
levelorder(T);
printf("\n");
}
return 0;
}
平衡二叉树的判断
#include
#include
using namespace std;
#define maxn 2000
#define error 1
#define ok 0
#define no 1
typedef int status;
char str[2000];
typedef struct node
{
char val;
node *lson, *rson;
}node,*lnode;//链式树
void create2(lnode &T,int &i,int n)
{
if(i==n)
return;
if(str[i]=='#')
{
T=NULL;
i++;
}
else
{
T=new node;
T->val=str[i];
i++;
create2(T->lson,i,n);
create2(T->rson,i,n);
}
}//已知先序遍历序建立二叉树
void preorder(lnode T)
{
if(T!=NULL)
{
printf("%c",T->val);
preorder(T->lson);
preorder(T->rson);
}
}//先序遍历
int getdepth(lnode T)
{
int ld,rd;
if(T==NULL)
return 0;
else
{
ld=getdepth(T->lson);
rd=getdepth(T->rson);
if(ld>rd)
return (ld+1);
else
return (rd+1);
}
}//求二叉树的深度
status balance(lnode T)
{
if(T==NULL)
return 1;
int lefthight=getdepth(T->lson);
int righthight=getdepth(T->rson);
int diff=lefthight-righthight;
if(diff>1||diff<-1)
return 0;
else
return (balance(T->lson)&&balance(T->rson));
}
int main()
{
int depth;
while(scanf("%s",str)!=EOF)
{
lnode T=NULL;
int i=0;
create2(T,i,strlen(str));
if(balance(T)==0)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)