Search code examples
javatreeternary-tree

Error in ternary tree implementation


I am trying to implement trenary tree, but I am getting the following error and I am not sure what is the problem, as my constructor requires an integer input.

Error:

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

required: no arguments found: int reason: actual and formal argument lists differ in length 1 error

Code:

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

}

Solution

  • It's the void:

    public Trenarytree(int data)
    

    With it, it's a method, without it, it's a constructor.