#include<stdio.h>
#include<stdlib.h>
typedef struct node{
char data;
struct node *lchild;
struct node *rchild;
}node,*tree;
//扩展先序遍历创建二叉树
tree creat(){
char a=getchar();
if(a=='#') return NULL;
else{
tree t=(tree)malloc(sizeof(node));
t->data=a;
// creat(t->lchild);
// creat(t->rchild);
t->lchild=creat();
t->rchild=creat();
return t;
}
}
//先序遍历
void preorder(tree t){
if(t){
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
//中序遍历
void midorder(tree t){
if(t){
midorder(t->lchild);
printf("%c",t->data);
midorder(t->rchild);
}
}
//后序遍历
void endorder(tree t){
if(t){
endorder(t->lchild);
endorder(t->rchild);
printf("%c",t->data);
}
}
typedef struct t1{
tree d[100];
int top;
}z,*zhan;
void creat_zhan(zhan *t111){
*t111=(zhan)malloc(sizeof(z));
(*t111)->top=-1;
}
void push(zhan t,tree p){
t->top=t->top+1;
t->d[t->top]=p;
}
void pop(zhan t,tree *p){
*p=t->d[t->top];
t->top--;
}
int IsEmpty(zhan s){
if(s->top==-1) return 1;
return 0;
}
//非递归先序
void pre(tree root){
tree p=root;
zhan s;
creat_zhan(&s);
// push(s,p);
while(p!=NULL ||!IsEmpty(s)){
while(p!=NULL){
printf("%c",p->data);
push(s,p);
p=p->lchild;
}
if(!IsEmpty(s)){
pop(s,&p);
p=p->rchild;
}
}
}
//非递归中序
void mid(tree root){
tree p=root;
zhan s;
creat_zhan(&s);
// push(s,p);
while(p!=NULL ||!IsEmpty(s)){
while(p!=NULL){
push(s,p);
p=p->lchild;
}
if(!IsEmpty(s)){
pop(s,&p);
printf("%c",p->data);
p=p->rchild;
}
}
}
typedef struct nodee{
tree a[100];
int q,h;
}*dui,d;
dui creat_dui(){
dui t=(dui)malloc(sizeof(d));
t->q=t->h=-1;
return t;
}
void entter(dui s,tree q){
s->h++;
s->a[s->h]=q;
}
void outt(dui s,tree *q){
s->q++;
*q=s->a[s->q];
}
int IsEmpty(dui s){
if(s->h==-1 && s->q==-1)
return 1;
return 0;
}
//层次遍历
void overload(tree root){
tree p=root;
dui s=creat_dui();
entter(s,p);
while(!IsEmpty(s)){
outt(s,&p);
printf("%c",p->data);
if(p->lchild!=NULL){
entter(s,p->lchild);
}
if(p->rchild!=NULL){
entter(s,p->rchild);
}
}
}
//统计二叉树结点数
int count;
void pree(tree root){
if(root!=NULL){
count++;
pree(root->lchild);
pree(root->rchild);
}
}
//叶节点个数
int leaf(tree root){
int nl,nr;
if(root==NULL) return 0;
if((root->lchild)==NULL&&(root->rchild)==NULL) return 1;
nl=leaf(root->lchild);
nr=leaf(root->rchild);
return (nl+nr);
}
//二叉树高度
int treedepth(tree root){
int hl,hr;
if(root){
hl=treedepth(root->lchild);
hr=treedepth(root->rchild);
}
int h=(hl>hr?hl:hr)+1;
return h;
}
//二叉树双亲节点
tree parent(tree p,tree q){
tree *t;
if(p==NULL) return NULL;
if(p->lchild==q || p->rchild==q)
return p;
*t=parent(p->lchild,q);
if(p!=NULL) return *t;
else return (parent(p->rchild,q));
}
main(){
tree t;
t=creat();
printf("先序遍历结果:");
preorder(t);
printf("\n");
printf("中序遍历结果:");
midorder(t);
printf("\n");
printf("后序遍历结果:");
endorder(t);
printf("\n");
printf("非递归先序结果:");
pre(t);
printf("\n");
printf("非递归中序结果:");
mid(t);
printf("\n");
printf("二叉树结点个数:");
pree(t);
printf("%d\n",count);
printf("二叉树叶结点个数:");
int a=leaf(t);
printf("%d\n",a);
printf("二叉树高度:");
int b=treedepth(t);
printf("%d\n",a);
printf("层次遍历结果:");
overload(t);
printf("\n");
//printf("双亲节点:");(有点问题)
//tree D,c=parent(t,D);
//printf("%c\n",c);
}
树的基本操作
最新推荐文章于 2022-03-12 17:53:52 发布