创建一颗二叉树,二叉树的前序遍历,中序遍历,后序遍历,层序遍历。

二叉树的有些操作是需要借助栈和队列完成的。

栈和队列的基本操作在链接中的博客中https://blog.csdn.net/Damn_Yang/article/details/83928852

下面就是代码的实现部分了

BinaryTree.h

#pragma once
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>

//struct TreeNode
//{
//	// data;
//	TNDataType data;
//	//struct TreeNode* childs[N];
//	SeqList childs; //->struct TreeNode*
//};

typedef char BTDataType;

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

// a是一个前序遍历的数组
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi);
void BinaryTreeDestory(BTNode** root);

int BinaryTreeSize(BTNode* root);
int BinaryTreeLeafSize(BTNode* root);
int BinaryTreeLevelKSize(BTNode* root, int k);
BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
int BinaryTreeHigh(BTNode* root);

// 遍历  递归&非递归
//前序遍历
void BinaryTreePrevOrder(BTNode* root);
//中序遍历
void BinaryTreeInOrder(BTNode* root);
//后序遍历
void BinaryTreePostOrder(BTNode* root);
//层序遍历
void BinaryTreeLevelOrder(BTNode* root);
//判断二叉树是否为完全二叉树
int BinaryTreeComplete(BTNode* root);

//前中后序遍历的非
void BinaryTreePrevOrderNonR(BTNode* root);
void BinaryTreeInOrderNonR(BTNode* root);
void BinaryTreePostOrderNonR(BTNode* root);

void TestBinaryTree();

BinaryTree.c

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

BTNode*BuyBTNode(BTDataType x)
{
	BTNode*Node = (BTNode*)malloc(sizeof(BTNode));
	Node->_left = NULL;
	Node->_right = NULL;
	Node->_data = x;
	return Node;
}

BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi)
{
	if (a[*pi] != '#')
	{
		BTNode*root = BuyBTNode(a[*pi]);
		++(*pi);
		root->_left = BinaryTreeCreate(a, n, pi);
		++(*pi);
		root->_right = BinaryTreeCreate(a, n, pi);
		return root;
	}
	else
	{
		return NULL;
	}
}
//void BinaryTreeDestory(BTNode** root)
//{
//
//}
//节点的个数
int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	else
	{
		return BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
	}
}

//叶子节点的个数
int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	if (root->_left == NULL && root->_right == NULL)
	{
		return 1;
	}
	return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
}

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

//查找这个节点是否在这个二叉树中
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	BTNode* ret = 0;
	if (root = NULL)
	{
		return 0;
	}
	if (root->_data == x)
	{
		return root;
	}
	ret = BinaryTreeFind(root->_left, x);
	if (ret)
	{
		return ret;
	}
	ret = BinaryTreeFind(root->_right, x);
	if (ret)
	{
		return ret;
	}
	return NULL;
}

//二叉树的高度
int BinaryTreeHigh(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	else
	{
		return BinaryTreeHigh(root->_left) >= BinaryTreeHigh(root->_right) ?
			BinaryTreeHigh(root->_left) + 1 : BinaryTreeHigh(root->_right) + 1;
	}
}

//是否为完全二叉树
int BinaryTreeComplete(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	BTNode*front;
	if (root)
	{
		QueuePush(&q, root);
	}
	while (QueueEmpty(&q))
	{
		front = QueueFront(&q);
		QueuePop(&q);
		if (front)
		{
			QueuePush(&q, front->_left);
			QueuePush(&q, front->_right);
		}
		else
		{
			break;
		}
	}
	while (QueueEmpty(&q))
	{
		BTNode*front = QueueFront(&q);
		if (front)
		{
			QueueDestory(&q);
			return -1;
		}
		else
		{
			QueuePop(&q);
		}
	}
	return 0;
}




