Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
<pre>class ConstructAVLTree{
	private Node rootNode;
	public ConstructAVLTree(){
		rootNode = null;
	}
private boolean searchElement(int element){
	return searchElement(rootNode, element);
}

private boolean searchElement(Node head, int element){
	boolean check = false;
	while((head != null) && !check){
		int headElement = head.element;
		if (element < headElement){
			head = head.leftChild;
		}
		else if (element > headElement){
			head = head.rightChild;
		}
		else{
			check = true;
			break;
		}
		check = searchElement(head, element);
	}
	return check;
}
}

public class AVLTreeExample {
    
    public static void main(String[] args) {
    	Scanner sc= new Scanner(System.in);
    	ConstructAVLTree obj = new ConstructAVLTree();
    	char choice;
int ch = sc.nextInt();
    	switch(ch){
case 1:
    			System.out.println("Enter integer element to search: ");
    			System.out.println(obj.searchElement(sc.nextInt()));
    			break;
}
}
}


What I have tried:

I am making a program that can add, remove elements (stacks and queues-like) and display elements in pre, in and post-order traversal. I tried changing boolean to void and default but didn't work and had more errors than expected. Hopefully someone can help me with this error, thank you.
Posted
Updated 20-Apr-23 1:53am

Java
private boolean searchElement(int element){

You have declared searchElement as a private method, so it is not visible outside the class object. Change it to public, in order for it to be callable from outside.
 
Share this answer
 
Comments
Rainy Plant 20-Apr-23 8:28am    
It actually works. Thank you
Your code is trying to call a private method of an object. Change from
Quote:
private boolean searchElement(int element){
return searchElement(rootNode, element);
}

To
Java
public boolean searchElement(int element){
	return searchElement(rootNode, element);
}
 
Share this answer
 
Comments
Rainy Plant 20-Apr-23 8:28am    
It actually works. Thank you
CPallini 20-Apr-23 8:38am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900