java 二叉树详解 + 实现代码

二叉树概念:

java二叉树的每个根节点最多有两颗字数,不存在子树大于2的节点,也就是说,二叉树是节点的最大度数为2的树,通常子树分为左子树和右子树,次序不能颠倒。

创建二叉树:

public void createTree(Object[] objs) {
		datas = new ArrayList<BinTree>();
		for (Object object : objs) {
			datas.add(new BinTree(object));
		}
		root = datas.get(0);// 将第一个作为根节点
		for (int i = 0; i < objs.length / 2; i++) {
			datas.get(i).leftNode = datas.get(i * 2 + 1);
			if (i * 2 + 2 < datas.size()) {// 避免偶数的时候 下标越界
				datas.get(i).rightNode = datas.get(i * 2 + 2);
			}
		}
}

 

二叉树的遍历有三种:

先序遍历:

  1. 访问根节点 

  2. 按先序遍历访问左子树   

  3. 按先序遍历访问右子树
    // 先序遍历
	public void preorder(BinTree root) {
		if (root != null) {
			visit(root.getData());
			preorder(root.leftNode);
			preorder(root.rightNode);
		}

	}

 

中序遍历:

  1. 按中序遍历左子树
  2.  访问根节点
  3.  按中序遍历访问右子树
	// 中序遍历
	public void inorder(BinTree root) {
		if (root != null) {
			inorder(root.leftNode);
			visit(root.getData());
			inorder(root.rightNode);
		}

	}

后序遍历:

  1. 按后序遍历访问左子树
  2.  按后序遍历访问右子树
  3.  访问根节点
	// 后序遍历
	public void afterorder(BinTree root) {
		if (root != null) {
			afterorder(root.leftNode);
			afterorder(root.rightNode);
			visit(root.getData());
		}

	}

运行代码:

	public static void main(String[] args) {

		Integer[] test = new Integer[] { 3, 1, 0, 2, 7, 5, 8, 9, 6 };
		BinTree tree = new BinTree();
		tree.createTree(test);
		System.out.println("前序遍历:");
		tree.preorder(tree.getRoot());
		System.out.println();
		System.out.println("中序遍历:");
		tree.inorder(tree.getRoot());
		System.out.println();
		System.out.println("后序遍历:");
		tree.afterorder(tree.getRoot());

	}

运行效果:

完整代码:

public class BinTree {
	private BinTree leftNode;// 左子树
	private BinTree rightNode;// 右子树
	private BinTree root;// 根节点
	private Object data; // 数据域
	private List<BinTree> datas;// 存储所有的节点

	public BinTree(BinTree leftNode, BinTree rightNode, Object data) {
		super();
		this.leftNode = leftNode;
		this.rightNode = rightNode;
		this.data = data;
	}

	public BinTree(Object data) {
		this(null, null, data);
	}

	public BinTree() {
		super();
	}

	public void createTree(Object[] objs) {
		datas = new ArrayList<BinTree>();
		for (Object object : objs) {
			datas.add(new BinTree(object));
		}
		root = datas.get(0);// 将第一个作为根节点
		for (int i = 0; i < objs.length / 2; i++) {
			datas.get(i).leftNode = datas.get(i * 2 + 1);
			if (i * 2 + 2 < datas.size()) {// 避免偶数的时候 下标越界
				datas.get(i).rightNode = datas.get(i * 2 + 2);
			}
		}
	}

	// 先序遍历
	public void preorder(BinTree root) {
		if (root != null) {
			visit(root.getData());
			preorder(root.leftNode);
			preorder(root.rightNode);
		}

	}

	// 中序遍历
	public void inorder(BinTree root) {
		if (root != null) {
			inorder(root.leftNode);
			visit(root.getData());
			inorder(root.rightNode);
		}

	}

	// 后序遍历
	public void afterorder(BinTree root) {
		if (root != null) {
			afterorder(root.leftNode);
			afterorder(root.rightNode);
			visit(root.getData());
		}

	}

	private void visit(Object obj) {
		System.out.print(obj + " ");
	}

	public Object getData() {
		return data;
	}

	public BinTree getRoot() {
		return root;
	}

	public static void main(String[] args) {

		Integer[] test = new Integer[] { 3, 1, 0, 2, 7, 5, 8, 9, 6 };
		BinTree tree = new BinTree();
		tree.createTree(test);
		System.out.println("前序遍历:");
		tree.preorder(tree.getRoot());
		System.out.println();
		System.out.println("中序遍历:");
		tree.inorder(tree.getRoot());
		System.out.println();
		System.out.println("后序遍历:");
		tree.afterorder(tree.getRoot());

	}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值