数据结构之树

二叉树基本操作

	学习用递归函数:
		1、求二叉树 叶子数量
		2、求树的高度
		3、拷贝二叉树
		4、递归遍历
		5、释放二叉树
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>

struct BinaryNode
{
	//数据域
	char ch;

	//指针域
	struct BinaryNode* lChild;
	struct BinaryNode* rChild;

};
void calculateLeafNum(struct BinaryNode* root, int *p)
{
	if (root == NULL)
	{
		return;
	}

	if (root->lChild == NULL && root->rChild == NULL)
	{
		(*p)++;
	}

	calculateLeafNum(root->lChild, p);
	calculateLeafNum(root->rChild, p);

}
int getTreeHeight(struct BinaryNode* root)
{
	if (root == NULL)
	{
		return 0;
	}

	//获取左子树高度
	int lHeight = getTreeHeight(root->lChild);
	//获取右子树高度
	int rHeight = getTreeHeight(root->rChild);

	//从左子树和右子树中取大的值+1
	int height = lHeight > rHeight ? lHeight + 1 : rHeight + 1;

	return height;

}

struct BinaryNode* copyTree(struct BinaryNode* root)
{
	if (root == NULL)
	{
		return NULL;
	}

	//先拷贝左子树
	struct BinaryNode* lChlild = copyTree(root->lChild);
	//再拷贝右子树
	struct BinaryNode* rClild = copyTree(root->rChild);
	//创建新节点
	struct BinaryNode* newNode = malloc(sizeof(struct BinaryNode));
	if (newNode == NULL)
	{
		return NULL;
	}
	newNode->ch = root->ch;

	//建立关系

	newNode->lChild = lChlild;
	newNode->rChild = rClild;

	return newNode;
}

void recursion(struct BinaryNode* root)
{
	if (root == NULL)
	{
		return;
	}

	先序遍历
	//printf("%c ", root->ch);

	recursion(root->lChild);

	中序遍历
	//printf("%c ", root->ch);

	recursion(root->rChild);

	//后序遍历
	printf("%c ", root->ch);

}

void freeTree(struct BinaryNode* root)
{
	if (root == NULL)
	{
		return;
	}

	//先释放左子树
	freeTree(root->lChild);
	//再释放右子树
	freeTree(root->rChild);
	//释放根
	printf("%c被释放了\n", root->ch);
	free(root);
}
void test01()
{
	struct BinaryNode nodeA = { 'A', NULL, NULL };
	struct BinaryNode nodeB = { 'B', NULL, NULL };
	struct BinaryNode nodeC = { 'C', NULL, NULL };
	struct BinaryNode nodeD = { 'D', NULL, NULL };
	struct BinaryNode nodeE = { 'E', NULL, NULL };
	struct BinaryNode nodeF = { 'F', NULL, NULL };
	struct BinaryNode nodeG = { 'G', NULL, NULL };
	struct BinaryNode nodeH = { 'H', NULL, NULL };

	//建立关系
	nodeA.lChild = &nodeB;
	nodeA.rChild = &nodeF;

	nodeB.rChild = &nodeC;

	nodeC.lChild = &nodeD;
	nodeC.rChild = &nodeE;

	nodeF.rChild = &nodeG;

	nodeG.lChild = &nodeH;

	//1、求二叉树 叶子数量
	int num = 0;
	calculateLeafNum(&nodeA, &num);
	//2、求树的高度
	int height = getTreeHeight(&nodeA);

	//3、拷贝二叉树
	struct BinaryNode* newTree = copyTree(&nodeA);
	//4、递归遍历
	printf("后序递归遍历:");
	recursion(newTree);
	printf("\n");
	//5、释放二叉树
	freeTree(newTree);

}


int main(void)
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

二叉树非递归遍历

	1、将根节点 入栈
	2、只要栈中元素个数大于 0  执行循环
		获取栈顶元素
		出栈
		如果标志位真  直接输出  并且执行下一次循环
		如果为假 将标志改为真
		将右子树  左子树 根 入栈
		执行下一次循环
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>

#include "seqStack.h"
struct BinaryNode
{
	//数据域
	char ch;

	//指针域
	struct BinaryNode* lChild;
	struct BinaryNode* rChild;
	
	//标志
	int flag;
};
void nonRecursion(struct BinaryNode* root)
{
	if (root == NULL)
	{
		return;
	}

	//初始化栈
	SeqStack myStack = init_SeqStack();

	push_SeqStack(myStack, root);

	while (size_SeqStack(myStack) > 0)
	{
		//获取栈顶元素
		struct BinaryNode* pTop = top_SeqStack(myStack);

		//出栈
		pop_SeqStack(myStack);

		//如果标志位真  直接输出  并且执行下一次循环
		if (pTop->flag == 1)
		{
			printf("%c ", pTop->ch);
			continue;
		}

		//如果为假 将标志改为真
		pTop->flag = 1;

		//将右子树  左子树 根 入栈
		if (pTop->lChild != NULL)
		{
			push_SeqStack(myStack, pTop->lChild);
		}
		if (pTop->rChild != NULL)
		{
			push_SeqStack(myStack, pTop->rChild);
		}
		push_SeqStack(myStack, pTop);

	}

	//销毁栈
	destroy_SeqStack(myStack);
}

void test01()
{
	struct BinaryNode nodeA = { 'A', NULL, NULL,0 };
	struct BinaryNode nodeB = { 'B', NULL, NULL,0 };
	struct BinaryNode nodeC = { 'C', NULL, NULL,0 };
	struct BinaryNode nodeD = { 'D', NULL, NULL,0 };
	struct BinaryNode nodeE = { 'E', NULL, NULL,0 };
	struct BinaryNode nodeF = { 'F', NULL, NULL,0 };
	struct BinaryNode nodeG = { 'G', NULL, NULL,0 };
	struct BinaryNode nodeH = { 'H', NULL, NULL,0 };

	//建立关系
	nodeA.lChild = &nodeB;
	nodeA.rChild = &nodeF;

	nodeB.rChild = &nodeC;

	nodeC.lChild = &nodeD;
	nodeC.rChild = &nodeE;

	nodeF.rChild = &nodeG;

	nodeG.lChild = &nodeH;

	//非递归遍历
	nonRecursion(&nodeA);
}

int main(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值