C语言实现二叉树的递归遍历与迭代遍历算法

二叉树的结点

typedef struct BiTNode
{
	int key;
	struct BiTNode* left;//左子树
	struct BiTNode* right;//右子树
} BiTNode, * BiTree;

两个辅助函数

1、创建结点

BiTNode* CreateBiTNode()
{
	BiTNode* node = malloc(sizeof(BiTNode));
	if (node != NULL)
	{
		node->key = 0;
		node->left = NULL;
		node->right = NULL;
	}
	return node;
}

2、添加数据

在结点node上添加数据,如果node为空,则创建结点后再添加数据,返回添加数据后的结点。

BiTNode* InsertBiTNode(BiTNode* node, int key)
{
	if (node == NULL)
		node = CreateBiTNode();
	node->key = key;
	return node;
}

遍历算法

1、递归遍历

(1)先序遍历

void Preorder(BiTree root)
{
	if (root == NULL)
	{
		printf("NULL\n");
		return;
	}
	printf("%d ", root->key);// 根
	if (root->left != NULL)	Preorder(root->left);
	if (root->right != NULL) Preorder(root->right);
}

(2)中序遍历

void Inorder(BiTree root)
{
	if (root == NULL)
	{
		printf("NULL\n");
		return;
	}
	if (root->left != NULL)	Inorder(root->left);
	printf("%d ", root->key);// 根
	if (root->right != NULL) Inorder(root->right);
}

(3)后序遍历

void Postorder(BiTree root)
{
	if (root == NULL)
	{
		printf("NULL\n");
		return;
	}
	if (root->left != NULL)	Postorder(root->left);
	if (root->right != NULL) Postorder(root->right);
	printf("%d ", root->key);// 根
}

2、迭代遍历

(1)先序遍历

void PreorderIteration(BiTree root)
{
	if (root == NULL)
	{
		printf("NULL\n");
		return;
	}

	BiTNode* stack[10] = { NULL };
	int stackTop = -1;

	stack[++stackTop] = root;
	while (stackTop != -1)
	{
		BiTNode* node = stack[stackTop];
		stack[stackTop--] = NULL;
		printf("%d ", node->key);
		if (node->right != NULL)
			stack[++stackTop] = node->right;
		if (node->left != NULL)
			stack[++stackTop] = node->left;
	}
}

(2)中序遍历

void InorderIteration(BiTree root)
{
	if (root == NULL)
	{
		printf("NULL\n");
		return;
	}

	BiTNode* stack[10] = { NULL };
	int stackTop = -1;

	BiTNode* move = root;
	while (stackTop != -1 || move != NULL)
	{
		while (move != NULL)
		{
			stack[++stackTop] = move;
			move = move->left;
		}
		if (stackTop != -1)
		{
			move = stack[stackTop];
			stack[stackTop--] = NULL;
			printf("%d ", move->key);
			move = move->right;
		}
	}
}

(3)后序遍历

void PostorderIteration(BiTree root)
{
	if (root == NULL)
	{
		printf("NULL\n");
		return;
	}

	BiTNode* stack[10] = { NULL };
	int stackTop = -1;

	BiTNode* current = root;
	BiTNode* visited = root;
	while (stackTop != -1 || current != NULL)
	{
		while (current != NULL)
		{
			stack[++stackTop] = current;
			current = current->left;
		}
		current = stack[stackTop];
		if (current->right != NULL && current->right != visited)
		{
			current = current->right;
		}
		else
		{
			printf("%d ", current->key);
			visited = current;
			current = NULL;
			stack[stackTop--] = NULL;
		}
	}
}

测试函数

int main()
{
    // 构建二叉树
	BiTree root = NULL;
	root = InsertBiTNode(root, 10);
	root->left = InsertBiTNode(root->left, 20);
	root->right = InsertBiTNode(root->right, 30);

	BiTNode* left = root->left;
	left->left = InsertBiTNode(left->left, 5);
	left->right = InsertBiTNode(left->right, 8);

	BiTNode* right = root->right;
	right->left = InsertBiTNode(right->left, 35);
	right->right = InsertBiTNode(right->right, 38);

    printf("先序遍历:\n");
	Preorder(root);
    printf("\n");
	PreorderIteration(root);
    printf("\n");

    printf("中序遍历:\n");
	Inorder(root);
    printf("\n");
	InorderIteration(root);
    printf("\n");

    printf("后序遍历:\n");
	Postorder(root);
    printf("\n");
	PostorderIteration(root);
    printf("\n");
	
	if (root != NULL)
		free(root);

	return 0;
}

测试结果

 

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值