Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my BST. When i run the command
echo 1 100 1 50 1 50 5 6 0 | java Submission | tr -d [:space:]
it should return
100(1)50(2)50(2)100(1)
Instead when i run it it returns
100(1)50(2) 50 100

What I have tried:

Java
<pre>import java.util.Scanner;

public class Submission {
    private static class Node {
        protected int data;
        Node right;
        Node left;
        int elementCount;

        Node(int data) {
            this.data = data;
            this.elementCount = 1;
            this.left = null;
            this.right = null;
        }
    }
    private static Node root;

    public static void main(String[] args) {
        root = null;
        Scanner s = new Scanner(System.in);
        int choice = -10;
        while (choice != 0) {
            //System.out.println("Select an action from the menu:");
            //System.out.println("1. Insert an element into the BST");
            //System.out.println("2. Search for an element in the BST");
            //System.out.println("3. Find the maximum element from the BST");
            //System.out.println("4. Find the minimum element from the BST");
            //System.out.println("5. Print the elements in the BST in preorder");
            //System.out.println("6. Print the elements in the BST in postorder");
            //System.out.println("7. Print the elements in the BST in inorder");
            //System.out.println("8. Delete an element");
            //System.out.println("0. Exit the program");
            choice = s.nextInt();
            switch (choice) {
                case 1:
                    //System.out.print("Enter the element to be inserted: ");
                    int data = s.nextInt();
                    root = insert(root, data);
                    //System.out.println(data + " has been inserted");
                    break;
                case 2:
                    //System.out.print("Enter the element to be searched: ");
                    data = s.nextInt();
                    Node searchResult = search(root, data);
                    if (searchResult != null) {
                        System.out.println(searchResult.data + "(" + searchResult.elementCount + ")");
                    } else {
                        System.out.println(data + "(0)");
                    }
                    break;
                case 3:
                    if (root == null) {
                        System.out.println("0(0)");
                    } else {
                        Node maxNode = findMax(root);
                        System.out.println(maxNode.data + "(" + maxNode.elementCount + ")");
                    }
                    break;
                case 4:
                    if (root == null) {
                        System.out.println("0(0)");
                    } else {
                        Node minNode = findMin(root);
                        System.out.println(minNode.data + "(" + minNode.elementCount + ")");
                    }
                    break;
                case 5:
                    //System.out.print("Preorder traversal of BST: ");
                    printPreorder(root);
                    System.out.println();
                    break;
                case 6:
                    //System.out.print("Postorder traversal of BST: ");
                    printPostorder(root);
                    System.out.println();
                    break;
                case 7:
                    //System.out.print("Inorder traversal of BST: ");
                    printInorder(root);
                    System.out.println();
                    break;
                case 8:
                    //System.out.print("Enter the element to be deleted: ");
                    data = s.nextInt();
                    if (delete(data)) {
                        
                    } else {
                        
                    }
                    break;
                case 0:
                    //System.out.println("Exiting the program");
                    System.exit(0);
                    break;
            }
        }
        s.close();
    }

    private static Node insert(Node node, int data) {
        if (node == null) {
            return new Node(data);
        }
        if (data < node.data) {
            node.left = insert(node.left, data);
        } else if (data > node.data) {
            node.right = insert(node.right, data);
        } else {
            node.elementCount++;
        }
        return node;
    }

    private static Node search(Node node, int data) {
        if (node == null || node.data == data) {
            return node;
        }
        if (data < node.data) {
            return search(node.left, data);
        }
        return search(node.right, data);
    }

    private static Node findMax(Node node) {
        if (node.right == null) {
            return node;
        }
        return findMax(node.right);
    }

    private static Node findMin(Node node) {
        if (node.left == null) {
            return node;
        }
        return findMin(node.left);
    }

    private static void printPreorder(Node node) {
        if (node != null) {
            System.out.print(node.data + "(" + node.elementCount + ")" + " ");
            printPreorder(node.left);
            printPreorder(node.right);
        }
    }

    private static void printPostorder(Node node) {
        if (node == null) {
            return;
        }
        printPostorder(node.left);
        printPostorder(node.right);
        System.out.print(node.data + " ");
    }

    private static void printInorder(Node node) {
        if (node == null) {
            return;
        }
        printInorder(node.left);
        System.out.print(node.data + "(" + node.elementCount + ")" + " ");
        printInorder(node.right);
    }

    private static boolean delete(int data) {
        Node parent = null;
        Node current = root;

        while (current != null && current.data != data) {
            parent = current;
            if (data < current.data) {
                current = current.left;
            } else {
                current = current.right;
            }
        }

        if (current == null) {

            return false;
        }

        if (current.elementCount > 1) {
            current.elementCount--;
            return true;
        }

        if (current.left == null && current.right == null) {
            if (current == root) {
                root = null;
            } else if (current == parent.left) {
                parent.left = null;
            } else {
                parent.right = null;
            }
        } else if (current.left == null) {
            if (current == root) {
                root = current.right;
            } else if (current == parent.left) {
                parent.left = current.right;
            } else {
                parent.right = current.right;
            }
        } else if (current.right == null) {
            if (current == root) {
                root = current.left;
            } else if (current == parent.left) {
                parent.left = current.left;
            } else {
                parent.right = current.left;
            }
        } else {
            Node successor = findMin(current.right);
            current.data = successor.data;
            current.elementCount = successor.elementCount;
            successor.elementCount = 1;
            return delete(successor.data);
        }

        return true;
    }

}
Posted
Updated 2-Apr-23 8:53am
Comments
Richard MacCutchan 3-Apr-23 5:01am    
You need to explain how the output is supposed to be formed from the input, e.g. how does "1 100 1 50 1 50 5 6 0" relate to "100(1)50(2)50(2)100(1)". And just dumping over 200 lines of code that no one here has ever seen, or has any idea what it is supposed to do, is somewhat rude.

1 solution

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 

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