Question

10d 10h left Java 7 1. Check the Structure Autocomplete Ready 1 > import java.io.*;... 10 ALL A binary tree uses a multi-node10d 10h left The root node will always be the first node in the array a. For example, given the tree a = [2, 1, 3, 4, 5), theConstraints • 1sq3 10 • 1*n, a[i] < 100

Would appreciate the answer in the Java coding language please and thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
The code for required method is provided below in bold.

public static String isValid(List<Integer> elements) {

    // start build up the array with the elements.
    TreeElement root = new TreeElement(elements.get(0), null, null);

    // iterate over rest of the items and build BST.
    for (int index = 1; index < elements.size(); ++index) {

        int elementToAdd = elements.get(index);

        // insert the element to the node.
        insertElement(root, elementToAdd);
    }

    // Do a preorder traversal of the created node and store it in a list.
    List<Integer> preOrderedElementList = new ArrayList<>();
    preOrderBST(root, preOrderedElementList);

    // Check if the list generated is equal to the input provided to this method.
    if (elements.equals(preOrderedElementList)) {
        return "Yes";
    }

    return "No";
}

/**
 * This method is responsible for doing pre order traversal on the node and store the trversal in the list passed.
 * 
 * @param node
 * @param preOrderedElementList
 */
private static void preOrderBST(TreeElement node, List<Integer> preOrderedElementList) {

    if (node == null) {
        return;
    }

    preOrderedElementList.add(node.info);

    // move left.
    preOrderBST(node.left, preOrderedElementList);

    // move right.
    preOrderBST(node.right, preOrderedElementList);
}

/**
 * This method will insert the element to the node and return the updated node.
 * 
 * @param node
 * @param elementToAdd
 * @return
 */
private static TreeElement insertElement(TreeElement node, int elementToAdd) {

    // if the tree is empty, create and return the new node.
    if (node == null) {
        return new TreeElement(elementToAdd, null, null);
    }
    if (elementToAdd < node.info) {
        // go left.
        node.left = insertElement(node.left, elementToAdd);

    } else if (elementToAdd > node.info) {
        // go right.
        node.right = insertElement(node.right, elementToAdd);
    }
    return node;
}

// Define tree node structure
static class TreeElement {

    private int         info;
    private TreeElement left;
    private TreeElement right;

    TreeElement(int info, TreeElement left, TreeElement right) {
        this.info = info;
        this.left = left;
        this.right = right;
    }
}

Sample Input1:

Arrays.asList(2, 1, 3, 4, 5)

Output: Yes

Sample Input2:

Arrays.asList(3, 4, 5, 1, 2)

Output: No

The screenshot for running sample inputs is provided below.

public static void main(String[] args) { System.out.println(isValid(Arrays.asList( ...a: 2, 1, 3, 4, 5))); System.out.println

Add a comment
Know the answer?
Add Answer to:
Would appreciate the answer in the Java coding language please and thank you! 10d 10h left...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first...

    QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first preorder postorder inorder 0.10000 points    QUESTION 9 What kind of traversal does the following algorithm (in pseudo-code) describe? Algorithm traversal (root) if (root is not null)      traversal (leftSubTree)      process (root)      traversal (rightSubTree) end if end traversal breadth first preorder inorder postorder 0.10000 points    QUESTION 10 What kind of traversal does the following algorithm (in pseudo-code)  describe? Algorithm traversal (root) if...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct...

    Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...

  • Question B1 You are given the following Java classes: public class Queue { private static class...

    Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • Can you complete the following code, please? Thank you. class BinaryNode: def __init__(self, valu...

    Can you complete the following code, please? Thank you. class BinaryNode: def __init__(self, value): self.__value = value self.__left = None self.__right = None self.__parent = None self.__height = 1    def getValue(self): return self.__value    def setHeight(self, height): self.__height = height    def getHeight(self): return self.__height    def setParent(self, node): self.__parent = node    def getParent(self): return self.__parent    def setLeftChild(self, child): self.__left = child child.setParent(self)    def setRightChild(self, child): self.__right = child child.setParent(self)    def createLeftChild(self, value): self.__left = BinaryNode(value)    def createRightChild(self, value): self.__right = BinaryNode(value)    def...

  • Could someone please summarize the following for my programming class? They are study questions for java...

    Could someone please summarize the following for my programming class? They are study questions for java What an association list is. How to test if an association list is empty. How to find the value associated with a key in an association list. How to add a key-value pair to an association list. How to delete a key-value pair from an association list. How efficient an association list is (using O notation). What a circular list is. What a circular...

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT