C语言二叉树实现

一、申明变量

typedef char BTDataType;
typedef struct BinaryTreeNode
{
	BTDataType _data;
	struct BinaryTreeNode* _left;//指向左孩子
	struct BinaryTreeNode* _right;//指向右孩子
}BTNode;

二、相关函数

// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int* pi) {
	if (a[*pi] != '#') {
		BTNode* root = (BTNode*)malloc(sizeof(BTNode));
		root->_data = a[*pi];
		(*pi)++;//若不用指针进行前进,则在递归时pi的值不变
		root->_left = BinaryTreeCreate(a, pi);//创建左子树
		(*pi)++;
		root->_right = BinaryTreeCreate(a, pi);//创建右子树
		return root;
	}
	else {
		return NULL;
	}
}
// 二叉树销毁
void BinaryTreeDestory(BTNode** root) {
	if (*root) {
		BinaryTreeDestory(&((*root)->_left));
		BinaryTreeDestory(&((*root)->_right));
		free(*root);
		*root = NULL;
	}
}
// 二叉树节点个数
int BinaryTreeSize(BTNode* root) {
	if (root == NULL) {
		return 0;
	}
	return BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
}
// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root) {
	if (root == NULL) {
		return NULL;
	}
	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);
}
// 二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x) {
	if (root == NULL) {
		return NULL;
	}
	if (root->_data == x) {
		return root;
	}
	BTNode* l = BinaryTreeFind(root->_left, x);
	BTNode* r = BinaryTreeFind(root->_right, x);
	if (l == NULL) {
		return r;
	}
	return l;
}
// 二叉树前序遍历非递归
void BinaryTreePrevOrderNor(BTNode* root) {
	Stack s;
	stackInit(&s, 100);
	BTNode* cur = root;
	BTNode* top;
	while (cur != NULL || stackEmpty(&s) != 1) {
		while (cur != NULL) {
			printf("%c ", cur->_data);
			stackPush(&s, cur);
			cur = cur->_left;
		}
		top = stackTop(&s);
		stackPop(&s);
		cur = top->_right;
	}
	printf("\n");
}
// 二叉树前序遍历
void BinaryTreePrevOrder(BTNode* root)
{
	if (root)
	{ 
		putchar(root->_data);
		BinaryTreePrevOrder(root->_left);
		BinaryTreePrevOrder(root->_right);
	}
}
// 二叉树中序遍历非递归
void BinaryTreeInOrderNor(BTNode* root) {
	BTNode* cur = root;
	BTNode* top;
	Stack s;
	stackInit(&s, 100);
	if (root == NULL) {
		stackDestory(&s);
		return NULL;
	}
	while (cur != NULL || stackEmpty(&s) != 1) {
		while (cur != NULL) {
			//存入栈
			stackPush(&s, cur);
			cur = cur->_left;
		}
		//获取栈顶元素
		top = stackTop(&s);
		printf("%c ", top->_data);
		stackPop(&s);
		//访问右子树
		cur = top->_right;
	}
	printf("\n");
}
//二叉树中序遍历
void BinaryTreeInOrder(BTNode* root)
{
	if (root)
	{
		BinaryTreeInOrder(root->_left);
		putchar(root->_data);
		BinaryTreeInOrder(root->_right);
	}
}
// 二叉树后序遍历非递归
void BinaryTreePostOrderNor(BTNode* root) {
	Stack s;
	stackInit(&s, 100);
	BTNode* prev = NULL;
	BTNode* cur = root;
	BTNode* top = NULL;
	while (cur != NULL || stackEmpty(&s) != 1) {
		while (cur != NULL) {
			//将节点入栈
			stackPush(&s, cur);
			cur = cur->_left;
		}
		top = stackTop(&s);
		if (prev == top->_right || top->_right == NULL) {
			printf("%c ", top->_data);
			prev = top;
			stackPop(&s);
		}
		else {
			cur = top->_right;
		}
	}
	printf("\n");
}
//二叉树后序遍历
void BinaryTreePostOrder(BTNode* root)
{
	if (root)
	{
		BinaryTreePostOrder(root->_left);
		BinaryTreePostOrder(root->_right);
		putchar(root->_data);
	}
}
// 层序遍历
void BinaryTreeLevelOrder(BTNode* root) {
	Queue q;
	queueInit(&q);
	BTNode* cur = root;
	queuePush(&q, cur);
	while (queueEmpty(&q) != 1) {
		cur = queueFront(&q);
		if (cur->_left != NULL) {
			queuePush(&q, cur->_left);
		}
		if (cur->_right != NULL) {
			queuePush(&q, cur->_right);
		}
		printf("%c ", cur->_data);
		queuePop(&q);
	}
	printf("\n");
}
// 判断二叉树是否是完全二叉树
int BinaryTreeComplete(BTNode* root) {
	Queue q;
	queueInit(&q);
	queuePush(&q, root);
	while (!queueEmpty(&q)) {
		BTNode* f = queueFront(&q);
		queuePop(&q);
		if (f != NULL) {
			queuePush(&q, f->_left);
			queuePush(&q, f->_right);
		}
		else {
			break;
		}
	}
	while (!queueEmpty(&q)) {
		BTNode* f = queueFront(&q);
		if (f) {
			return 0;
		}
		else {
			queuePop(&q);
		}
	}
	return 1;
}

三、检验函数

int main() {
	char str[] = "abc##de#g##f###";
	int i = 0;
	BTNode* root = BinaryTreeCreate(str, &i);

	printf("pre前序\n");
	BinaryTreePrevOrderNor(root);

	printf("in中序\n");
	BinaryTreeInOrderNor(root);
	
	printf("post后序\n");
	BinaryTreePostOrderNor(root);
	
	printf("层序IO\n");
	BinaryTreeLevelOrder(root);

	
	i = BinaryTreeLeafSize(root);
	printf("节叶子点数为%d\n", i);

	i = BinaryTreeSize(root);
	printf("总节点数%d\n", i);

	printf("对g进行查找,并将返回结果输出\n");
	BTNode* f = BinaryTreeFind(root, 'g');
	printf("%c\n", f->_data);

	printf("查看是否为完全二叉树,是打印1,不是打印0\n");
	i = BinaryTreeComplete(root);
	printf("%d\n", i);

	BinaryTreeDestory(&root);
	BinaryTreePrevOrderNor(root);

	return 0;
}

C语言二叉树实现_第1张图片
实现的二叉树
C语言二叉树实现_第2张图片

你可能感兴趣的:(c语言,c++,数据结构)