Issue
I'm a beginner in Java and data structures and I've been trying to implement a binary search tree. I have written a code but it throws some error and I can't find it I know this is a blatant question but I really need help with this so I can continue my learning process
package binary tree;
import java.util.Scanner;
public class BinarySearchTree {
private static Node root;
BinarySearchTree() {
root = null;
}
static class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
public void display() {
System.out.print(value + " ");
}
}
public static void insert(int value) {
root = insert(root, value);
}
public static Node insert(Node node, int value) {
if (node == null) {
node = new Node(value);
} else if (value < node.value) {
node.left = insert(node.left, value);
} else if (value > node.value) {
node.right = insert(node.right, value);
}
return node;
}
public static void run() {
Scanner scan = new Scanner(System.in);
int nodeSize = scan.nextInt();
int nodeValue;
System.out.println("Enter Node Values:");
for (int i = 0; i < nodeSize; i++) {
nodeValue = scan.nextInt();
insert(nodeValue);
}
scan.close();
}
public void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
node.display();
inOrder(node.right);
}
}
public static void main(String args[]) {
run();
BinarySearchTree bst = new BinarySearchTree();
bst.inOrder(root);
}
}
I tried to create a node as a binary tree and getting input from the user and display it in inOrder traversal but the code gets only the size of the node and input but does not print any output
Solution
Besides the syntax error in package binary tree
(it should be a single name, not two), you have a mix up of static and instance members. First your run
function will populate the static root
member. But then you create an instance with new BinarySearchTree
and its constructor clears the static root
variable! So now you have lost your input.
Corrections to make:
root
should be an instance variableinsert
should be an instance method- The
bst
instance should be created before callinginsert
, and thoseinsert
calls should be on that instance. For this reason it may be more appropriate to not have arun
function, but move that code intomain
.
Corrected class:
public class BinarySearchTree {
private Node root; // Remove static. Should be member of instance
BinarySearchTree() {
root = null;
}
static class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
public void display() {
System.out.print(value + " ");
}
}
public void insert(int value) { // Remove static. Is instance method
root = insert(root, value);
}
public Node insert(Node node, int value) { // Remove static. Should be instance method
if (node == null) {
node = new Node(value);
} else if (value < node.value) {
node.left = insert(node.left, value);
} else if (value > node.value) {
node.right = insert(node.right, value);
}
return node;
}
public void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
node.display();
inOrder(node.right);
}
}
public void inOrder() { // Overload so you don't have to pass obvious argument
inOrder(root); // Provide root of the instance
}
public static void main(String args[]) {
BinarySearchTree bst = new BinarySearchTree(); // First create the BST instance
// Then get the input
System.out.println("Enter number of nodes: ");
Scanner scan = new Scanner(System.in);
int nodeSize = scan.nextInt();
int nodeValue;
System.out.println("Enter Node Values: ");
for (int i = 0; i < nodeSize; i++) {
nodeValue = scan.nextInt();
bst.insert(nodeValue); // Insert in the BST instance
}
scan.close();
bst.inOrder(); // Call overloaded method
}
}
Answered By - trincot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.