树的遍历算法总结

树的遍历算法

  1. 先序
  2. 中序
  3. 后序
    递归 和 非 递归 算法
    【注, 后序的非递归算法 和 先序 思路相反】

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class TraverseTree {

	// 先序遍历, 非递归
	// 利用堆栈
	public static void preOrderUnRecur(Node head) {
		System.out.print("先序 非递归: ");
		if (head != null) {
			Stack<Node> stack = new Stack<Node>();
			stack.push(head);
			while (!stack.isEmpty()) {
				head = stack.pop();
				System.out.print(head.data + " ");
				if (head.right != null) {   // 有右先压右
					stack.push(head.right);
				}
				if (head.left != null) {    // 有左后压左
					stack.push(head.left);
					
				}
			}
		}
		System.out.println();
	}
	
	// 层序遍历   非递归
	//利用队列
	public static void seqOrdUnRecur(Node head) {
		System.out.print("层序 非递归: ");
		if (head != null) {
			Queue<Node> queue = new LinkedList<Node>();
			queue.add(head);  // 
			while (!queue.isEmpty()) {  // 队列为空时,结束遍历
				head = queue.poll();  // 出队列
				System.out.print(head.data + " ");
				// 左右孩子分别入队列
				if(head.left != null) {  
					queue.add(head.left);  
				}
				if(head.right != null) {  
					queue.add(head.right);  
				}
			}
		}
		System.out.println();
	}
	
	// 先序遍历 递归
	public static void preOrderRecur(Node head) {
		if (head == null) {
			return;
		}
		System.out.print(head.data + " ");
		preOrderRecur(head.left);
		preOrderRecur(head.right);
	}
	
	// 中序遍历 递归
	public static void inOrderRecur(Node head) {
		if (head == null) {
			return;
		}
		inOrderRecur(head.left);
		System.out.print(head.data + " ");
		inOrderRecur(head.right);
	}

	// 后序遍历 递归
	public static void posOrderRecur(Node head) {
		if (head == null) {
			return;
		}
		posOrderRecur(head.left);
		posOrderRecur(head.right);
		System.out.print(head.data + " ");
	}

}

posted @ 2019-12-10 17:41  JimmyYang_MJ  阅读(236)  评论(0编辑  收藏  举报