- 首先,创建树结点类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.