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);
}
网友评论