递归 非递归 遍历二叉树

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

二叉树的重要性,想必大家都非常清楚。在数据结构中,二叉树是一种非常重要,也非常基础的非线性结构。

遍历是二叉树最基础也是最重要的操作了。最常见的分为前序遍历,中序遍历,后续遍历。废话不多说,先直接上代码。

package tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class BinTree {
	
	public int[] array = {1,2,3,4,5,6,7,8,9};
	public List<Node> nodelist = null;
	
	public  class Node {
		Node leftChild;
		Node rightChild;
		int data;
		
		Node(int data) {
			this.data = data;
			this.leftChild = null;
			this.rightChild = null;
		}
	}
	
	public void createBinTree() {
		nodelist = new ArrayList<Node>();
		for(int index=0; index<array.length; index++) {
			nodelist.add(new Node(array[index]));
		}
		
		//此数组长度为9,父节点有(9/2-1=3)个。先对前n-1个父节点建立子节点
		for(int parentIndex=0; parentIndex<array.length/2 - 1; parentIndex++) {
			nodelist.get(parentIndex).leftChild = nodelist.get(parentIndex*2 + 1);
			nodelist.get(parentIndex).rightChild = nodelist.get(parentIndex*2 + 2);
		}
		//最后一个父节点有可能没有右孩子。只有数组长度为奇数的时候才有右孩子
		int lastParent = array.length / 2 - 1;
		nodelist.get(lastParent).leftChild = nodelist.get(lastParent*2 + 1);
		if(array.length % 2 == 1) {
			nodelist.get(lastParent).rightChild = nodelist.get(lastParent*2 + 2);
		}
	}
	
	public void preOrderTraverse(Node node) {
		if(node == null) {
			return;
		}
		System.out.print(node.data + " ");
		preOrderTraverse(node.leftChild);
		preOrderTraverse(node.rightChild);
	}
	
	public void inOrderTraverse(Node node) {
		if(node == null) {
			return;
		}
		inOrderTraverse(node.leftChild);
		System.out.print(node.data + " ");
		inOrderTraverse(node.rightChild);
	}
	
	public void lastOrderTraverse(Node node) {
		if (node == null) {
			return;
		}
		lastOrderTraverse(node.leftChild);
		lastOrderTraverse(node.rightChild);
		System.out.print(node.data + " ");
		
	}
	
	public void norecursivePre(Node node) {
		/*
		 * 用栈模拟递归的方式
		 */
		Stack<Node> stack = new Stack<Node>();
		if(node != null) {
			stack.push(node);
			while(!stack.empty()) {
				node = stack.pop();
				System.out.print(node.data + " ");
				if (node.rightChild != null) {
					stack.push(node.rightChild);
				}
				if(node.leftChild != null) {
					stack.push(node.leftChild);
				}
			}
		}
	}
	
	public void norecursiveIn(Node node) {
		if (node == null) {
			return;
		}
		Stack<Node> stack = new Stack<Node>();
		stack.push(node);
		
		while(!stack.isEmpty()) {
			while(stack.peek() != null) {
				stack.push(stack.peek().leftChild);
			}
			Node p = stack.pop();
			
			if(!stack.isEmpty()) {
				System.out.print(stack.peek().data + " ");
				p = stack.pop();
				stack.push(p.rightChild);
			}
		}
	}
	

	public static void main(String[] args) {
		BinTree tree = new BinTree();
		tree.createBinTree();
		Node root = tree.nodelist.get(0);
		
		tree.preOrderTraverse(root);
		System.out.println();
		tree.norecursivePre(root);
		System.out.println();
		
		System.out.println();
		tree.inOrderTraverse(root);
		System.out.println();
		tree.norecursiveIn(root);
		System.out.println();
		
		System.out.println();
		tree.lastOrderTraverse(root);
		
	}
}

让代码run起来:

1 2 4 8 9 5 3 6 7 
1 2 4 8 9 5 3 6 7 

8 4 9 2 5 1 6 3 7 
8 4 9 2 5 1 6 3 7 

8 9 4 5 2 6 7 3 1 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值