How can i insert an item at root in a binary search tree in java?
<p>I want to insert a new item in the root of a binary search tree. Below, i present the method for this and the helping methods for left and right rotation.I think there is a mistake there cause when (using the method below) try to print the tree with an inorder traverse, it stops just behind the new root, so it doesn't print the new root and the items in the right side of the root. I have checked the print method after inserting in a normal way items to the tree and it works, so i don't think there is a problem there.</p>

<pre><code>public class ST {

    private TreeNode root;

    public ST() {
        this.size = 0;
        this.root = null;
    }

    public void insert_at_root(Suspect item) {
        insert_at_rootRec(item, this.root);
    }

    private TreeNode insert_at_rootRec(Suspect item, TreeNode head) {
        if (head == null)
            return new TreeNode(item);
        if (item.key() &lt; head.item.key()) {
            head.left = insert_at_rootRec(item, head.left);
            head = rotateRight(head);
        } else {
            head.right = insert_at_rootRec(item, head.right);
            head = rotateLeft(head);
        }
        return head;

    }

    private TreeNode rotateRight(TreeNode h) {
        TreeNode x = h.left;
        h.left = x.right;
        x.right = h;
        return x;
    }

    private TreeNode rotateLeft(TreeNode h) {
        TreeNode x = h.right;
        h.right = x.left;
        x.left = h;
        return x;
    }



    public void printTreeByAFM(PrintStream stream) {
        printTreeByAFMRec(this.root, stream);
    }

    private void printTreeByAFMRec(TreeNode root, PrintStream stream) {
        if (root == null)
            return;
        printTreeByAFMRec(root.left, stream);
        stream.println(root.item);
        printTreeByAFMRec(root.right, stream);
    }

}
</code></pre>