二叉树

来源:互联网 发布:最新机顶盒软件排行 编辑:程序博客网 时间:2024/06/05 20:47
package class1;
class BinaryTree {
class Node {
private Comparable data ;
private Node left ;
private Node right ;
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.print(this.data + "\t"); 
if(this.right != null) {
this.right.printNode();
}
}
}
private Node root;
public void add(Comparable data){
Node newNode = new Node() ;
newNode.data = 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(8);
bt.add(3); 
bt.add(3); 
bt.add(10);
bt.add(9);
bt.add(1);
bt.add(5);
bt.add(5);
System.out.println("排序之后的结果:") ;
bt.print();
}
}