二叉树先序、中序、后序的递归和非递归实现

1 二叉树的创建

今天看了一下二叉树,突然发现书上讲了它的遍历方式,却没有讲如何创建二叉树,如果不先创建的话,我感觉即使写出了遍历的代码,也没法遍历啊。真的是觉得很奇怪!这里附上我写的代码,首先我这棵树我是用数组先存的,按照完全二叉树存的,没有节点的地方值为0
结构如下:
在这里插入图片描述

	int[] arr = { 1, 2, 3, 4, 5, 0, 0, 0, 0, 6, 7 };
	int index = 0;
	//测试用的方法
	@Test 
	public void test() {
		TreeNode root = null;
		root = CreateTree(arr, 0);
		System.out.println("先序遍历");
		preOrderRecur(root);
		System.out.println();
		System.out.println("中序遍历");
		inOrderRecur(root);
		System.out.println();
		System.out.println("后序遍历");
		posOrderRecur(root);
	}
	// 创建树
	public TreeNode CreateTree(int[] arr, int index) {
		TreeNode node = null;
		if (index < arr.length && arr[index] != 0) {
			node = new TreeNode(arr[index]);
			node.setLeft(CreateTree(arr, 2 * index + 1));
			node.setRight(CreateTree(arr, 2 * index + 2));
		}
		return node;
	}

树的定义

public class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x) {
		val = x;
	}
//省略了get和set方法
}

2先序遍历

2.1 先序遍历的递归实现

先读取根节点的值,然后再递归的遍历左子树和又子树,就是先序遍历

	// 先序遍历树的递归算法
	public void preOrderRecur(TreeNode head) {
		if (head == null) {
			return;
		}
		System.out.print(head.val + "  ");
		preOrderRecur(head.left);
		preOrderRecur(head.right);
	}

2.2 先序遍历非递归实现

用非递归的过程的具体过程如下:

  1. 申请一个新栈,记为stack,然后将头节点head压入stack中。
  2. 从stack中弹出新节点,记为cur,然后打印cur节点的值,再将cur节点的右孩子(如果不为空的话)先压入栈中,最后将cur的左孩子(不为空的话)压入栈中。
  3. 不断重复步骤2,直到stack为空,全部过程结束
    具体代码如下:
// 先序遍历的非递归算法
	public void preOrderUnRecur(TreeNode head) {
		if (head != null) {
			Stack<TreeNode> stack = new Stack<TreeNode>();
			stack.add(head);
			while (!stack.isEmpty()) {
				head = stack.pop();
				System.out.print(head.val + "  ");
				if (head.right != null) {
					stack.push(head.right);
				}
				if (head.left != null) {
					stack.push(head.left);
				}
			}
		}
	}

3 中序遍历

3.1 中序遍历的递归算法
// 中序遍历树的递归算法
public void inOrderRecur(TreeNode head) {
		if (head == null) {
			return;
		}
		inOrderRecur(head.left);
		System.out.print(head.val + "  ");
		inOrderRecur(head.right);
	}


3.2 中序遍历的非递归算法

具体过程如下:

  1. 申请一个新的栈,记为stack,初始时,令变量cur=head
  2. 先把cur节点压入栈中,对以cur节点为头的整棵子树来说,一次把左边界压入栈中,即不停的令cur=cur.left,然后重复步骤2
  3. 不断重复步骤2,直到发现cur为空,此时从stack中弹出一个节点,记为node,打印node的值,并且让cur=nod.ight,然后重复步骤2,
  4. 当stack为空且cur为空时,整个过程停止。
    具体代码如下:
// 中序遍历的非递归算法
	public void inOrderUnRecur(TreeNode head) {
		if (head != null) {
			Stack<TreeNode> stack = new Stack<TreeNode>();
			while (!stack.isEmpty() || head != null) {
				if (head != null) {
					stack.push(head);
					head = head.left;
				} else {
					head = stack.pop();
					System.out.print(head.val + "  ");
					head = head.right;
				}
			}
		}
	}

4 后序遍历

4.1 后序遍历的递归算法
// 后序遍历树的递归算法
	public void posOrderRecur(TreeNode head) {
		if (head == null) {
			return;
		}
		posOrderRecur(head.left);
		posOrderRecur(head.right);
		System.out.print(head.val + "  ");
	}

4.2 后序遍历的非递归算法

后序遍历的非递归实现方法,有很多种,这里记录用两个栈实现的,我感觉一个栈实现的有点难,实际上还是自己太菜
具体过程如下:

  1. 申请两个栈,记为s1和s2,然后将头节点head压入s1中。
  2. 从s1中弹出的节点记为cur,然后依次将cur的左孩子和右孩子压入s1中。
  3. 在整个过程中,每一个从s1中弹出的节点都放进s2中。
  4. 不断重复步骤2和步骤3,直到s1为空,过程停止
  5. 从s2中依次弹出节点并打印,打印的顺序就是后序遍历的顺序

具体实现代码如下:

// 后序遍历的非递归算法
	public void posOrderUnRecur(TreeNode head) {
		if (head != null) {
			Stack<TreeNode> s1 = new Stack<TreeNode>();
			Stack<TreeNode> s2 = new Stack<TreeNode>();
			s1.push(head);
			while (!s1.isEmpty()) {
				head = s1.pop();
				s2.push(head);
				if (head.left != null) {
					s1.push(head.left);
				}
				if (head.right != null) {
					s1.push(head.right);
				}
			}
			while (!s2.isEmpty()) {
				System.out.print(s2.pop().val + "  ");
			}
		}
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Master_Yoda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值