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 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 {
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
There are plenty of examples of Binary Search Tree classes written in Java available on the...
There are plenty of examples of Binary Search Tree classes written in Java available on the Internet. Find one. Either download or copy and paste the code into an appropriately named Java class file. Compile that file. Then, create the class BinarySrcTree, which will inherit from the class you downloaded from the Internet. Place a comment in the class that states where you obtained your base class (URL or Web page or site name). You will then overwrite or overload...
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT