Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Computer Science(Java): Do not use Queque,stack... For initial sequence values,

ID: 3782525 • Letter: C

Question

Computer Science(Java): Do not use Queque,stack... For initial sequence values, accept them as string and then parse to Integer

Instructions: Your program should read from the standard input a sequence of integer values, with each value separated by a space. Your task is to Build a binary search tree using the values in the order they are entered Print 3 traversals: pre-order, in-order, and post-order. Allow the user to insertidelete a value. Once a new tree is generated, print it in-order. Find predecessor of a given value. The predecessor is the node that appears right before the given value in an in-order traversal. Find successor of a given value. The successor is the node that appears right after the given value in an in-order traversal

Explanation / Answer

class Node

{

    char data;

    Node left, right;

    Node(char item)

    {

        data = item;

        left = right = null;

    }

}

class BinaryTree

{

    Node root;

    static int preIndex = 0;

    Node buildTree(char in[], char pre[], int inStrt, int inEnd)

    {

        if (inStrt > inEnd)

            return null;

        Node tNode = new Node(pre[preIndex++]);

        if (inStrt == inEnd)

            return tNode;

        int inIndex = search(in, inStrt, inEnd, tNode.data);

        tNode.left = buildTree(in, pre, inStrt, inIndex - 1);

        tNode.right = buildTree(in, pre, inIndex + 1, inEnd);

        return tNode;

    }

    

    int search(char arr[], int strt, int end, char value)

    {

        int i;

        for (i = strt; i <= end; i++)

        {

            if (arr[i] == value)

                return i;

        }

        return i;

    }

    void printInorder(Node node)

    {

        if (node == null)

            return;

        printInorder(node.left);

        System.out.print(node.data + " ");

        printInorder(node.right);

    }

    public static void main(String args[])

    {

        BinaryTree tree = new BinaryTree();

        char in[] = new char[]{'D', 'B', 'E', 'A', 'F', 'C'};

        char pre[] = new char[]{'A', 'B', 'D', 'E', 'C', 'F'};

        int len = in.length;

        Node root = tree.buildTree(in, pre, 0, len - 1);

        System.out.println("Inorder traversal of constructed tree is : ");

        tree.printInorder(root);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote