二叉树的设计,设计二叉树的基本操作.(有问题欢迎指点)

基本要求:1.定义二叉链存储结构 2.建立二叉链表存储的二叉树 3.实现二叉树的遍历(先序、中序、后序、层序) 4.求二叉树的高度、求结点双亲、统计叶子结点数目、打印二叉树,交换每个结点的左孩子和右孩子等操作

测试数据:ABD#G###CE#H##F##

二叉树:                      A

                                  /     \

                                B       C

                              /          /   \

                            D         E     F

                              \           \

                              G          H

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100

const int N=100;
typedef char DataType;
typedef struct Node   //定义二叉链存储结构
{
	DataType data;
	struct Node *lchild;
	struct Node *rchild;
}BiTNode, *BiTree;

typedef struct Queue
{
	int front;
	int rear;
	Node *data[MaxSize];
}SqQueue;

void CreateBiTree(BiTree *root) //建立二叉链表存储的二叉树
{
    char ch;
    scanf(" %c",&ch);
    if(ch=='#') *root=NULL;     // #表示当前结点为空
    else
	{
        *root=(BiTree)malloc(sizeof(BiTNode)); 
        (*root)->data=ch;
        CreateBiTree(&((*root)->lchild));       // 构造左子树
        CreateBiTree(&((*root)->rchild));          // 构造右子树
    }
	
}

void PreOrder(BiTree root)    //先序遍历二叉树(递归)
{
    if(root!=NULL)
	{
		printf("%c",root->data);      
        PreOrder(root->lchild);  
        PreOrder(root->rchild);  
	}
}

void InOrder(BiTree root)     //中序遍历二叉树(递归)
{
    if(root!=NULL)
	{
		InOrder(root->lchild);
        printf("%c",root->data);
        InOrder(root->rchild);
	}
}
void PostOrder(BiTree root)   //后序遍历二叉树(递归)
{
    if(root!=NULL)
	{
		PostOrder(root->lchild);
        PostOrder(root->rchild);
        printf("%c",root->data);
	}
}

void LevelOrder(BiTree root)  //层序遍历二叉树
{
	Node *q=NULL;
	SqQueue *Q;
	Q=(SqQueue*)malloc(sizeof(SqQueue));
	Q->front=Q->rear=-1;
	if(root==NULL)return;
	Q->data[++Q->rear]=root;
	while(Q->front!=Q->rear)
	{
		q=Q->data[++Q->front];
		printf("%c",q->data);
		if(q->lchild!=NULL) Q->data[++Q->rear]=q->lchild;
		if(q->rchild!=NULL) Q->data[++Q->rear]=q->rchild;
	}
}

int TreeDepth(BiTree root)   //求二叉树的高度
{
	int hl,hr,h;
	if(root==NULL)return 0;
	else
	{
		hl=TreeDepth(root->lchild);
		hr=TreeDepth(root->rchild);
		return h=(hl>hr?hl:hr)+1;
	}
}

BiTree Parent(BiTree root,char x)  //求结点双亲
{
	if(root==NULL)
        return NULL;
    if(root->lchild!= NULL)
    {
        if(root->lchild->data==x)
	    {
			printf("该节点的父结点是:%c",root->data);
			return root;
        }
    }
    if(root->rchild != NULL)
    {
        if(root->rchild->data==x)    
        {
			printf("该节点的父结点是:%c",root->data); 
			return root;
        }
    
    }
    Parent(root->lchild,x);
    Parent(root->rchild,x);
}

int Countleaf(BiTree root,int count) //叶子结点数目
{
	if(root)
	{
		if((root->lchild==NULL)&&(root->rchild==NULL))
			count++;
		count=Countleaf(root->lchild,count);
		count=Countleaf(root->rchild,count);
	}
	return count;
}

void PrintTree(BiTree root,int level)  //打印二叉树
{
	int i;
	if(root->rchild)
		PrintTree(root->rchild,level+1);
	printf("----");
    for(i=0;i<level;i++)
		printf("-----");
	printf("%c\n",root->data);
	if(root->lchild)
	{
		PrintTree(root->lchild,level+1);
	}
}

int Exchange(BiTree root) //交换每个结点的左孩子和右孩子
{
	BiTree p;
	p=(BiTree)malloc(sizeof(BiTree));
	if(root)
	{
		p=root->rchild;
		root->rchild=root->lchild;
		root->lchild=p;
	    Exchange(root->lchild);
	    Exchange(root->rchild);
	}
	return 1;
}

int main()
{
    BiTree T;
	BiTree p;
	char ch;
	int count=0;
	printf("请输入二叉树:");
    CreateBiTree(&T);
	printf("\n");
	printf("先序遍历输出二叉树:");
    PreOrder(T);
	printf("\n");
	printf("中序遍历输出二叉树:");
	InOrder(T);
  	printf("\n");
	printf("后序遍历输出二叉树:");
	PostOrder(T);
	printf("\n");
	printf("层序遍历输出二叉树:");
    LevelOrder(T);
	printf("\n");
	printf("二叉树的高度:%d",TreeDepth(T));
	printf("\n");
	printf("请输入要查询的结点:");
	scanf("%c\n",&ch);
	p=Parent(T,ch);
	if(p) printf("该结点的双亲结点为:%c\n",p->data);
	else printf("该结点为根节点或不存在!\n");
    printf("叶子结点数目:%d",Countleaf(T,count));
	printf("\n");
	printf("打印二叉树:\n");
	PrintTree(T,1);
	printf("交换每个结点的左孩子和右孩子后,层序遍历为:");
	Exchange(T);
	LevelOrder(T);
	printf("\n");
    return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值