Java实现二叉树的各种操作

package com.special;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

public class BinaryTree {
	
	//前序遍历:递归法
	static ArrayList<Integer> preList = new ArrayList<Integer>();
	public ArrayList<Integer> preOrder(TreeNode root) {	
		if(root == null)	return preList;	
		preList.add(root.val);		
		//if(root.left == null && root.right == null) return preList;	
		preOrder(root.left);
		preOrder(root.right);		
		return preList;
	}
	
	//前序遍历:迭代法,借助栈
	public ArrayList<Integer> preOrder2(TreeNode root) {	
		ArrayList<Integer> preList2 = new ArrayList<Integer>();
		if(root == null)	return preList;	
		
		Stack<TreeNode> stack = new Stack<TreeNode>();
		stack.push(root);
		while(!stack.isEmpty()) {
			TreeNode temp = stack.pop();
			preList2.add(temp.val);
				
			if(temp.right != null) {
				stack.push(temp.right);
			}
			
			if(temp.left != null) {
				stack.push(temp.left);
			}
		}
		
		return preList2;
	}
	
	//中序遍历:递归法
	static ArrayList<Integer> inList = new ArrayList<Integer>();
	public ArrayList<Integer> inOrder(TreeNode root) {	
		if(root == null)	return inList;	
		inOrder(root.left);		
		inList.add(root.val);				
		inOrder(root.right);		
		return inList;
	}
	
	//中序遍历:迭代法,借助栈
	public ArrayList<Integer> inOrder2(TreeNode root) {	
		ArrayList<Integer> inList2 = new ArrayList<Integer>();
		if(root == null)	return inList2;	
		
		Stack<TreeNode> stack = new Stack<TreeNode>();
		TreeNode node = root;
		while(node != null || !stack.isEmpty()) {
			while(node != null) {	//中序遍历核心思想:添加某个元素之前要添加它的所有左节点
				stack.push(node);
				node = node.left;
			}
			
			node = stack.pop();
			inList2.add(node.val);
			node = node.right;
		}
		
		return inList2;
	}
	
	//后序遍历:递归法
	static ArrayList<Integer> postList = new ArrayList<Integer>();
	public ArrayList<Integer> postOrder(TreeNode root) {	
		if(root == null)	return postList;	
		postOrder(root.left);
		postOrder(root.right);	
		postList.add(root.val);
		return postList;
	}
	
	//层序遍历:迭代法,借助队列
	public ArrayList<Integer> levelOrder(TreeNode root) {
		ArrayList<Integer> levelList = new ArrayList<Integer>();
		if(root == null) 
			return levelList;
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		//int count = queue.size();	//记录队列中每一层元素的个数
		queue.offer(root);
		
		while(!queue.isEmpty()) {
			TreeNode temp = queue.poll();
			if(temp != null) {
				levelList.add(temp.val);
			}
			
			if(temp.left != null) {
				queue.offer(temp.left);
			}
			
			if(temp.right != null) {
				queue.offer(temp.right);
			}
		}		
		return levelList;
	}
	
	//层序遍历增强版(逐行打印):迭代法,借助队列
	public void levelOrder2(TreeNode root) {
		if(root == null) 
			return;
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		int count = queue.size();	//记录队列中每一层元素的个数
		queue.offer(root);		
		while(!queue.isEmpty()) {
			if(count == 0) {
				count = queue.size();
			}
			
			TreeNode temp = queue.poll();
			if(temp != null) {
				count --;
				System.out.print(temp.val + " ");
			}
			
			if(count == 0) {
				System.out.println();
			}
			
			if(temp.left != null) {
				queue.offer(temp.left);
			}
			
			if(temp.right != null) {
				queue.offer(temp.right);
			}
		}	
	}
	
	//Z字型遍历:迭代法,借助栈
	public ArrayList<Integer> ZOrder(TreeNode root) {
		ArrayList<Integer> ZList = new ArrayList<Integer>();
		if(root == null) 
			return ZList;
		
		Stack<TreeNode> s1 = new Stack<TreeNode>();	//负责奇数层
		Stack<TreeNode> s2 = new Stack<TreeNode>();	//负责偶数层
		s1.push(root);
		
		while(!s1.isEmpty() || !s2.isEmpty()) {
			while(!s1.isEmpty()) {
				TreeNode temp = s1.pop();
				ZList.add(temp.val);
				if(temp.left != null) {
					s2.push(temp.left);
				}
				
				if(temp.right != null) {
					s2.push(temp.right);
				}
			}
			
			while(!s2.isEmpty()) {
				TreeNode temp = s2.pop();
				ZList.add(temp.val);
				if(temp.right != null) {
					s1.push(temp.right);
				}
				
				if(temp.left != null) {
					s1.push(temp.left);
				}
			}
		}
		
		return ZList;
	}
	
	//求二叉树的深度
	public int getDepth(TreeNode root) {
		if(root == null)
			return 0;
		
		int leftDepth = getDepth(root.left);
		int rightDepth = getDepth(root.right);
		
		return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
	}
	
	//将有序数组重建为二叉排序树(有序数组实际上是中序遍历的结果)
	public TreeNode convert(int[] array, int start, int end) {
		if(array == null || start > end) {
			return null;
		}
		int middle = (start + end) / 2;
		TreeNode root = new TreeNode(array[middle]);	
		root.left = convert(array, start, middle - 1);		
		root.right = convert(array, middle + 1, end);
		return root;
	}
	
	public static void main(String[] args) {
		TreeNode root1 = new TreeNode(1);
		root1.left = new TreeNode(2);
//		root1.right = new TreeNode(3);
//		
//		root1.left.left = new TreeNode(4);
//		root1.left.right = new TreeNode(5);
//		root1.left.left.left = new TreeNode(8);
//		
//		root1.right.left = new TreeNode(6);
//		root1.right.right = new TreeNode(7);
//		root1.right.right.right = new TreeNode(9);
//		
		BinaryTree t = new BinaryTree();
		postList = t.postOrder(root1);
		for(int i : postList) {
			System.out.print(i + " ");
		}
//	
		
//		int[] array = {1,2,3,4,5,6,7};
//		TreeNode root = t.convert(array, 0, array.length - 1);
//		System.out.println(root.val);
//		preList = t.preOrder(root);
//		for(int i : preList) {
//			System.out.print(i + " ");
//		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值