二叉树的Morris遍历

二叉树的Morris遍历

之前总结过二叉树的经典遍历算法,包括递归和常规非递归算法,其时间复杂度和空间复杂度均为O(n)。Morris算法巧妙地利用了二叉树的线索化思路,把叶节点的空指针利用起来,将二叉树的遍历算法的空间复杂度降低为O(1),时间复杂度仍然为O(n)。
主要思路:找到每个节点的左子树的最右节点,让它指向本节点。在遍历之后恢复节点的空指针。

文章目录

  • 二叉树的Morris遍历
    • 前序遍历
    • 中序遍历
    • 后序遍历
    • 测试代码

前序遍历

	public static void MorrisPre(Node head) {

		if (head == null) {
			return;
		}
		Node cur1 = head;
		Node cur2 = null;
		while (cur1 != null) {
			cur2 = cur1.left;
			if (cur2 != null) {
				while (cur2.right != null && cur2.right != cur1) {
					cur2 = cur2.right;
				}
				if (cur2.right == null) {
					cur2.right = cur1;
					System.out.print(cur1.value + " "); //打印时机
					cur1 = cur1.left;
					continue;
				} else {
					cur2.right = null;
				}
			} else {
				System.out.print(cur1.value + " "); //打印叶节点
			}
			cur1 = cur1.right;
		}
		System.out.println();
	}

中序遍历

	public static void MorrisIn(Node head) {

		if (head == null) {
			return;
		}

		Node cur1 = head;
		Node cur2 = null;

		while (cur1 != null) {
			cur2 = cur1.left;
			if (cur2 != null) {
				while (cur2.right != null && cur2.right != cur1) {
					cur2 = cur2.right;
				}
				if (cur2.right == null) {
					cur2.right = cur1;
					cur1 = cur1.left;
					continue;
				} else {
					cur2.right = null;
				}
			}
			System.out.print(cur1.value + " ");   //注意不同顺序的不同打印时机
			cur1 = cur1.right;
		}
			System.out.println();
	}

后序遍历

后序遍历较为复杂,需要将每个子树的右边界逆序打印,再更改回来。

	public static void printEdge(Node head) {
		Node Tail = reverseEdge(head);
		Node cur = Tail;
		while (cur != null) {
			System.out.print(cur.value + " ");
			cur = cur.right;
		}
		reverseEdge(Tail);
	}

	public static Node reverseEdge(Node from) {
		Node pre = null;
		Node next = null;
		while (from != null) {
			next = from.right;
			from.right = pre;
			pre = from;
			from = next;
		}
		return pre;
	}

	public static void MorrisPos(Node head) {

		if (head == null) {
			return;
		}
		Node cur1 = head;
		Node cur2 = null;
		while (cur1 != null) {
			cur2 = cur1.left;
			if (cur2 != null) {
				while (cur2.right != null && cur2.right != cur1) {
					cur2 = cur2.right;
				}
				if (cur2.right == null) {
					cur2.right = cur1;
					cur1 = cur1.left;
					continue;
				} else {
					cur2.right = null;
					printEdge(cur1.left);
				}
			}
			cur1 = cur1.right;
		}
		printEdge(head);
		System.out.println();
	}
	

测试代码

	public static void main(String[] args) {
		Node head = new Node(1);
		head.left = new Node(2);
		head.right = new Node(3);
		head.left.left = new Node(4);
		head.left.right = new Node(5);
		head.right.left = new Node(6);
		head.right.right = new Node(7);

		System.out.println("PreOrder: ");
		MorrisPre(head);
		System.out.println("InOrder:");
		MorrisIn(head);
		System.out.println("PosOrder:");
		MorrisPos(head);

	}

你可能感兴趣的:(算法)