-1

i am facing problem while implementing the preorder traversal , if i use newBst.root then it gives me expected output but while i donot use it then the output donot show expected result it throughs an error method preOrder in class treedataStructure.BinaryserachTree cannot be applied to given types; required: treedataStructure.BinaryNode

public class Main {

    public static void main(String[] args) {
        
        BinarySearchTree newBST = new BinarySearchTree();
        
        newBST.insert(70);
        newBST.insert(50);
        newBST.insert(90);
        newBST.insert(30);
        newBST.insert(60);
        newBST.insert(80);
        newBST.insert(100);
        newBST.insert(20);
        newBST.insert(40);  
        
        newBST.preOrder(newBST.root);
        newBST.inOrder(newBST.root);
        

    }

}
public class BinaryNode {
    
    public int value;
    public int height;
    //child
    public BinaryNode left;
    public BinaryNode right;
    

}

public class BinarySearchTree {
    
    BinaryNode root;
    
    public BinarySearchTree() {
        root = null; 
        
    }
    
    // Insert Method
    private BinaryNode insert(BinaryNode currentNode, int value) {
        
        if(currentNode == null) { 
            BinaryNode newNode = new BinaryNode(); 
            newNode.value = value; 
            System.out.println("The value successfully inserted"); 
            return newNode;
        } else if(value <= currentNode.value){ 
            currentNode.left = insert(currentNode.left, value); 
            return currentNode; 
        } else {
            currentNode.right = insert(currentNode.right, value);
            return currentNode; 
        }
    
    }
    
    public void insert(int value) {
        insert(root, value);
    }
    
    // Pre Order Traversal 
    public void preOrder(BinaryNode node) {
        if(node == null) {
            return;
        }
        System.out.print(node.value + " ");
        preOrder(node.left);
        preOrder(node.right);   
        
    }
    
    //In Order traversal
    public void inOrder(BinaryNode node) {
        if(node == null) {
            System.out.println("ACV");
            return;
        }
        
        inOrder(node.left);
        System.out.print(node.value + " ");
        inOrder(node.right);
    }

}

expected output

70 12 80 90 

output get

method preOrder in class treedataStructure.BinaryserachTree cannot be applied to given types; required: treedataStructure.BinaryNode

1 Answers1

0

These methods take a node as argument because they are called recursively for each node in the tree.

If you want (rightly so!) to be able to call these methods without argument, then just overload them so you have a signature that does not take an argument. Add this to your BinarySearchTree class:

public void preOrder() {
    preOrder(root);
}

public void inOrder() {
    inOrder(root);
}

And now in your main code you can call:

newBST.preOrder();
newBST.inOrder();
trincot
  • 317,000
  • 35
  • 244
  • 286