二叉树(递归实现先序遍历,中序遍历,后序遍历,非递归实现先序遍历,中序遍历,后序遍历,二叉树层序遍历,二叉树结点个数,二叉树叶子结点个数,判断一颗树是否为完全二叉树)

二叉树(递归实现先序遍历,中序遍历,后序遍历,非递归实现先序遍历,中序遍历,后序遍历,二叉树层序遍历,二叉树结点个数,二叉树叶子结点个数,判断一颗树是否为完全二叉树)
1.构造二叉树:通过前序遍历的数组eg:"ABD##E#H##CF##G##"构建二叉树;
2.递归实现二叉树的前中后序遍历;
3.非递归实现二叉树的前中后序遍历;
4.后序遍历销毁二叉树;
5.层序遍历:借助队列实现;
6.判断一颗二叉树是否为完全二叉树:有右没左,或有孩子且标记为1;
7.二叉树结点个数的计数通过遍历实现;
8.叶子结点的个数通过判断该结点的类型实现.
BinaryTree.h

#ifndef _BINARYTREE_H_
#define _BINARYTREE_H_

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef char BTDataType;

typedef struct BinaryTreeNode
{
	BTDataType _data;
	struct BinaryTreeNode* _left;
	struct BinaryTreeNode* _right;
}BTNode;

// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDataType* src);
// 递归遍历
void BinaryTreePrevOrder(BTNode* root);//先序遍历
void BinaryTreeInOrder(BTNode* root);//中序遍历
void BinaryTreePostOrder(BTNode* root);//后序遍历
void BinaryTreeLevelOrder(BTNode* root);// 层序遍历
void BinaryTreeDestory(BTNode** root);//销毁二叉树
// 非递归遍历
void BinaryTreePrevOrderNonR(BTNode* root);//先序遍历
void BinaryTreeInOrderNonR(BTNode* root);//中序遍历
void BinaryTreePostOrderNonR(BTNode* root);//后序遍历
//递归实现
int BinaryTreeLeafSize(BTNode* root);二叉树叶子结点个数
int BinaryTreeLevelKSize(BTNode* root, int k);第K层结点个数
//非递归实现
int BinaryTreeSizeNonR(BTNode* root);二叉树结点个数
int BinaryTreeLeafSizeNonR(BTNode* root);//二叉树叶子结点个数
int BinaryTreeLevelKSizeNonR(BTNode* root, int k);//第K层结点个数
int BinaryTreeComplete(BTNode* root);// 判断二叉树是否是完全二叉树
#endif // !_BINARYTREE_H_

Queue.h

#ifndef _QUEUE_H_
#define _QUEUE_H_
#include "BinaryTree.h"

typedef BTNode * QueueDataType;

typedef struct QueueNode
{
	QueueDataType _data;
	struct QueueNode* _next;
}QueueNode;

typedef struct Queue {
	QueueNode * _head;
	QueueNode * _real;
}Queue;

void QueueInit(Queue* plist);
void QueueDestory(Queue* plist);
void QueuePop(Queue* plist);
void QueuePush(Queue* plist, QueueDataType x);
QueueDataType QueueTop(Queue* plist);
int QueueEmpty(Queue* plist);
int QueueSize(Queue* plist);

#endif //_QUEUE_H_

Stack.h

#ifndef _Stack_H_
#define _Stack_H_


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "BinaryTree.h"

typedef BTNode* StDataType;

typedef struct Stack
{
	StDataType* array; // 指向动态开辟的数组
	size_t size; // 有效数据个数
	size_t capicity; // 容量空间的大小
}Stack;

void StackInit(Stack* psl, size_t capacity);
void StackDestory(Stack* psl);
void CheckCapacity(Stack* psl);
void StackPush(Stack* psl, StDataType x);
void StackPop(Stack* psl);
StDataType StackTop(Stack* psl);
int StackEmpty(Stack* psl);

#endif // _Stack_H_

Binarytree.c

#include "BinaryTree.h"
#include "Queue.h"
#include "Stack.h"

// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDataType* src) {
	static int s_n = 0;
	if (src[s_n] == '#') {
		s_n++;
		return NULL;
	}
	BTNode* cur = (BTNode*)malloc(sizeof(BTNode));
	cur->_data = src[s_n];
	s_n++;
	cur->_left = BinaryTreeCreate(src);
	cur->_right = BinaryTreeCreate(src);
	return cur;
}

//递归遍历
//前序遍历(递归)
void BinaryTreePrevOrder(BTNode* root) {
	//assert(root);
	if (root) {
		putchar(root->_data);
		BinaryTreePrevOrder(root->_left);
		BinaryTreePrevOrder(root->_right);
	}
}

//中序遍历(递归)
void BinaryTreeInOrder(BTNode* root) {
	//assert(root);
	if (root) {
		BinaryTreeInOrder(root->_left);
		putchar(root->_data);
		BinaryTreeInOrder(root->_right);
	}
}

//后序遍历(递归)
void BinaryTreePostOrder(BTNode* root) {
	//assert(root);
	if (root) {
		BinaryTreePostOrder(root->_left);
		BinaryTreePostOrder(root->_right);
		putchar(root->_data);
	}
}

//层序遍历(队列实现)
void BinaryTreeLevelOrder(BTNode* root) {
	BTNode* cur;
	Queue qu;
	QueueInit(&qu);
	QueuePush(&qu, root);
	while (!QueueEmpty(&qu)) {
		cur = QueueTop(&qu);
		putchar(cur->_data);
		if (cur->_left) {
			QueuePush(&qu, cur->_left);
		}
		if (cur->_right) {
			QueuePush(&qu, cur->_right);
		}
		QueuePop(&qu);
	}
	QueueDestory(&qu);
}

//销毁二叉树
void BinaryTreeDestory(BTNode** root) {
	assert(root);
	if (root) {
		BinaryTreePostOrder((*root)->_left);
		BinaryTreePostOrder((*root)->_right);
		free(*root);
		*root = NULL;
	}
}

//非递归遍历(均用栈实现)
//前序遍历(非递归)
void BinaryTreePrevOrderNonR(BTNode* root) {
	assert(root);
	BTNode* cur = root;
	Stack st;
	StackInit(&st, 100);
	while (cur)	{
		putchar(cur->_data);
		if (cur->_right) {
			StackPush(&st, cur->_right);
		}
		if (cur->_left) {
			cur = cur->_left;
		}
		else {
			cur = StackTop(&st);
			StackPop(&st);
		}
	}
	StackDestory(&st);
}

//中序遍历(非递归)
void BinaryTreeInOrderNonR(BTNode* root) {
	assert(root);
	BTNode* cur = root;
	Stack st;
	StackInit(&st, 100);
	while (!StackEmpty(&st) || cur) {
		for (; cur; cur = cur->_left) {
			StackPush(&st, cur);
		}
		cur = StackTop(&st);
		putchar(cur->_data);
		StackPop(&st);
		cur = cur->_right;
	}
	StackDestory(&st);
}

//后序遍历(非递归)
void BinaryTreePostOrderNonR(BTNode* root) {
	assert(root);
	BTNode* cur = root;
	Stack st;
	char tag[100];
	StackInit(&st, 100);
	do {
		for (; cur; cur = cur->_left) {
			StackPush(&st, cur);
			tag[st.size - 1] = 0;
		}
		while (!StackEmpty(&st) && tag[st.size - 1]) {
			cur = StackTop(&st);
			putchar(cur->_data);
			StackPop(&st);
		}
		if (!StackEmpty(&st)) {
			cur = StackTop(&st);
			tag[st.size - 1] = 1;
			cur = cur->_right;
		}
	} while (!StackEmpty(&st));
	StackDestory(&st);
}

//递归实现
//二叉树叶子结点个数
int BinaryTreeLeafSize(BTNode* root) {
	if (root == NULL) {
		return 0;
	}
	else if ((root->_left == NULL) && (root->_right == NULL)) {
		return 1;
	}
	else {
		return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
	}
}

