【问题标题】:Error in ternary tree implementation三叉树实现错误
【发布时间】:2017-07-21 22:43:12
【问题描述】:

我正在尝试实现三叉树,但出现以下错误,我不确定问题出在哪里,因为我的构造函数需要整数输入。

错误:

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;
}

}

【问题讨论】:

  • 你不应该在库中导入 .* 不是一个好习惯
  • @RajHassani 像“你永远不应该”这样的陈述很难辩护。你知道所有的场景吗?毕竟是java.io/java.util .. 在这里想知道“X 班是从哪里来的”几乎/根本不用担心。

标签: java tree ternary-tree


【解决方案1】:

这是虚空:

public Trenarytree(int data)

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

【讨论】:

  • 现在一切都说得通了。谢谢
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多