二叉树编程实践

本文介绍了一种使用C语言实现的二叉树结构,并提供了先序、中序及后序遍历的方法。此外,还实现了统计叶子节点数量、计算树深度的功能,并展示了如何复制一棵二叉树。
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include<stdio.h>
#include <string.h>

int num = 0;
/*
typedef struct BiTNode
{
	int data;
	struct BiTNode *lchild,*rchild;
};*/

//第一种表示方法 :二叉链表示法
struct BiTNode
{
	int data;
	struct BiTNode *lchild,*rchild;
};
typedef struct BiTNode BiTNode;
typedef struct BiTNode* BITree;

//先序遍历
void preOrder(BiTNode*root)
{
	if (root==NULL)
	{
		return;
	}
	printf("%d ",root->data);
	preOrder(root->lchild);
	preOrder(root->rchild);
}
//中序遍历
void inOrder(BiTNode*root)
{
	if (root==NULL)
	{
		return;
	}
	inOrder(root->lchild);
	printf("%d ",root->data);
	inOrder(root->rchild);
}
//后序遍历
void postOrder(BiTNode*root)
{
	if (root==NULL)
	{
		return;
	}
	postOrder(root->lchild);
	postOrder(root->rchild);
	printf("%d ",root->data);
}
//统计叶子节点个数
//使用了全部变量 不好?如果多线程 引发资源竞争
int count(BiTNode*root)
{
	if (root)
	{
		if ((root->lchild==NULL)&&(root->rchild==NULL))
		{
			num++;
			printf("data:%d\n",root->data);
		}
		count(root->lchild);
		count(root->rchild);
	}
	return num;
}
//避免使用全局变量
void count2(BiTNode*root,int*num)
{
	if (root)
	{
		if ((root->lchild==NULL)&&(root->rchild==NULL))
		{
			(*num)++;
			printf("data:%d\n",root->data);
		}
		if (root->lchild)
		{
			count2(root->lchild,num);
		}
		if (root->rchild)
		{
			count2(root->rchild,num);
		}
	}
}
//递归本质探究  遍历路径一样 访问时机不同
void count3(BiTNode*root,int*num)
{
	if (root)
	{
		if (root->lchild)
		{
			count3(root->lchild,num);
		}
		if ((root->lchild==NULL)&&(root->rchild==NULL))
		{
			(*num)++;
			printf("data:%d\n",root->data);
		}
		if (root->rchild)
		{
			count3(root->rchild,num);
		}
	}
}
//求树的深度
int Depth(BiTNode*root)
{
	int depthleft = 0;
	int depthright = 0;
	int depthtree = 0;
	if (root == NULL)
	{
		depthtree = 0;
		return depthtree;
	}
	depthleft = Depth(root->lchild);
	depthright = Depth(root->rchild);
	depthtree = 1 + (depthleft>depthright?depthleft:depthright);
	return depthtree;
}
//完全Copy二叉树 
BiTNode* CopyTree(BiTNode*root)
{
	BiTNode*newtree = NULL;
	BiTNode*left =    NULL;
	BiTNode*right =   NULL;
	if (root == NULL)
	{
		//tmp = NULL;
		return newtree;
	}
	if (root->lchild)
	{
		left = CopyTree(root->lchild);
	}
	else
	{
		left = NULL;
	}
	if (root->rchild)
	{
		right = CopyTree(root->rchild);
	}
	else
	{
		right = NULL;
	}
	newtree = (BiTNode*)malloc(sizeof(BiTNode));
	if (newtree==NULL)
	{
		return NULL;
	}
	newtree->lchild = left;
	newtree->rchild = right;
	newtree->data = root->data;
}
void display01()
{
	BiTNode t1,t2,t3,t4,t5,t6;
	int num = 0;
	int dep = 0;
	BiTNode*newtree;
	memset(&t1,0,sizeof(BiTNode));
	memset(&t2,0,sizeof(BiTNode));
	memset(&t3,0,sizeof(BiTNode));
	memset(&t4,0,sizeof(BiTNode));
	memset(&t5,0,sizeof(BiTNode));
	memset(&t6,0,sizeof(BiTNode));

	t1.data = 1;
	t2.data = 2;
	t3.data = 3;
	t4.data = 4;
	t5.data = 5;
	t6.data = 6;
	//建立关系 
	t1.lchild = &t2;
	t1.rchild = &t3;
	t2.lchild = &t4;
	t2.rchild = &t5;
	t3.lchild = &t6;
	/*树的遍历*/
	printf("\n先序遍历\n");
	preOrder(&t1);
	printf("\n中序遍历\n");
	inOrder(&t1);
	printf("\n后序遍历\n");
	postOrder(&t1);
	count3(&t1,&num);
	printf("\n叶子节点:%d\n",num);
	dep = Depth(&t1);
	printf("\n树的高度:%d\n",dep);
	newtree = CopyTree(&t1);
	printf("\n先序遍历\n");
	preOrder(newtree);
}

int main()
{
	display01();
	system("pause");
	return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值