经常看到有些人写关于二叉树的一些博客,说了很多概念,然后建立二叉树那块的方法真是让我拙计啊,看到好多人是这样建立的,代码大概是这样:
public class BinaryTree {
int data;
BinaryTree left;
BinaryTree right;
public BinaryTree(int d,BinaryTree l,BinaryTree r){
data=d;
left=l;
right=r;
}
//其它遍历方法之类的。。。不再重复
public static void main(String[] args) {
//每次建立一个节点,然后其它的节点与以前的节点进行关联
BinaryTree bt1=new BinaryTree(1,null,null);
BinaryTree bt2=new BinaryTree(2,bt1,null);
BinaryTree bt5=new BinaryTree(5,null,null);
BinaryTree bt4=new BinaryTree(4,null,bt5);
BinaryTree root=new BinaryTree(3,bt2,bt4);
}
}
第二种方法:上边那种太蛋疼了,写下个人建立的方法,这种写法使用了Java中的方法调用链,可以很准确的控制每个节点的左右子树,而且很直观,缺点就是如果数据量大了还是不方便
public static class BTNode {
public BTNode left, right;
public int data;
public BTNode(int d){
data=d;
left=null;
right=null;
}
public BTNode getLeft() {
return left;
}
public BTNode setLeft(BTNode left) {
this.left = left;
return this;
}
public BTNode getRight() {
return right;
}
public BTNode setRight(BTNode right) {
this.right = right;
return this;
}
public int getData() {
return data;
}
public BTNode setData(int data) {
this.data = data;
return this;
}
public void printNode() {
System.out.print(data+"\t");
}
public static void main(String[] args) {
//构建一个二叉树
BTNode root=new BTNode(10);
root.setLeft(new BTNode(5)
.setLeft(new BTNode(7))
.setRight(new BTNode(8)))
.setRight(new BTNode(10)
.setLeft(new BTNode(1)
.setRight(new BTNode(3)))
.setRight(new BTNode(2)));
}
}
这种方法还有建立二叉排序树的方法,用这种方法建立的二叉树在中序遍历时,打印出来的是经过排序的数字:
public class BinarySortTree {
// 二叉树的跟节点
private BTNode root = null;
/** 添加节点 */
public BinarySortTree addNode(int n) {
if (root == null) {
root=new BTNode(n);
}
addNode(root,n);
return this;
}
private void addNode(BTNode node,int n){
if(n<node.getData()){
if(node.getLeft()==null) node.setLeft(new BTNode(n));
else addNode(node.getLeft(),n);
}else{
if(node.getRight()==null) node.setRight(new BTNode(n));
else addNode(node.getRight(),n);
}
}
/**
* 得到二叉树的跟节点引用
*/
public BTNode getRoot() {
return root;
}
/**
* 中序遍历
*/
public void inOrderVisit(){
visit1(root);
}
/**中序遍历*/
private void visit1(BTNode root){
if(null!=root){
visit1(root.getLeft());
root.printNode();
visit1(root.getRight());
}
}
public static void main(String[] args) {
BinarySortTree b=new BinarySortTree();
b.addNode(7).addNode(4).addNode(18).addNode(2)
.addNode(3).addNode(5);
b.inOrderVisit();
}
/**
* 二叉树的节点,抽象层次不同,包含左右子节点和跟的值
*/
public static class BTNode {
public BTNode left, right;
public int data;
public BTNode(int d){
data=d;
left=null;
right=null;
}
public BTNode getLeft() {
return left;
}
public BTNode setLeft(BTNode left) {
this.left = left;
return this;
}
public BTNode getRight() {
return right;
}
public BTNode setRight(BTNode right) {
this.right = right;
return this;
}
public int getData() {
return data;
}
public BTNode setData(int data) {
this.data = data;
return this;
}
public void printNode() {
System.out.print(data+"\t");
}
}
}
上边的代码中的遍历二叉树的方法可以再写简单一点,可以更简略的类似函数式编程的思想写为如下:
1 public void inOrderVisit1(){ 2 new Object(){ 3 void visit(BTNode root){ 4 if(null!=root){ 5 visit(root.getLeft()); 6 root.printNode(); 7 visit(root.getRight()); 8 } 9 } 10 }.visit(getRoot()); 11 }
最后归纳一下,其实熟练掌握数据结构还是在编程中很重要的,而且可以java语言特有的语言特性,在二叉树中可以玩出很多新的花样,比如:
1.如何按照层次来遍历一个二叉树?
2.如何按照层次来构建一个二叉树?