Hyungwon Shim
Hyungwon Shim

Reputation: 1

What am I missing ? BST hidden case failure

Some hidden test cases fail to output correctly. I've tested for all edge cases I can think of; what am I missing?

Edge Cases Tested:

public void delete(int k) {
        Node nodeToDelete = getDescendant(root, k);

        // Case 1: Node has no children (leaf node)
        if (nodeToDelete.left == null && nodeToDelete.right == null) {
            if (nodeToDelete == root) {
                root = null;
            } else if (nodeToDelete.parent.left == nodeToDelete) {
                nodeToDelete.parent.left = null;
            } else {
                nodeToDelete.parent.right = null;
            }
        }
        // Case 2: Node has one child
        else if (nodeToDelete.left == null || nodeToDelete.right == null) {
            Node child = (nodeToDelete.left != null) ? nodeToDelete.left : nodeToDelete.right;

            if (nodeToDelete == root) {
                root = child;
            } else if (nodeToDelete.parent.left == nodeToDelete) {
                nodeToDelete.parent.left = child;
            } else {
                nodeToDelete.parent.right = child;
            }
            child.parent = nodeToDelete.parent;
        }
        // Case 3: Node has two children
        else {
            Node successor = minDescendant(nodeToDelete.right);
            int successorKey = successor.key;
            delete(successorKey); // Remove the successor
            nodeToDelete.key = successorKey; // Replace node's key with successor's key
        }
    }

    private Node minDescendant(Node n) {
        Node current = n;
        while (current.left != null) {
            current = current.left;
        }
        return current;
    }

    public int successorKey(int k) {
        Node node = getDescendant(root, k);

        // Case 1: Node has a right subtree
        if (node.right != null) {
            return minDescendant(node.right).key;
        }

        // Case 2: No right subtree; move up to find the successor
        Node ancestor = node.parent;
        while (ancestor != null && node == ancestor.right) {
            node = ancestor;
            ancestor = ancestor.parent;
        }
        return ancestor.key;
    }

    public int minKey() {
        return minDescendant(root).key;
    }


    /* Part (b) methods */

    public int medianKey() {
        List<Integer> sortedKeys = new ArrayList<>();
        inOrderTraversal(root, sortedKeys);

        // Median key is the middle of the sorted keys
        int n = sortedKeys.size();
        return sortedKeys.get((n - 1) / 2);
    }

    private void inOrderTraversal(Node node, List<Integer> keys) {
        if (node == null) return;
        inOrderTraversal(node.left, keys);
        keys.add(node.key);
        inOrderTraversal(node.right, keys);
    }


    /* Part (c) methods */

    public int nodesInLevel(int h) {
        return countNodesAtLevel(root, h, 0);
    }

    private int countNodesAtLevel(Node node, int targetLevel, int currentLevel) {
        if (node == null) return 0;
        if (currentLevel == targetLevel) return 1;

        int leftCount = countNodesAtLevel(node.left, targetLevel, currentLevel + 1);
        int rightCount = countNodesAtLevel(node.right, targetLevel, currentLevel + 1);

        return leftCount + rightCount;
    }

Upvotes: 0

Views: 32

Answers (0)

Related Questions