//第K层结点个数
int BinaryTreeLevelKSize(BTNode* root, int k) {
	if (root == NULL || k <= 0) {
		return 0;
	}
	else if (root&&k == 1) {
		return 1;
	}
	else {
		return BinaryTreeLevelKSize(root->_left, k - 1) + BinaryTreeLevelKSize(root->_right, k - 1);
	}
}

//非递归实现
//二叉树结点个数
int BinaryTreeSizeNonR(BTNode* root) {
	assert(root);
	int count = 0;
	BTNode* cur = root;
	Stack st;
	StackInit(&st, 100);
	while (cur)	{
		count++;
		//putchar(cur->_data);
		if (cur->_right) {
			StackPush(&st, cur->_right);
		}
		if (cur->_left) {
			cur = cur->_left;
		}
		else {
			cur = StackTop(&st);
			StackPop(&st);
		}
	}
	StackDestory(&st);
	return count;
}

//二叉树叶子结点个数
int BinaryTreeLeafSizeNonR(BTNode* root) {
	assert(root);
	int count = 0;
	BTNode* cur = root;
	Stack st;
	StackInit(&st, 100);
	while (cur)	{
		if (cur->_left == NULL && cur->_right == NULL) {
			count++;
		}
		//putchar(cur->_data);
		if (cur->_right) {
			StackPush(&st, cur->_right);
		}
		if (cur->_left) {
			cur = cur->_left;
		}
		else {
			cur = StackTop(&st);
			StackPop(&st);
		}
	}
	StackDestory(&st);
	return count;
}

//第K层结点个数
int BinaryTreeLevelKSizeNonR(BTNode* root, int k) {
	if (root == NULL || k <= 0) {
		return 0;
	}
	int cur_level_size = 0;
	int cur_level = 0;
	Queue qu;
	QueueInit(&qu);
	QueuePush(&qu,root);
	while (!QueueEmpty(&qu)) {
		cur_level++;
		cur_level_size = QueueSize(&qu);
		if (cur_level == k) {
			break;
		}
		int tmp_count = 0;
		while (tmp_count < cur_level_size) {
			tmp_count++;
			root = QueueTop(&qu);
			QueuePop(&qu);
			if (root->_left) {
				QueuePush(&qu, root->_left);
			}
			if (root->_right) {
				QueuePush(&qu, root->_right);
			}
		}
	}
	QueueDestory(&qu);
	if (cur_level == k) {
		return cur_level_size;
	}
}

// 判断二叉树是否是完全二叉树
int BinaryTreeComplete(BTNode* root) {
	int tag = 0;
	BTNode* cur;
	Queue qu;
	QueueInit(&qu);
	QueuePush(&qu, root);
	while (!QueueEmpty(&qu)) {
		cur = QueueTop(&qu);
		putchar(cur->_data);
		if (root->_right&&root->_left == NULL) {
			return 0;
		}
		if (tag && (root->_left || root->_right)) {
			return 0;
		}
		if (cur->_left) {
			QueuePush(&qu, cur->_left);
		}
		if (cur->_right) {
			QueuePush(&qu, cur->_right);
		}
		else {
			tag = 1;
		}
		QueuePop(&qu);
	}
	QueueDestory(&qu);
	return 1;
}

Queue.c

#include "Queue.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

void QueueInit(Queue* plist) {
	assert(plist);
	plist->_head = NULL;
	plist->_real = NULL;
}

void QueueDestory(Queue* plist)
{
	assert(plist);
	QueueNode * tmp;
	while (plist->_head) {
		tmp = plist->_head;
		plist->_head = plist->_head->_next;
		free(tmp);
	}
}

void QueuePop(Queue* plist)
{
	assert(plist);
	QueueNode * tmp;
	if (plist->_head) {
		tmp = plist->_head;
		plist->_head = plist->_head->_next;
		free(tmp);
	}
}

void QueuePush(Queue* plist, QueueDataType x) {
	assert(plist);
	QueueNode * cur = (QueueNode *)malloc(sizeof(QueueNode));
	cur->_data = x;
	cur->_next = NULL;
	if (QueueEmpty(plist)) {
		plist->_head = plist->_real = cur;
		return;
	}
	plist->_real->_next = cur;
	plist->_real = cur;
}

