2013-01-22 99 views
0

對於下面的代碼,我發現了以下錯誤:爲什麼我會在這裏不允許「'空''類型?

SearchTree.java:19: error: 'void' type not allowed here 
    this.setRoot(this.insertgt(this.insertionSort(value))); 

什麼問題?

代碼:

import java.util.Arrays; 

public class SearchTree { 
    private TreeNode root; 
    private static int sum = 0; 
    public void setRoot(TreeNode n){ 
     this.root = n; 
    } 

    public TreeNode getRoot(){ 
     return this.root; 
    } 

    // returns a SearchTree 
    public void insert(int[] value) { 
     System.out.println(value == null); 
     this.setRoot(this.insertgt(this.insertionSort(value))); 
    } 

    public void insertgt(int[] value) { 
     if (value.length == 0) return; 
     TreeNode currentNode; 
     currentNode.left = new TreeNode(ArrayMinimum.minimum(value)); 
     if (value.length > 0) { 
      currentNode = new TreeNode(insertgt(value)); 
      currentNode.right = new TreeNode(insertgt(value)); 
     } 
     return currentNode; 
    } 

    // returns sum of node.values 
    public int sum() { 
     if (this.left == null && this.right == null) return sum; 
     sum = this.value + sum; 
     if (this.left != null && this.right != null) { 
      sum = this.left.sum(); 
      sum = this.right.sum(); 
     } 
    } 

    // returns number of nodes 
    public int numberOfLeafs() { 
     if (this.left == null && this.right == null) 
      return(1 + this.left.numberOfLeafs() + this.right.numberOfLeafs()); 
     return(1 + this.left.numberOfLeafs() + this.right.numberOfLeafs()); 
    } 

    // returns zahl if searchTree contains zahl 
    public int search(int zahl) { 
     if (this.value == zahl) return null; 
     return(1 + this.left.search(zahl) + this.right.search(zahl)); 
    } 

    public int[] insertionSort(int[] data) { 
     for(int i = 1; i < data.length; i++) { 
      int t = data[i]; 
      int j = i - 1; 
      while(j >= 0 && data[j] > t) { 
       data[j + 1] = data[j]; 
       j--; 
      } 
      data[j + 1] = t; 
     } 
     return data; 
    } 
} 

class ArrayMinimum { 
    // removes and returns the minimum element 
    static int minimum(int [] v) { 
     int min = v[0]; 
     for (int i = 0; i < v.length; i++) { 
      if (v[i] < min) { 
       min = v[i]; 
      } 
     } 
     ArrayUtils.remove(value, min); 
     return min; 
    } 
} 

回答

0

setRoot方法期望接收一個TreeNode作爲參數,問題是你傳遞的參數是void的方法insertgt的結果,你必須從insertgt方法返回一個TreeNode對象

this.setRoot(this.insertgt(this.insertionSort(value))); 
0
 this.setRoot(this.insertgt(this.insertionSort(value))); 

insertgt方法似乎返回void和你傳遞的是對setRoot方法,它需要一個TreeNode,這就是爲什麼你得到錯誤。

相關問題