二叉树之4-给定数组创建二叉查找树

2018-10-08  本文已影响0人  旭仔_2e16
image.png
class BinarySearchTree{
 private Node root;
 public Node getRoot(){
    return root;  
  }
public void buildTree(int value){
  buildTree(null, value);
}
public void buildTree(Node node, int value){
   if(root==null){
      root=new Root();
      node = root;
   }
  if(value<root.value) {
    if(node.left==null){
        node.left=new Node(node ,left,right,value);
    }else{
        buildTree(node.left, value);
    }
  }else{
      if(node.right==null){
        node.right=new Node(node,left,right,value);
      }else{
        buildTree(node.right, value)
    }
  }
 }
  public Node buildTree(int[] arr){
    if(arr == null || arr.lenght == 0){
      return null;
    }
    for(int i=0; i<arr.length; i++){
        buildTree(arr[i])
    }
  return root;
  }
}

//测试
@Test
public void test(){
  BinarySearchTree   binarySearchTree = new   BinarySearchTree();
  int[] arr= [4,7,3,1,8,10,6,13,14];
  binarySearchTree.buildTree(arr);
}

上一篇 下一篇

猜你喜欢

热点阅读