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

项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

二叉树是一种非常重要的数据结构,也是平时面试的时候面试官喜欢出的问题之一。关于二叉树的概念,就不做过多解释,估计各种课本网络上各种资料都充斥着关于二叉树的原理介绍。我们是实战派,原理不在啰嗦,重点看代码,看看怎样实现一棵二叉树,并分别用实现先序遍历,中序遍历以及后续遍历。

package leilei.bit.edu.tree;

import java.util.Stack;

public class BinTree<T> {
	
	private Node<T> root;
	
	public BinTree(Node<T> root) {
		this.root = root;
	}
	
	public Node<T> getRoot() {
		return root;
	}

	public void setRoot(Node<T> root) {
		this.root = root;
	}

	//内部节点类
	private static class Node<T>
	{
		public T data;
		public Node<T> left;
		public Node<T> right;
		
		public Node(T data,Node<T> left,Node<T> right) {
			this.data = data;
			this.left = left;
			this.right = right;
		}
	}
	
	/*
	 * 递归先序遍历
	 */
	public void preorder(Node<T> root) {
		if (root != null) {
			System.out.print(root.data + " ");
			preorder(root.left);
			preorder(root.right);
		}
	}

	/*
	 * 递归中序遍历
	 */
	public void midorder(Node<T> root) {
		if (root != null) {
			midorder(root.left);
			System.out.print(root.data + " ");
			midorder(root.right);
		}
	}
	
	/*
	 * 递归后序遍历
	 */
	public void postorder(Node<T> root) {
		if (root != null) {
			postorder(root.left);
			postorder(root.right);
			System.out.print(root.data + " ");
		}
	}
	
	
	/*
	 * 非递归先序
	 */
	public void preorder_no_recursive(Node<T> root) {
		if (root == null)
			return;
		Stack<Node<T>> stack = new Stack<Node<T>>();
		
		stack.push(root);
		while(!stack.isEmpty()) {
			while(stack.peek() != null) {
				System.out.print(stack.peek().data + " ");
				stack.push(stack.peek().left);
			}
			Node<T> p = stack.pop();
			
			if(!stack.isEmpty()) {
				p = stack.pop();
				stack.push(p.right);
			}
		}
	}
	
	public static void main(String[] args) {
		Node<String> G = new Node<String>("G",null,null);
		Node<String> F = new Node<String>("F",null,null);
		Node<String> E = new Node<String>("E",null,null);
		Node<String> D = new Node<String>("D",F,null);
		Node<String> B = new Node<String>("B",D,E);
		Node<String> C = new Node<String>("C",null,G);
		Node<String> root = new Node<String>("A",B,C);
		
		BinTree<String> tree = new BinTree<String>(root);
		
		System.out.println("The preorder is:");
		tree.preorder(tree.root);
		
		System.out.println("\nThe midorder is:");
		tree.midorder(tree.root);
		
		System.out.println("\nThe postorder is:");
		tree.postorder(tree.root);
		
		System.out.println("\nThe no_recursive is:");
		tree.preorder_no_recursive(tree.root);
	}
}

代码运行结果

The preorder is:
A B D F E C G 
The midorder is:
F D B E A C G 
The postorder is:
F D E B G C A 
The no_recursive is:
A B D F E C G 

另外发现一个写二叉树不错的链接,有空可以详细看看里面的代码:
https://github.com/yuzhangcmu/LeetCode/blob/master/tree/TreeDemo.java

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值