树的简单操作

#include<stdio.h>
#include"stdlib.h"
typedef struct Node
{
	char data;
	struct Node *Lchild;
	struct Node *Rchild;
}Bitnode,*BiTree;



/*void CreateBitree(BiTree *root)
{
	char ch;
	ch=getchar();
	if(ch=='^') *root=NULL;
	else
	{
		*root=(BiTree)malloc(sizeof(Bitnode));
		(*root)->data=ch;
		CreateBitree(&((*root)->Lchild));
		CreateBitree(&((*root)->Rchild));
	}
}*/


//层次遍历用到队列 
typedef struct
{
	BiTree date[100];
	int first;
	int last;	
}sequeue;

 
int empty(sequeue *p)               //是否为空 
{
	if(p->first==p->last)
	{
		return 1;
	}
	return 0;
}


void initqueue(sequeue **p)          //初始化 
{
	*p=(sequeue*)malloc(sizeof(sequeue));
	if((*p)!=NULL)
	{
		(*p)->first=-1;
		(*p)->last=-1;
	}
	else
	{
		printf("memory overflow!\n");
		exit (0);
		
	}
}


void Enqueue(sequeue *p,BiTree root)  //入队 
{
	if(p->last==100-1)
	{
		printf("队列已满\n");
	getch();
	}
	else
	{
		p->last++;
		p->date[p->last]=root;
		
	}
}


BiTree outqueue(sequeue *p)           //出队 
{
	BiTree root;
	if(empty(p))
	{
		printf("队列为空\n");
	}
	else
	{
		p->first++;
		root=p->date[p->first];
		return root;
	}
}





BiTree CreateBitree()
{
	BiTree root;
	char ch;
	ch=getchar();
	if(ch=='^') root=NULL;
	else
	{
		root=(BiTree)malloc(sizeof(Bitnode));
		(root)->data=ch;
		root->Lchild=CreateBitree();
		root->Rchild=CreateBitree();
	}
	return root;
}


void PreOrder(BiTree root)            //先序遍历 
{
	if(root)
	{
		printf("%c",root->data);
		PreOrder(root->Lchild);
		PreOrder(root->Rchild);	
	}
}


void InOrder(BiTree root)             //中序遍历 
{
	if(root)
	{
	    InOrder(root->Lchild);
		printf("%c",root->data);
		InOrder(root->Rchild);	
	}
}


void PosOrder(BiTree root)            //后序遍历 
{
	if(root)
	{
	    PosOrder(root->Lchild);
		PosOrder(root->Rchild);	
		printf("%c",root->data);
	}
}


void Levelorder(BiTree root)          //层次遍历 
{
	sequeue *q;
	BiTree p;
	initqueue(&q);
	Enqueue(q,root);
	while(!empty(q))
	{
		p=outqueue(q);
		printf("%c",p->data);
		if(p->Lchild!=NULL)  Enqueue(q,p->Lchild);
		if(p->Rchild!=NULL)  Enqueue(q,p->Rchild);
	}
	
}


void leaf(BiTree root)                //求叶子结点 
{
	if(root)
	{
		leaf(root->Lchild);
		if(root->Lchild==NULL && root->Rchild==NULL)
		printf("%c",root->data);
		leaf(root->Rchild);	
	}
}


int leafsum(BiTree root)              //叶子结点数目 
{
	int nl,nr;
	if(root==NULL) return 0;
	if(root->Lchild==NULL && root->Rchild==NULL) return 1;
	nl=leafsum(root->Lchild);
	nr=leafsum(root->Rchild);
	return(nl+nr);
}


void printBiTree(BiTree root,int n)   //打印树 
{
	int i;
	if(root!=NULL)
	{
	printBiTree(root->Rchild,n+1);
	for(i=0;i<3*n;i++)  printf(" ");
	printf("%c\n",root->data);
	printBiTree(root->Lchild,n+1);
	}
}


int BiTreedepth(BiTree root)         //求树的深度    
{
	int hl,hr,h;
	
	if(root=NULL) return 0;
	else 
	{
		hl=BiTreedepth(root->Lchild);printf("n");
		hr=BiTreedepth(root->Rchild);
		h=(hl>hr?hl:hr)+1; 
		return h;
	}
}


int d=0;
void deep(BiTree root ,int h)       //求树的深度 
{
	if(root)
	{
		if(h>d) d=h;
		deep(root->Lchild,h+1);
		deep(root->Rchild,h+1);  
	}
}


BiTree parent(BiTree root,char Data)//寻找双亲结点 
{
	BiTree p;
	if(root==NULL) return NULL;
	if(root->Lchild->data==Data||root->Rchild->data==Data) return root;
	p=parent(root->Lchild,Data);
	if(p!=NULL) return p;
	else return(parent(root->Rchild,Data));
}


void main()
{
    BiTree root,p;
    int sum,n=1,h=1;
    char Data;
    printf("请输入二叉树的结点:"); 
    root=CreateBitree();
  //CreateBitree(&root);
    printf("\n先序遍历:");
    PreOrder(root);
    printf("\n中序遍历:"); 
    InOrder(root);
    printf("\n后序遍历:");
    PosOrder(root);	
    printf("\n叶子结点为:");
    leaf(root);
    sum=leafsum(root);
    printf("\n叶子结点数为:%d",sum); 
    printf("\n按树状打印:\n") ;
    printBiTree(root,n);
    deep(root,h);
    //h=BiTreedepth(root);
    printf("树的深度:%d\n",d);
    printf("请输入结点:");
    getchar();
    Data=getchar(); 
    p=parent(root,Data);
   /* if(p!=NULL)*/ printf("双亲结点为:%c\n",p->data);
    //else printf("没有该结点");
    printf("层次遍历:");
    Levelorder(root);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值