二叉树的遍历(含代码)

  •                                             **二叉树的遍历**                                           
    

建立二叉树时,使用递归思想,若为‘ ’则为NULL,节点不存在,否则开辟新空间,存入数据
若为先序 则先存,后左右
若为中序 先左 再存 后右
若为后序 先左右 再存

后序遍历,使用递归思想,先一直访问左,再一直访问右边,最后打印根数据

非递归遍历,中序输出,中序为 左根右 需要借助栈 先访问左直至空 如果栈不为空,出栈,打印数据 再访问右节点

按层次遍历时,借助队列,,先让根入队 再进入循环 出队 每次出队时让左右节点入队,同时打印当前节点数据即可

代码如下

#include <stdio.h>
#include <stdlib.h>
struct tree* stact [50];
int cnt = -1;
int tail = 0,head=0;
struct tree
{
	char date;
	struct tree* lchild;
	struct tree* rchild;
};
struct tree* creattree(struct tree* node);
void Postorder(struct tree* node);
void Middleorder(struct tree* node);
void Push(struct tree* node);
struct tree* Pop();
void Join(struct tree* node);
struct tree* out();
void arrangement(struct tree* node);
int main()
{
	struct tree *root=NULL;
	printf("请以先序输入数据:\n");
	root=creattree(root);
	printf("\n");
	printf("按后序遍历打印输出:\n");
	Postorder(root);
	printf("\n");
	printf("按中序遍历打印输出:\n");
	Middleorder(root);
	printf("\n");
	printf("按层次遍历打印输出:\n");
	arrangement(root);
	
}
struct tree* creattree(struct tree* node)
{
	char temp;
	temp = getchar();
	if (temp == ' ')
	{
		node = NULL;
	}
	else
	{
		node = (struct tree*)malloc(sizeof(struct tree));
		node->date = temp;
		node->lchild=creattree(node->lchild);
		node->rchild=creattree(node->rchild);
	}
	return node;
}
void Postorder(struct tree* node)
{
	if (node != NULL)
	{
		Postorder(node->lchild);
		Postorder(node->rchild);
		printf("%c",node->date);
	}
}
void Middleorder(struct tree* node)
{
	struct tree *temp = node;
	while (temp!= NULL ||cnt!=-1)
	{
		while (temp)
		{
			Push(temp);
			temp = temp->lchild;
		}
		if (cnt != -1)
		{
			temp = Pop();
			printf("%c", temp->date);
			temp = temp->rchild;
		}
	}
}
void Push(struct tree* node)
{
	stact[++cnt] = node;
}
struct tree* Pop()
{
	return stact[cnt--];
}
void Join(struct tree* node)
{
	stact[tail++] = node;
}
struct tree* out()
{
	return stact[head++];
}
void arrangement(struct tree* node)
{
	struct tree* temp;
	temp = node;
	Join(temp);
	while (head != tail)
	{
		temp = out();
		printf("%c", temp->date);
		if (temp->lchild != NULL)
		{
			Join(temp->lchild);
		}
		if (temp->rchild != NULL)
		{
			Join(temp->rchild);
		}
	}
}

运行实例
运行截图

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是使用C语言实现二叉树遍历的完整代码,包括二叉树结构体定义、创建二叉树、三种遍历方式的递归实现和非递归实现。 ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <stddef.h> // 二叉树结构体 typedef struct TreeNode { int val; // 节点值 struct TreeNode* left; // 左子树指针 struct TreeNode* right; // 右子树指针 } TreeNode; // 创建二叉树 TreeNode* createTree() { int val; scanf("%d", &val); if (val == -1) { // 输入-1表示当前节点为null return NULL; } TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = val; root->left = createTree(); // 创建左子树 root->right = createTree(); // 创建右子树 return root; } // 前序遍历递归实现 void preorderTraversalRecursive(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->val); preorderTraversalRecursive(root->left); preorderTraversalRecursive(root->right); } // 前序遍历非递归实现 void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } TreeNode* stack[100]; // 定义一个栈,用于存储节点 int top = -1; // 栈顶指针 stack[++top] = root; // 根节点入栈 while (top >= 0) { TreeNode* node = stack[top--]; // 出栈 printf("%d ", node->val); // 访问根节点 if (node->right != NULL) { // 先将右子树入栈 stack[++top] = node->right; } if (node->left != NULL) { // 再将左子树入栈 stack[++top] = node->left; } } } // 中序遍历递归实现 void inorderTraversalRecursive(TreeNode* root) { if (root == NULL) { return; } inorderTraversalRecursive(root->left); printf("%d ", root->val); inorderTraversalRecursive(root->right); } // 中序遍历非递归实现 void inorderTraversal(TreeNode* root) { if (root == NULL) { return; } TreeNode* stack[100]; int top = -1; TreeNode* node = root; while (node != NULL || top >= 0) { while (node != NULL) { stack[++top] = node; // 将左子树入栈 node = node->left; } node = stack[top--]; // 出栈 printf("%d ", node->val); // 访问根节点 node = node->right; // 访问右子树 } } // 后序遍历递归实现 void postorderTraversalRecursive(TreeNode* root) { if (root == NULL) { return; } postorderTraversalRecursive(root->left); postorderTraversalRecursive(root->right); printf("%d ", root->val); } // 后序遍历非递归实现 void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } TreeNode* stack[100]; int top = -1; TreeNode* node = root; TreeNode* last_visited = NULL; // 上次访问的节点 while (node != NULL || top >= 0) { while (node != NULL) { stack[++top] = node; node = node->left; } node = stack[top]; if (node->right == NULL || node->right == last_visited) { printf("%d ", node->val); // 访问根节点 top--; last_visited = node; node = NULL; } else { node = node->right; // 访问右子树 } } } int main() { TreeNode* root = createTree(); // 创建二叉树 printf("前序遍历(递归):"); preorderTraversalRecursive(root); // 前序遍历(递归) printf("\n前序遍历(非递归):"); preorderTraversal(root); // 前序遍历(非递归) printf("\n中序遍历(递归):"); inorderTraversalRecursive(root); // 中序遍历(递归) printf("\n中序遍历(非递归):"); inorderTraversal(root); // 中序遍历(非递归) printf("\n后序遍历(递归):"); postorderTraversalRecursive(root); // 后序遍历(递归) printf("\n后序遍历(非递归):"); postorderTraversal(root); // 后序遍历(非递归) return 0; } ``` 运行程序后,可以输入二叉树的节点值,用-1表示当前节点为null,例如: 输入: ``` 1 2 4 -1 -1 5 -1 -1 3 -1 6 -1 -1 ``` 输出: ``` 前序遍历(递归):1 2 4 5 3 6 前序遍历(非递归):1 2 4 5 3 6 中序遍历(递归):4 2 5 1 3 6 中序遍历(非递归):4 2 5 1 3 6 后序遍历(递归):4 5 2 6 3 1 后序遍历(非递归):4 5 2 6 3 1 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值