//递归
//前序遍历
void BinaryTreePrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		return;
	}
	printf("%c ", root->_data);
	BinaryTreePrevOrder(root->_left);
	BinaryTreePrevOrder(root->_right);

}
//中序遍历
void BinaryTreeInOrder(BTNode* root)
{
	if (root == NULL)
	{
		return;
	}
	BinaryTreeInOrder(root->_left);
	printf("%c ", root->_data);
	BinaryTreeInOrder(root->_right);


}
//后序遍历
void BinaryTreePostOrder(BTNode* root)
{
	if (root == NULL)
	{
		return;
	}
	BinaryTreePostOrder(root->_left);
	BinaryTreePostOrder(root->_right);
	printf("%c ", root->_data);


}
//层序遍历
void BinaryTreeLevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	if (root)
	{
		QueuePush(&q, root);
	}
	while (QueueEmpty(&q) != 0)
	{
		DataType front = QueueFront(&q);
		printf("%c ", front->_data);
		QueuePop(&q);
		if (front->_left)
		{
			QueuePush(&q, front->_left);
		}
		if (front->_right)
		{
			QueuePush(&q, front->_right);
		}
	}
}


//非递归
//前序遍历
void BinaryTreePrevOrderNonR(BTNode* root)
{
	BTNode* cur = root;
	Stack s;
	BTNode* top;
	StackInit(&s);
	while (cur || StackEmpty(&s) != 0)
	{
		//访问左路节点 左路节点入栈
		while (cur)
		{
			printf("%c ", cur->_data);
			StackPush(&s, cur);
			cur = cur->_left;
		}
		//栈里面出的节点 表示左树已经访问过了
		top = StackTop(&s);
		StackPop(&s);
		//子问题访问右树
		cur = top->_right;
	}
}

//中序遍历
void BinaryTreeInOrderNonR(BTNode* root)
{
	BTNode* cur = root;
	Stack s;
	BTNode* top;
	StackInit(&s);
	while (cur || StackEmpty(&s) != 0)
	{
		while (cur)
		{
			StackPush(&s, cur);
			cur = cur->_left;
		}
		//左树已经访问过了 还剩右树
		top = StackTop(&s);
		StackPop(&s);
		printf("%c ", top->_data);
		cur = top->_right;
	}
}

//后序遍历
void BinaryTreePostOrderNonR(BTNode* root)
{
	BTNode* cur = root;
	BTNode* top;
	BTNode* prev = NULL;
	Stack s;
	StackInit(&s);
	while (cur || StackEmpty(&s) != 0)
	{
		while (cur)
		{
			StackPush(&s, cur);
			cur = cur->_left;
		}
		top = StackTop(&s);
		if (top->_right == NULL || top->_right == prev)
		{
			printf("%c ", top->_data);
			prev = top;
			StackPop(&s);
		}
		else
		{
			cur = top->_right;
		}
	}
}


int main()
{

	char array[] = { 'A', 'B', 'D', '#', '#', '#', 'C',
		'E', '#', '#', 'F', '#', '#' };
	int i = 0;
	BTNode*tree = BinaryTreeCreate(array, sizeof(array) / sizeof(BTDataType), &i);
	printf("结点的个数:%d\n", BinaryTreeSize(tree));
	printf("叶子结点的个数:%d\n", BinaryTreeLeafSize(tree));
	printf("第K层结点的个数:%d\n", BinaryTreeLevelKSize(tree,3));
	printf("数的高度:%d\n", BinaryTreeHigh(tree));
	printf("是否为完全二叉树?:%d\n", BinaryTreeComplete(tree));


	BinaryTreePrevOrder(tree);
	printf("\n");
	BinaryTreeInOrder(tree);
	printf("\n");
	BinaryTreePostOrder(tree);
	printf("\n");
	BinaryTreeLevelOrder(tree);
	printf("\n");




	BinaryTreePrevOrderNonR(tree);
	printf("\n");
	BinaryTreeInOrderNonR(tree);
	printf("\n");
	BinaryTreePostOrderNonR(tree);
	printf("\n");

	return 0;
}

猜你喜欢

转载自blog.csdn.net/Damn_Yang/article/details/84728761