java实现二叉树的先中后序遍历

标题:java实现二叉树的先中后序遍历

package com.hhh.aa.basicTreeBST03;

import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import org.junit.Test;

import com.hhh.aa.basicTree02.TreeNode;

/**
 * 测试先中后序遍历
 * 同时先中序还使用了后序的
 * @author dell
 *
 */
public class TestPreInPostOrder {
	/**
	 * 测试先序遍历
	 */
	//方法一:递归
	public void preOrder(TreeNode head) {
		if(head == null) {
			return ;
		}else {
			System.out.print(head.val + " ");
			this.preOrder(head.left);
			this.preOrder(head.right);
		}
	}
	
	//方法二:迭代,删除后,放right, left
	public void preOrder02(TreeNode head) {
		System.out.println("方法二:迭代,删除后,放right, left");
		if(head == null) {
			return ;
		}
		Deque<TreeNode> s = new LinkedList<>();
		s.push(head);
		
		while(!s.isEmpty()) {
			TreeNode p = s.pop();
			System.out.print(p.val + " ");
			if(p.right != null) {
				s.push(p.right);
			}
			if(p.left != null) {
				s.push(p.left);
			}
		}
		
	}
	//迭代,先中通用的
	public void preOrder03(TreeNode head) {
		System.out.println("迭代,先中通用的");
		if(head == null) {
			return ;
		}
		Deque<TreeNode> s = new LinkedList<>();
		s.push(head);
		
		while(!s.isEmpty()) {
			TreeNode p = s.peek();
			System.out.print(p.val + " ");
			if(p.left != null) {
				s.push(p.left);
			}else {
				p = s.pop();
				while(p.right == null && !s.isEmpty()) {
					p = s.pop();
				}
				if(p.right != null) {
					s.push(p.right);
				}else {
					break;
				}
			}
		}
	}
	//迭代,后序版的
	public void preOrder04(TreeNode head) {
		System.out.println("迭代,后序版的");
		if(head == null) {
			return ;
		}
		Deque<TreeNode> s = new LinkedList<>();
		s.push(head);
		
		while(!s.isEmpty()) {
			TreeNode p = s.peek();
			System.out.print(p.val + " "); //新增的
			if(p.left != null) {
				s.push(p.left);
			}else {
				while(p.right == null) {
					TreeNode last = s.pop();
//					System.out.print(last.val + " ");
					while(!s.isEmpty() && s.peek().right == last) {
						last = s.pop();
//						System.out.print(last.val + " ");
					}
					if(s.isEmpty()) {
						break;
					}
					p = s.peek();
				}
				if(s.isEmpty()) {
					break;
				}else {
					s.push(p.right);
				}
			}
		}
	}
	
	/**
	 * 测试中序序遍历
	 */
	//方法一:递归
	public void inOrder(TreeNode head) {
		if(head == null) {
			return ;
		}else {
			this.inOrder(head.left);
			System.out.print(head.val + " ");
			this.inOrder(head.right);
		}
	}
	
	//迭代,先中通用的
	public void inOrder03(TreeNode head) {
		System.out.println("迭代,先中通用的");
		if(head == null) {
			return ;
		}
		Deque<TreeNode> s = new LinkedList<>();
		s.push(head);
		
		while(!s.isEmpty()) {
			TreeNode p = s.peek();
			if(p.left != null) {
				s.push(p.left);
			}else {
				p = s.pop();
				System.out.print(p.val + " ");
				while(p.right == null && !s.isEmpty()) {
					p = s.pop();
					System.out.print(p.val + " ");
				}
				if(p.right != null) {
					s.push(p.right);
				}else {
					break;
				}
			}
		}
	}
	//迭代,后序版的
	public void inOrder04(TreeNode head) {
		System.out.println("迭代,后序版的");
		if(head == null) {
			return ;
		}
		Deque<TreeNode> s = new LinkedList<>();
		s.push(head);
		
		while(!s.isEmpty()) {
			TreeNode p = s.peek();			
			if(p.left != null) {
				s.push(p.left);
			}else {
				System.out.print(p.val + " ");//增加的
				while(p.right == null) {
					TreeNode last = s.pop();
//					System.out.print(last.val + " ");
					while(!s.isEmpty() && s.peek().right == last) {
						last = s.pop();
//						System.out.print(last.val + " ");
					}
					if(s.isEmpty()) {
						break;
					}
					p = s.peek();
					System.out.print(p.val + " ");//增加的
				}
				if(s.isEmpty()) {
					break;
				}else {
					s.push(p.right);
				}
			}
		}
	}
	
	/**
	 * 测试后序遍历
	 */
	//方法一:递归
	public void postOrder(TreeNode head) {
		if(head == null) {
			return ;
		}else {
			this.postOrder(head.left);
			this.postOrder(head.right);
			System.out.print(head.val + " ");
		}
	}
	
	//迭代,后序版的
	public void postOrder04(TreeNode head) {
		System.out.println("迭代,后序版的");
		if(head == null) {
			return ;
		}
		Deque<TreeNode> s = new LinkedList<>();
		s.push(head);
		
		while(!s.isEmpty()) {
			TreeNode p = s.peek();			
			if(p.left != null) {
				s.push(p.left);
			}else {
				while(p.right == null) {
					TreeNode last = s.pop();
					System.out.print(last.val + " ");
					while(!s.isEmpty() && s.peek().right == last) {
						last = s.pop();
						System.out.print(last.val + " ");
					}
					if(s.isEmpty()) {
						break;
					}
					p = s.peek();
				}
				if(s.isEmpty()) {
					break;
				}else {
					s.push(p.right);
				}
			}
		}
	}
	
	/**
	 * 初始化一个tree
	 * 类广度遍历
	 * @param a
	 * @return
	 */
	public TreeNode initTree(Integer[] a) {
		if(a == null || a.length == 0) {
			return null;
		}
		
		int t = 0;
		TreeNode p = new TreeNode(a[t]);  //至少有一个元素
		Queue<TreeNode> q = new LinkedList<>();
		q.offer(p);
		
		while(!q.isEmpty()) {
			TreeNode node = q.poll();
			if(t + 1 == a.length) {  //先判断数组中是否还有下一个元素
				return p;
			}else {
				t++;
				if(a[t] == null) {  //若下一个元素为null,则不需要创建新的节点
					node.left = null;
				}else {
					node.left = new TreeNode(a[t]);
					q.offer(node.left);
				}
			}
			if(t + 1 == a.length) {
				return p;
			}else {
				t++;
				if(a[t] != null){  //上面的简写,a[t] == null,不需要再赋值
					node.right = new TreeNode(a[t]);
					q.offer(node.right);
				}
			}
		}
		
		return p;
	}
	
	@Test
	public void test() {
		Integer[] a = new Integer[] {1, 2, 3, 4, 5, 9, 10, null, 6, 7, 8, null, null, null, 11};
		
		TreeNode head = this.initTree(a);
		
		System.out.println("先序----------");
		System.out.println("方法一:递归:");
		this.preOrder(head);
		System.out.println();

		this.preOrder02(head);
		System.out.println();

		this.preOrder03(head);
		System.out.println();

		this.preOrder04(head);
		System.out.println();
		
		System.out.println("中序----------");
		System.out.println("递归");
		this.inOrder(head);
		System.out.println();
		
		this.inOrder03(head);
		System.out.println();
		
		this.inOrder04(head);
		System.out.println();
		
		System.out.println("后序-----------");
		System.out.println("递归");
		this.postOrder(head);
		System.out.println();
		
		this.postOrder04(head);
		System.out.println();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值