树的基本操作

#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);
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值