二叉树(C语言实现)超级简洁版

数据结构设计

//树的节点
typedef struct tree_node
{
	struct tree_node* left;
	struct tree_node* right;
	char key;
}tree_node;

接口设计

//创建一个节点
tree_node* tree_create_node(char key);
//创建一颗二叉树
tree_node* tree_create();

//前序遍历 |递归式|(中->左->右)
void pre_order_traverse1(tree_node* node);
//前序遍历 |非递归式|(中->左->右)
void pre_order_traverse2(tree_node* node);

//中序遍历 |递归式|(左->中->右)
void in_order_traverse1(tree_node* node);
//中序遍历 |非递归式|(左->中->右)
void in_order_traverse2(tree_node* node);

//后序遍历 |递归式|(左->右->中)
void post_order_traveser1(tree_node* node);
//后序遍历 |非递归式|(左->右->中)(先按中->右->左的顺序入输出栈,再从输出栈输出)
void post_order_traveser2(tree_node* node);

//层次遍历
void level_traverse(tree_node* root);

测试代码

int main()
{
	/* A-B-D-#-#-E-#-#-C-F-#-#-G-#-# */
	tree_node* tree = tree_create();

	printf("\n前序遍历1:");
	pre_order_traverse1(tree);

	printf("\n前序遍历2:");
	pre_order_traverse2(tree);

	printf("\n中序遍历1:");
	in_order_traverse1(tree);

	printf("\n中序遍历2:");
	in_order_traverse2(tree);

	printf("\n后序遍历1:");
	post_order_traveser1(tree);

	printf("\n后序遍历2:");
	post_order_traveser2(tree);

	printf("\n层次遍历:");
	level_traverse(tree);

	printf("\n");
	return 0;
}

接口实现:

这里在进行非递归遍历时可以使用之前自己实现的stack和queue的数据结构,并且不会有兼容性之类的报错。不过我这人挺作的,于是在此处使用了C++的stack和queue的模板库,在使用过程中遇到并学习然后解决了一些未曾注意到的细节性的知识,有兴趣可以自己尝试一下。

//创建一个节点
tree_node* tree_create_node(char key)
{
	tree_node* node = (struct tree_node*)malloc(sizeof(struct tree_node));
	if (node == NULL)
		return NULL;
	node->key = key;
	node->left = NULL;
	node->right = NULL;

	return node;
}

//创建一颗二叉树
tree_node* tree_create()
{
	char str;
	tree_node* cur;

	scanf_s("%c", &str,1);
	if (str == '#')
		cur = NULL;
	else
	{
		cur = tree_create_node(str);
		cur->left = tree_create();
		cur->right = tree_create();
	}
	return cur;
}

//前序遍历 |递归式|(中->左->右)
void pre_order_traverse1(tree_node* node)
{
	if (node!=NULL)
	{
		printf("%c\t", node->key);
		pre_order_traverse1(node->left);
		pre_order_traverse1(node->right);
	}
}
//前序遍历 |非递归式|(中->左->右)
void pre_order_traverse2(tree_node* node)
{
	std::stack<tree_node*>stack;
	tree_node* cur = node;

	while (cur!=NULL||!stack.empty())
	{
		if (cur!=NULL)
		{
			printf("%c\t", cur->key);
			stack.push(cur);
			cur = cur->left;
		}
		else
		{
			cur = std::move(stack.top());
			stack.pop();
			cur = cur->right;
		}//end_if
	}//end_while
	return;
}


//中序遍历 |递归式|(左->中->右)
void in_order_traverse1(tree_node* node)
{
	if (node != NULL)
	{
		in_order_traverse1(node->left);
		printf("%c\t", node->key);
		in_order_traverse1(node->right);
	}
	return;
}

//中序遍历 |非递归式|(左->中->右)
void in_order_traverse2(tree_node* node)
{
	std::stack<tree_node*>stack;
	tree_node* cur = node;
	while (cur != NULL || !stack.empty())
	{
		if (cur != NULL)
		{
			stack.push(cur);
			cur = cur->left;
		}
		else
		{
			cur = std::move(stack.top());
			stack.pop();
			printf("%c\t", cur->key);
			cur = cur->right;
		}//end_if
	}//end_while
	return;
}

//后序遍历 |递归式|(左->右->中)
void post_order_traveser1(tree_node* node)
{
	if (node != NULL)
	{
		post_order_traveser1(node->left);
		post_order_traveser1(node->right);
		printf("%c\t", node->key);
	}
	return;
}

//后序遍历 |非递归式|(左->右->中)(先按中->右->左的顺序入输出栈,再从输出栈输出)
void post_order_traveser2(tree_node* node)
{
	std::stack<tree_node*>stack;
	std::stack<char>print;
	tree_node* cur = node;
	while (cur != NULL || !stack.empty())
	{
		if (cur != NULL)
		{
			print.push(cur->key);
			stack.push(cur);
			cur = cur->right;
		}
		else
		{
			cur = std::move(stack.top());
			stack.pop();
			cur = cur->left;
		}//end_if
	}//end_while

	while (!print.empty())
	{
		printf("%c\t", print.top());
		print.pop();
	}//end_while
}

//层次遍历
void level_traverse(tree_node* root)
{
	std::queue<tree_node*>queue;
	
	if (root != NULL)
		queue.push(root);

	while (!queue.empty())
	{
		tree_node* cur = queue.front();
		printf("%c\t", cur->key);
		queue.pop();

		if(cur->left!=NULL)
			queue.push(cur->left);
		if (cur->right != NULL)
			queue.push(cur->right);
	}//end_while

	return;
}

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