【数据结构】二叉树基本操作

栈和队列的相关函数:

栈:https://blog.csdn.net/weixin_41892460/article/details/82973851
队列:https://blog.csdn.net/weixin_41892460/article/details/82973881

BinaryTree.h

#ifndef __BINARYTREE_H__
#define __BINARYTREE_H__

typedef  char  BTDataType;

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




//typedef struct TreeNode 
//{ 
// BTDataType _data; 
// struct TreeNode* _firstChild; 
// struct TreeNode* _nextBrother; 
//}TreeNode; 



typedef  struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;



BTNode* BuyBTNode(BTDataType x);
//a是一个前序遍历的数组
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi);
//遍历   递归非递归
void BinaryTreePrevOrder(BTNode* root);
void BinaryTreeInOrder(BTNode* root);
void BinaryTreePostOrder(BTNode* root);
int BinaryTreeSize(BTNode* root);
int BinaryTreeLeafSize(BTNode* root);
int BinaryTreeKlevelSize(BTNode* root, int k);
void BinaryTreeDestory(BTNode** root);
BTNode*  BinaryTreeFind(BTNode* root, BTDataType x);
int BTreeHeight(BTNode* root);
void BinaryTreeLevelOrder(BTNode* root);
int BinaryTreeComplete(BTNode* root);
void BinaryTreePrevOrderNonR(BTNode* root);
void BinaryTreeInOrderNonR(BTNode* root);
void BinaryTreePostOrderNonR(BTNode* root);
void Test();










#endif

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;
	}
}

//int BinaryTreeSize(BTNode* root)
//{
//	int size = 0;
//	if (root == NULL)
//	{
//		return  0;
//	}
//	++size;
//	BinaryTreeSize(root->left);
//	BinaryTreeSize(root->right);
//}


int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
}


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);
}

BTNode*  BinaryTreeFind(BTNode* root, BTDataType x)
{
	BTNode* ret;
	if (root == NULL || 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 BTreeHeight(BTNode* root)
{
	int leftHeight;
	int rightHeight;
	if (root == NULL)
	{
		return 0;
	}
	leftHeight = BTreeHeight(root->left);
	rightHeight = BTreeHeight(root->right);
	return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}

void BinaryTreeDestory(BTNode** pptree)
{
	BTNode* root = *pptree;
	if (root == NULL)
	{
		return;
	}
	BinaryTreeDestory(&root->left);
	BinaryTreeDestory(&root->right);
	free(root);
	pptree = NULL;
}

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);
}

int BinaryTreeKlevelSize(BTNode* root, int k)
{
	if (root == NULL)
		return 0;
	if (k == 1)
		return 1;
	return BinaryTreeKlevelSize(root->left, k - 1) + BinaryTreeKlevelSize(root->right, k - 1);
}

void BinaryTreeLevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	BTNode *p = NULL;
	if (root == NULL)
		return;
	QueuePush(&q, root);
	while (QueueEmpty(&q) != 0)
	{
		p = QueueFront(&q);
		QueuePop(&q);
		printf("%c", p->data);
		if (p->left)
		{
			QueuePush(&q, p->left);
		}
		if (p->right)
		{
			QueuePush(&q, p->right);
		}
	}
	QueueDestory(&q);
}


int BinaryTreeComplete(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	if (root)
		QueuePush(&q, root);
	while (QueueEmpty(&q) != 0)
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);
		if (front)
		{
			QueuePush(&q, front->left);
			QueuePush(&q, front->right);
		}
		else
		{
			break;
		}
	}
	while (QueueEmpty(&q) != 0)
	{
		BTNode* front = QueueFront(&q);
		if (front)
		{
			return -1;
		}
		else
		{
			QueuePop(&q);
		}
	}
	QueueDestory(&q);
	return 0;
}
void BinaryTreePrevOrderNonR(BTNode* root)
{
	BTNode* cur = root;
	Stack s;
	StackInit(&s);
	while (cur || StackEmpty(&s) != 0)
	{
		//访问左路节点,左路节点进栈
		while (cur)
		{
			printf("%c", cur->data);
			StackPush(&s, cur);
			cur = cur->left;
		}
		//栈里面出来节点,表示左树已经访问过
		BTNode* top = StackTop(&s);
		StackPop(&s);
		//子问题访问有树
		cur = top->right;
	}
}
void BinaryTreeInOrderNonR(BTNode* root)
{
	BTNode* cur = root;
	Stack s;
	StackInit(&s);
	while (cur || StackEmpty(&s) != 0)
	{
		while (cur)
		{
			StackPush(&s, cur);
			cur = cur->left;
		}
		//左树已经访问过了
		//还剩根及右树
		BTNode* top = StackTop(&s);
		StackPop(&s);
		printf("%c", top->data);
		cur = top->right;
	}
}
void BinaryTreePostOrderNonR(BTNode* root)
{
	BTNode* cur = root;
	BTNode* prev = NULL;
	Stack s;
	StackInit(&s);
	while (cur || StackEmpty(&s) != 0)
	{
		while (cur)
		{
			StackPush(&s, cur);
			cur = cur->left;
		}
		BTNode* top = StackTop(&s);
		if (top->right == NULL || top->right == prev)
		{
			printf("%c", top->data);
			prev = top;
			StackPop(&s);
		}
		else
		{
			cur = top->right;
		}
	}
}

Test.c

#include "BinaryTree.h"

void Test()
{
	char array[] = { 'A', 'B', 'D', '#', '#', '#', 'C',
		'E', '#', '#', 'F', '#', '#' };
	size_t i = 0;
	BTNode* tree = BinaryTreeCreate(array, sizeof(array) / sizeof(BTDataType), &i);
	/* BinaryTreeDestory(&tree);*/
	BinaryTreePrevOrder(tree);
	printf("\n");
	BinaryTreePrevOrderNonR(tree);
	printf("\n");
	BinaryTreeInOrder(tree);
	printf("\n");
	BinaryTreeInOrderNonR(tree);
	printf("\n");
	BinaryTreePostOrder(tree);
	printf("\n");
	BinaryTreePostOrderNonR(tree);
	printf("\n");

	/*printf("%d", BinaryTreeSize(tree));*/
	/*printf("%d", BinaryTreeLeafSize(tree));*/
	/*printf("%d", BinaryTreeKlevelSize(tree, 1));*/
	/* BinaryTreeLevelOrder(tree);*/
	/*printf("%d", BTreeHeight(tree));*/
	/*printf("%d", BinaryTreeComplete(tree));*/
}
int main()
{
	Test();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值