二叉树的实现和递归遍历

package com.sunxl.test;

import java.util.LinkedList;
import java.util.List;

import com.sun.star.packages.NoEncryptionException;

/**
 * @author 熊浪
 * @Email xiongl@sunline.cn
 * @Time 2017年6月9日
 * @此类作用
 */
@SuppressWarnings("unused")
public class BinaryTree {

	private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	private static List<Node> nodeList = null;

	/**
	 * 私有内部类 满二叉树的个数为2^i-1
	 * 
	 * @author 熊浪
	 * @Email xiongl@sunline.cn
	 * @Time 2017年6月9日
	 * @此类作用
	 */
	private class Node {
		Node left;// 左节点
		Node right;// 有节点
		int data;// 节点值

		// 构造方法创建一个节点
		Node(int newData) {
			left = null;
			right = null;
			data = newData;
		}
	}

	/**
	 * 公开内部静态类
	 * 
	 * @author 熊浪
	 * @Email xiongl@sunline.cn
	 * @Time 2017年6月9日
	 * @此类作用
	 */
	public static class NodeObject {
		NodeObject left;
		NodeObject right;
		Object data;

		NodeObject(Object newData) {
			left = null;
			right = null;
			data = newData;
		}

		public NodeObject getLeft() {
			return left;
		}

		public void setLeft(NodeObject left) {
			this.left = left;
		}

		public NodeObject getRight() {
			return right;
		}

		public void setRight(NodeObject right) {
			this.right = right;
		}

		public Object getData() {
			return data;
		}

		public void setData(Object data) {
			this.data = data;
		}
	}

	/**
	 * 创建二叉树
	 */
	public void createBinTree() {
		nodeList = new LinkedList<Node>();
		// 把array数组的值放入nodeList中
		for (int i = 0, len = array.length; i < len; i++) {
			nodeList.add(new Node(array[i]));
		}
		// 按照前一个节点的索引和后个一个节点创建关系,1-0,2-1、2(2*0+1=1,2*0+2=2),3-3,4(2*1+1,2*1+2),5,6(2*2+1,2*2+2);总共需要循环的次数为len/2-1次,但最后一个赋值可能不一定有有节点,所以需要电镀提取
		for (int i = 0, len = array.length / 2 - 1; i < len; i++) {
			nodeList.get(i).left = nodeList.get(2 * i + 1);// 左节点:把上一个节点和左节点联系起来(通过索引*2+1)
			nodeList.get(i).right = nodeList.get(2 * i + 2);// 右节点:把上一个节点和右节点联系起来(通过索引*2+2)
		}
		// 最后一个父节点:因为最后一个父节点可能没有右节点,所以单独拿出来处理
		int lastParentIndex = array.length / 2 - 1;
		nodeList.get(lastParentIndex).left = nodeList.get(lastParentIndex * 2 + 1);
		// 右孩子,如果数组的长度为奇数才建立右孩子
		if (array.length % 2 == 1) {
			nodeList.get(lastParentIndex).right = nodeList.get(lastParentIndex * 2 + 2);
		}
	}

	/**
	 * 创建Object数组二叉树
	 * 
	 * @param nodeList
	 */
	public void createObjectBinTree(LinkedList<NodeObject> nodeList) {
		for (int i = 0, len = nodeList.size() / 2 - 1; i < len; i++) {
			nodeList.get(i).left = nodeList.get(2 * i + 1);
			nodeList.get(i).right = nodeList.get(2 * i + 2);
		}
		int lastParentIndex = nodeList.size() / 2 - 1;
		nodeList.get(lastParentIndex).left = nodeList.get(2 * lastParentIndex + 1);
		if (nodeList.size() % 2 == 1) {
			nodeList.get(lastParentIndex).right = nodeList.get(2 * lastParentIndex + 2);
		}
	}

	/**
	 * 先序遍历
	 * 
	 * @param node
	 */
	public static void preOrderTraverse(Node node) {
		if (node == null)
			return;
		System.out.print(node.data + "\t");
		preOrderTraverse(node.left);// 回调遍历左节点
		preOrderTraverse(node.right);// 回调遍历右节点
	}

	/**
	 * 中序遍历
	 * 
	 * @param node
	 */
	public static void inOrderTraverse(Node node) {
		if (node == null)
			return;
		inOrderTraverse(node.left);
		System.out.print(node.data + "\t");
		inOrderTraverse(node.right);
	}

	/**
	 * 后序遍历
	 * 
	 * @param node
	 */
	public static void postOrderTraverse(Node node) {
		if (node == null)
			return;
		postOrderTraverse(node.left);
		postOrderTraverse(node.right);
		System.out.print(node.data + "\t");
	}

	/**
	 * 			1 
	 * 		2 		3 
	 * 	  4    5  6   7 
	 *  8   9
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		BinaryTree binTree = new BinaryTree();
		binTree.createBinTree();
		// nodeList中第0个索引处的值即为根节点
		Node root = nodeList.get(0);

		System.out.println("先序遍历:");
		System.out.println("效果图:");
		System.out.println(1 + "\t" + 2 + "\t" + 4 + "\t" + 8 + "\t" + 9 + "\t" + 5 + "\t" + 3 + "\t" + 6 + "\t" + 7);
		System.out.println("打印图:");
		preOrderTraverse(root);
		System.out.println();

		System.out.println("中序遍历:");
		System.out.println("效果图:");
		System.out.println(2 + "\t" + 4 + "\t" + 8 + "\t" + 9 + "\t" + 5 + "\t" + 1 + "\t" + 3 + "\t" + 6 + "\t" + 7);
		System.out.println("打印图:");
		inOrderTraverse(root);
		System.out.println();

		System.out.println("后序遍历:");
		System.out.println("效果图:");
		System.out.println(2 + "\t" + 4 + "\t" + 8 + "\t" + 9 + "\t" + 5 + "\t" + 3 + "\t" + 6 + "\t" + 7 + "\t" + 1);
		System.out.println("打印图:");
		postOrderTraverse(root);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值