二叉树的简单实现 Java

4 篇文章 0 订阅
1 篇文章 0 订阅
  1. 首先,创建树结点类TreeNode.java
public class TreeNode <T>{
	private T data;
	private TreeNode<T> left;
	private TreeNode<T> right;
	private TreeNode<T> father;
	
	public TreeNode(T data,TreeNode<T> left,TreeNode<T> right,TreeNode<T> father){
		this.data = data;
		this.left = left;
		this.right = right;
		this.father = father;
	}

	public TreeNode(T data){
		this.data = data;
		this.left = null;
		this.right = null;
		this.father = null;
	}

	public T getData() {
		return data;
	}

	public TreeNode<T> getFather() {
		return father;
	}

	public TreeNode<T> getLeft() {
		return left;
	}

	public TreeNode<T> getRight() {
		return right;
	}

	public void setFather(TreeNode<T> father) {
		this.father = father;
	}

	public void setLeft(TreeNode<T> left) {
		this.left = left;
	}

	public void setRight(TreeNode<T> right) {
		this.right = right;
	}

	public void setData(T data) {

		this.data = data;
	}

	public void linkLeft(TreeNode<T> left){
		//将左儿子连接起来
		this.setLeft(left);
		left.setFather(this);
	}

	public void linkRight(TreeNode<T> right){
		//将右儿子连接起来
		this.setRight(right);
		right.setFather(this);
	}

	public boolean equals(TreeNode<T> obj) {
		//比较两个结点的大小
		return this.getData().equals(obj.getData());
	}

	public boolean hasLeft(){
		//是否有左儿子
		return this.left != null;
	}

	public boolean hasRight(){
		//是否有右儿子
		return this.right != null;
	}

	public boolean hasFather(){
		return this.father != null;
	}
}

2. 创建二叉树类BinaryTree.java

import java.util.ArrayDeque;
import java.util.Queue;

public class BinaryTree<T>{
	private TreeNode<T> root;
	public BinaryTree(TreeNode<T> root){
		this.root = root;
	}

	public void firstOrder(TreeNode<T> root){
		//先序遍历
		if (root == null)
			return;
		System.out.print(root.getData() + "\t");
		if (root.hasLeft())
			firstOrder(root.getLeft());
		if (root.hasRight())
			firstOrder(root.getRight());
	}

	public void inOrder(TreeNode<T> root){
		//中序遍历
		if (root == null)
			return;
		if (root.hasLeft())
			inOrder(root.getLeft());
		System.out.print(root.getData() + "\t");
		if (root.hasRight())
			inOrder(root.getRight());
	}

	public void lastOrder(TreeNode<T> root){
		//后序遍历
		if (root == null)
			return;
		if (root.hasLeft())
			lastOrder(root.getLeft());
		if (root.hasRight())
			lastOrder(root.getRight());
		System.out.print(root.getData() + "\t");
	}

	public void floorOrder(TreeNode<T> root){
		//层次遍历
		Queue<TreeNode<T>> temp = new ArrayDeque<>();
		if (root == null)
			return;
		temp.add(root);
		while (!temp.isEmpty()){
			if (temp.peek().hasLeft())
				temp.add(temp.peek().getLeft());
			if (temp.peek().hasRight())
				temp.add(temp.peek().getRight());
			System.out.print(temp.poll().getData() + "\t");
		}
	}

	public static void main(String[] args){
		TreeNode<Integer> a = new TreeNode<>(1);
		TreeNode<Integer> b = new TreeNode<>(2);
		TreeNode<Integer> c = new TreeNode<>(3);
		TreeNode<Integer> d = new TreeNode<>(4);
		TreeNode<Integer> e = new TreeNode<>(5);
		TreeNode<Integer> f = new TreeNode<>(6);
		TreeNode<Integer> g = new TreeNode<>(7);
		TreeNode<Integer> h = new TreeNode<>(8);
		a.linkLeft(b);
		a.linkRight(c);
		b.linkLeft(d);
		b.linkRight(e);
		c.linkLeft(f);
		c.linkRight(g);
		d.linkLeft(h);

		BinaryTree<Integer> tree = new BinaryTree<>(a);
		System.out.println("first order: ");
		tree.firstOrder(a);
		System.out.println("\nin order: ");
		tree.inOrder(a);
		System.out.println("\nlast order: ");
		tree.lastOrder(a);
		System.out.println("\nfloor order: ");
		tree.floorOrder(a);
	}
}

3.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值