简单二叉树实现

class BinaryTree{

  private class Node{

    private Comparable date;

    private Node left;

    private Node right;

    public void addNode(Comparable newDate){

      if(this.date.CompareTo(newDate.date) > 0){

        if(this.left == null){

          this.left = new Node(newDate.date);

         }else{

           this.left = addNode(newDate.date); 

         }

      }else{

        if(this.right == null){

          this.right = new Node(newDate.date);

        }else{

           this.right = addNode(newDate.date);

        }

      }

    public void toArrayNode(){

      if(this.left != null){

        this.left.toArrayNode();

      }

      BindaryTree.this.objDate[BinaryTree.this.foot++] = this.date;

      if(this.right != null){

        this.right.toArrayNode();

      }

    }

  }

  private Node root;

  private Object objDate[];

  private int foot;

  private int count;

  public Object[] toArray(){

    this.foot = 0;

    this.objDate = new Object[this.count];

    this.root.toArray();

    return this.objDate;

  }

  public void add(Object date){

    Node newDate = new Node((Comparable) date);

    if(root == null){

      this.root = newDate

    }else{

      this.root.addNode(newDate);

    }

    this.count++;

}

   

}

}

posted on 2018-03-30 17:34  风中少年2018  阅读(115)  评论(0)    收藏  举报

导航