二叉树的实例
//2012.7.29
//二叉树
class Student implements Comparable<Student>{
int score;
String name;
public Student(String name,int score){
this.name = name;
this.score = score ;
}
public int compareTo(Student stu){
if(this.score>stu.score){
return 1;
}else{
return -1;
}
}
public String toString(){
return "姓名:" + name + "\t成绩:" + score;
}
};
class BinaryTree{
class Node{ // 声明一个节点类
private Student data ; // 保存具体的内容
private Node left ; // 保存左子树
private Node right ; // 保存右子树
public Node(Student data){
this.data = data ;
}
public void addNode(Node newNode){
// 确定是放在左子树还是右子树
if(newNode.data.compareTo(this.data)<0){ // 内容小,放在左子树
if(this.left==null){
this.left = newNode ; // 直接将新的节点设置成左子树
}else{
this.left.addNode(newNode) ; // 继续向下判断
}
}
if(newNode.data.compareTo(this.data)>=0){ // 放在右子树
if(this.right==null){
this.right = newNode ; // 没有右子树则将此节点设置成右子树
}else{
this.right.addNode(newNode) ; // 继续向下判断
}
}
}
public void printNode(){ // 输出的时候采用中序遍历
if(this.left!=null){
this.left.printNode() ; // 输出左子树
}
System.out.println(this.data.toString()) ;
if(this.right!=null){
this.right.printNode() ;
}
}
};
private Node root ; // 根元素
public void add(Student data){ // 加入元素
Node newNode = new Node(data) ; // 定义新的节点
if(root==null){ // 没有根节点
root = newNode ; // 第一个元素作为根节点
}else{
root.addNode(newNode) ; // 确定是放在左子树还是放在右子树
}
}
public void print(){
this.root.printNode() ; // 通过根节点输出
}
};
public class ComparableDemo03{
public static void main(String args[]){
BinaryTree bt = new BinaryTree() ;
bt.add(new Student("as",8)) ;
bt.add(new Student("b",3)) ;
bt.add(new Student("c",3)) ;
bt.add(new Student("d",10)) ;
bt.add(new Student("e",9)) ;
bt.add(new Student("f",1)) ;
bt.add(new Student("g",5)) ;
bt.add(new Student("h",5)) ;
System.out.println("排序之后的结果:") ;
bt.print() ;
}
};
这个是仿写的,没有默写……但是注释了好多