int QueueEmpty(Queue* plist) {
	assert(plist);
	return plist->_head == NULL;
}

QueueDataType QueueTop(Queue* plist) {
	assert(plist);
	if (QueueEmpty(plist)) {
		return (QueueDataType)0;
	}
	return plist->_head->_data;
}

//队列长度
int QueueSize(Queue* plist) {
	assert(plist);
	if (plist == NULL) {
		return 0;
	}
	int count = 0;
	QueueNode* cur;
	for (cur = plist->_head; cur; cur = cur->_next) {
		count++;
	}
	return count;
}

Stack.c

#include "Stack.h"

//初始栈
void StackInit(Stack* psl, size_t capacity) {
	assert(psl);
	psl->capicity = capacity;
	psl->array = (StDataType*)malloc(capacity*sizeof(StDataType));
	assert(psl->array);
	psl->size = 0;
}

//销毁栈
void StackDestory(Stack* psl) {
	assert(psl);
	if (psl->array) {
		free(psl->array);
		psl->capicity = 0;
		psl->size = 0;
		psl->array = NULL;
	}
}

//扩容栈
void CheckCapacity(Stack* psl) {
	assert(psl);
	if (psl->size == psl->capicity) {
		psl->capicity *= 2;
		psl->array = (StDataType*)realloc(psl->size, psl->capicity*sizeof(StDataType));
	}
}

//入栈
void StackPush(Stack* psl, StDataType x) {
	assert(psl);
	CheckCapacity(psl);
	psl->array[psl->size] = x;
	psl->size++;
}

//出栈
void StackPop(Stack* psl) {
	assert(psl || psl->size);
	psl->size--;
}

//返回栈顶元素
StDataType StackTop(Stack* psl) {
	assert(psl);
	if (StackEmpty(psl)) {
		return (StDataType)0;
	}
	return psl->array[psl->size - 1];
}

//判空
int StackEmpty(Stack* psl) {
	assert(psl);
	return psl->size == 0;
}

main.c

#include "BinaryTree.h"
#include "Queue.h"
#include "Stack.h"

int main() {
	int count;
	Queue que;
	//ABD##E#H##CF##G##
	//ABD#GI##J###CE#HK###F##
	BTNode* root = BinaryTreeCreate("ABD#GI##J###CE#HK###F##");
	BinaryTreePrevOrder(root);
	putchar('\n');
	BinaryTreeInOrder(root);
	putchar('\n');
	BinaryTreePostOrder(root);
	putchar('\n');
	BinaryTreeLevelOrder(root);
	putchar('\n');
	BinaryTreePrevOrderNonR(root);
	putchar('\n');
	BinaryTreeInOrderNonR(root);
	putchar('\n');
	BinaryTreePostOrderNonR(root);
	putchar('\n');
	//printf("%d\n", BinaryTreeSize(root));
	printf("%d\n", BinaryTreeLeafSize(root));
	printf("%d\n", BinaryTreeSizeNonR(root));
	printf("%d\n", BinaryTreeLeafSizeNonR(root));
	printf("%d\n", BinaryTreeLevelKSize(root, 1));
	printf("%d\n", BinaryTreeLevelKSize(root, 2));
	printf("%d\n", BinaryTreeLevelKSize(root, 3));
	printf("%d\n", BinaryTreeLevelKSize(root, 4));
	printf("%d\n", BinaryTreeLevelKSize(root, 5));
	printf("%d\n", BinaryTreeLevelKSizeNonR(root, 1));
	printf("%d\n", BinaryTreeLevelKSizeNonR(root, 2));
	printf("%d\n", BinaryTreeLevelKSizeNonR(root, 3));
	printf("%d\n", BinaryTreeLevelKSizeNonR(root, 4));
	printf("%d\n", BinaryTreeLevelKSizeNonR(root, 5));
	BinaryTreeComplete(root);
	putchar('\n');
	system("pause");				   
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值