java 二分法排序

直接上代码:
测试类:Test

public class Test {
    publicstatic void main(String[] args) {
       int[] arrys= { 56, 23, 57};
       BinaryTreebinaryTree = new BinaryTree();
       for (int i =0; i < arrys.length; i++) {
         binaryTree.insert(arrys[i]);
       }
      binaryTree.printTree();
    }
}

节点类:Node
public class Node {

    Nodeleft;
    Noderight;
    intdata;

    publicNode(int data) {
       this.data =data;
    }

}

二叉树类:BinaryTree

public class BinaryTree {
    Noderoot;

    public voidinsert(int data) {
       root =insert(root, data);
    }

    private Nodeinsert(Node node, int data) {
       //如果node为null那么首先创建一个node对象
       if (node ==null) {
          node = newNode(data);
       } else{
          if (data<= node.data) {
             node.left =insert(node.left, data);
          } else{
             node.right =insert(node.right, data);
          }
       }

       returnnode;
    }

    public voidprintTree() {
      this.printTree(root);
    }

    private voidprintTree(Node node) {
       if (node ==null)
         return;
      printTree(node.left);
      System.out.print(node.data + " ");
      printTree(node.right);
    }
}

你可能感兴趣的:(java)