The method createBTree(int[], int) is undefined for the type MainFunction

Nick Tess :

I was about to build a binary tree, I didn't use the Generic because the static method can't invoke it so I planned to use the Object class to replace it. In that way, I could enter any type of value like int or String as I want. But unfortunately, here it is, it is an error. I put the code below, and pls help with this. I appreciate it.

import java.util.*;

public class MainFunction {

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3};
        BinaryTree bt = createBTree(arr,0);//this is where the error is ,(The method createBTree(int[], int) is undefined for the type MainFunction)
    }

}

public class BinaryTree 
{
        private Object val;
        private BinaryTree leftBTree;
        private BinaryTree rightBTree;


        public BinaryTree(Object val)
        {
            this.val = val;
        }

        private void clearTree()
        {
            this.val = null;
            this.leftBTree = null;
            this.rightBTree = null;
        }

        public void addRightTree (BinaryTree tree)
        {
            rightBTree = tree;
        }

        public void addLeftTree (BinaryTree tree)
        {
            leftBTree = tree;
        }

        public void editTree(Object val)
        {
            this.val = val;
        }

        public boolean isEmpty(BinaryTree tree)
        {
            if(tree != null)
                return false;
            return true;
        }

        public void deleteTree(BinaryTree tree)
        {
            tree.clearTree();
            if(tree.leftBTree != null)deleteTree(tree.leftBTree);
            if(tree.rightBTree != null)deleteTree(tree.rightBTree);
        }

        public static BinaryTree createBTree(Object[] arr,int index)
        {
            BinaryTree tree = null;
            if(index<arr.length&&arr[index] != null)
            {
                tree = new BinaryTree(arr[index]);
                tree.leftBTree = createBTree(arr,index*2+1);
                tree.rightBTree = createBTree(arr,index*2+2);
            }
            return tree;
        }

        public void preOrderTraversal(BinaryTree tree)
        {
            System.out.println(tree.val);
            if(tree.leftBTree != null)
                preOrderTraversal(tree.leftBTree);
            if(tree.rightBTree != null)
                preOrderTraversal(tree.rightBTree);
        }

        public void inOrderTraversal(BinaryTree tree)
        {
            if(tree.leftBTree != null)
                inOrderTraversal(tree.leftBTree);
            System.out.println(tree.val);
            if(tree.rightBTree != null)
                inOrderTraversal(tree.rightBTree);
        }

        public void postOrderTraversal(BinaryTree tree)
        {
            if(tree.leftBTree != null)
                postOrderTraversal(tree.leftBTree);
            if(tree.rightBTree != null)
                postOrderTraversal(tree.rightBTree);
            System.out.println(tree.val);
        }

}

Dmitry Pisklov :

First of all, I didn't get why you can't use generics - with static method, you just make method static, too. Secondly, int[] is NOT Object[] - because primitives in java are not objects, hence your code is not working.

Here's the fixed code with generics that works (note it uses Integer instead of int):

public class BinaryTree<T> {
    private T val;
    private BinaryTree<T> leftBTree;
    private BinaryTree<T> rightBTree;

    public BinaryTree(T val) {
        this.val = val;
    }

    private void clearTree() {
        this.val = null;
        this.leftBTree = null;
        this.rightBTree = null;
    }

    public void addRightTree(BinaryTree<T> tree) {
        rightBTree = tree;
    }

    public void addLeftTree(BinaryTree<T> tree) {
        leftBTree = tree;
    }

    public void editTree(T val) {
        this.val = val;
    }

    public boolean isEmpty(BinaryTree<T> tree) {
        return tree == null;
    }

    public void deleteTree(BinaryTree<T> tree) {
        tree.clearTree();
        if (tree.leftBTree != null) deleteTree(tree.leftBTree);
        if (tree.rightBTree != null) deleteTree(tree.rightBTree);
    }

    public static <T> BinaryTree<T> createBTree(T[] arr, int index) {
        BinaryTree<T> tree = null;
        if (index < arr.length && arr[index] != null) {
            tree = new BinaryTree<>(arr[index]);
            tree.leftBTree = createBTree(arr, index * 2 + 1);
            tree.rightBTree = createBTree(arr, index * 2 + 2);
        }
        return tree;
    }

    public void preOrderTraversal(BinaryTree<T> tree) {
        System.out.println(tree.val);
        if (tree.leftBTree != null)
            preOrderTraversal(tree.leftBTree);
        if (tree.rightBTree != null)
            preOrderTraversal(tree.rightBTree);
    }

    public void inOrderTraversal(BinaryTree<T> tree) {
        if (tree.leftBTree != null)
            inOrderTraversal(tree.leftBTree);
        System.out.println(tree.val);
        if (tree.rightBTree != null)
            inOrderTraversal(tree.rightBTree);
    }

    public void postOrderTraversal(BinaryTree<T> tree) {
        if (tree.leftBTree != null)
            postOrderTraversal(tree.leftBTree);
        if (tree.rightBTree != null)
            postOrderTraversal(tree.rightBTree);
        System.out.println(tree.val);
    }

    public static void main(String[] args) {
        Integer[] arr = new Integer[]{1, 2, 3};
        BinaryTree<Integer> bt = createBTree(arr, 0);
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=296407&siteId=1