三元树实现中的错误

时间:2017-03-02 09:29:30

标签: java tree ternary-tree

我正在尝试实现trenary树,但是我收到以下错误,我不确定是什么问题,因为我的构造函数需要整数输入。

错误:

Trenarytree.java:46: error: constructor Trenarytree in class Trenarytree cannot be applied to given types;
    Trenarytree tree = new Trenarytree(1); 
                       ^

必需:没有参数   发现:int   原因:实际和正式的参数列表长度不同 1错误

代码:

import java.io.*;
import java.util.*;

public class Trenarytree {

public int y = 0;
public int count = 0;
private static Node root;

public void Trenarytree(int data)
{
    root = new Node(data);
}

public void add(Node parent, Node child)
{
    if (parent.getLeft() == null)
    {
        parent.setLeft(child);
    }
    else if (parent.getMiddle() == null){
        parent.setMiddle(child);
    }
    else
    {
        parent.setRight(child);
    }
}

public Node sum(Node z){
    if (z.getLeft()!=null) y++;
    if (z.getRight()!=null) y++;
    if (z.getMiddle()!=null) y++;
    if (y % 2 ==0){
        count++;
    y=0;};
}

public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc = new Scanner (System.in);
    int M = sc.nextInt();
    int N = sc.nextInt();
    int k, l;
    Node[] array;
    Trenarytree tree = new Trenarytree(1); 
    array[1] = new Node(1);
    for (int i = 0; i < N; i++){
        k = sc.nextInt();
        l = sc.nextInt();
        array[k] = new Node(k);
        if (i==1) tree.add(root, array[k]);
            else tree.add(array[l], array[k]);

    }
}
}

class Node {
private int key;
private Node left;
private Node right;
private Node middle;

Node (int key) {
    this.key = key;
    right = null;
    left = null;
    middle = null;
}

public void setKey(int key) {
    this.key = key;
}

public int getKey() {
    return key;
}

public void setLeft(Node left) {
    this.left = left;
}


public Node getLeft() {
    return left;
}

public void setMiddle(Node middle) {
    this.middle = middle;
}

public Node getMiddle() {
    return middle;
}
public void setRight(Node right ) {
    this.right = right;
}

public Node getRight() {
    return right;
}

}

2 个答案:

答案 0 :(得分:3)

这是无效的:

public Trenarytree(int data)

有了它,它就是一个方法,没有它,它就是一个构造函数。

答案 1 :(得分:2)

从构造函数Trenarytree定义中删除void。构造函数不应该返回任何东西,它们返回一个构造的对象。