Question

In: Computer Science

Problem 1: Write a Java program for traversing a Binary Search Tree in following ways: (use...

Problem 1:

Write a Java program for traversing a Binary Search Tree in following ways: (use any data structure, library functions) ---------- 20 points

i) Depth First Traversal: Inorder, LVR

ii) Depth First Traversal: Inorder, RVL

iii) Depth First Traversal: Preorder, VLR

iv) Depth First Traversal: Preorder, VRL

v) Depth First Traversal: Postorder, LRV

vi) Depth First Traversal: Postorder, RLV

No choice menu required.

Sample Input (taken from keyboard, single space separated nodes as found in a complete BFS: top-down, left to right traversal):

22 10 30 5 15 25 40 1 8

Sample Output (in console or in a file):

Depth First Traversal: Inorder, LVR -> 1 5 8 10 15 22 25 30 40

Depth First Traversal: Inorder, RVL -> 40 30 25 22 15 10 8 5 1

Depth First Traversal: Preorder, VLR -> 22 10 5 1 8 15 30 25 40

Depth First Traversal: Preorder, VRL -> 22 30 40 25 10 15 5 8 1

Depth First Traversal: Postorder, LRV -> 1 8 5 15 10 25 40 30 22

Depth First Traversal: Postorder, RLV -> 40 25 30 15 8 1 5 10 22

Solutions

Expert Solution

public class BinarySearchTree {

    class TreeNode {
        public int key;
        public TreeNode p;
        public TreeNode left;
        public TreeNode right;

        public TreeNode() {
            p = left = right = null;
        }

        public TreeNode(int k) {
            key = k;
            p = left = right = null;
        }
    }

    public TreeNode root;

    public BinarySearchTree() {
        root = null;
    }

    public void insert(int k) {
        TreeNode newNode = new TreeNode(k);
        
        if(root == null) {
            root = newNode;
        } else {
            TreeNode parent = root;
            while(true) {
                if(parent.key == k) {
                    return;
                }
                if(parent.key > k) {
                    if(parent.left == null) {
                        parent.left = newNode;
                        newNode.p = parent;
                        return;
                    } else {
                        parent = parent.left;
                    }
                } else {
                    if(parent.right == null) {
                        parent.right = newNode;
                        newNode.p = parent;
                        return;
                    } else {
                        parent = parent.right;
                    }
                }
            }
        }
    }

    private void inorderLVR(TreeNode t) {
        if(t != null) {
            inorderLVR(t.left);
            System.out.print(t.key + " ");
            inorderLVR(t.right);
        }
    }
    private void inorderRVL(TreeNode t) {
        if(t != null) {
            inorderRVL(t.right);
            System.out.print(t.key + " ");
            inorderRVL(t.left);
        }
    }


    private void preorderVLR(TreeNode t) {
        if(t != null) {
            System.out.print(t.key + " ");
            preorderVLR(t.left);
            preorderVLR(t.right);
        }
    }
    private void preorderVRL(TreeNode t) {
        if(t != null) {
            System.out.print(t.key + " ");
            preorderVRL(t.right);
            preorderVRL(t.left);
        }
    }


    private void postorderLRV(TreeNode t) {
        if(t != null) {
            postorderLRV(t.left);
            postorderLRV(t.right);
            System.out.print(t.key + " ");
        }
    }
    private void postorderRLV(TreeNode t) {
        if(t != null) {
            postorderRLV(t.right);
            postorderRLV(t.left);
            System.out.print(t.key + " ");
        }
    }
    

    /**
     * @param args
     */
    public static void main(String[] args) {
        int[] array = {22, 10, 30, 5, 15, 25, 40, 1, 8};
        BinarySearchTree bst = new BinarySearchTree();
        for (int i = 0; i < array.length; i++)
            bst.insert(array[i]);
        
        bst.printReport();
    }

    private void printReport() {
        System.out.print("Depth First Traversal: Inorder, LVR -> ");
        inorderLVR(root);
        System.out.println();
        System.out.print("Depth First Traversal: Inorder, RVL -> ");
        inorderRVL(root);
        System.out.println();
        System.out.print("Depth First Traversal: Preorder, VLR -> ");
        preorderVLR(root);
        System.out.println();
        System.out.print("Depth First Traversal: Preorder, VRL -> ");
        preorderVRL(root);
        System.out.println();
        System.out.print("Depth First Traversal: Postorder, LRV -> ");
        postorderLRV(root);
        System.out.println();
        System.out.print("Depth First Traversal: Postorder, RLV -> ");
        postorderRLV(root);
        System.out.println();
    }

}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
JAVA PROGRAM Create a Binary Search Tree with the following elements in the order mentioned below:...
JAVA PROGRAM Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85, 89, 3, 2, 8, 65, 92 Print the Pre-order of this tree Print the height and the balance factor of the nodes in the order they were inserted (5, 85, 89, 3, 2, 8, 65, 92) in the form of a table with three columns and 9 rows. Use column headers “Node”, “Height”, and “Balance Factor” for the three columns respectively. Use...
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
Write a java program that presents the user with the following menu Linear Search Binary Search...
Write a java program that presents the user with the following menu Linear Search Binary Search The user can choose any of these operations using numbers 1 or 2. Once selected, the operation asks the user for an input file to be searched (the file is provided to you and is named input.txt). If option 1 or 2 is selected, it asks the user for the search string. This is the string that the user wants the program to search...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation /** Class to encapsulate a tree node. */ protected static class Node implements Serializable {
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write the code.
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT