Question

In: Computer Science

public class ValidBST { class BSTTest<Key extends Comparable<Key>, Value> { private Node root; // root of...

public class ValidBST {


    class BSTTest<Key extends Comparable<Key>, Value> {
        private Node root;             // root of BST

        private class Node {
            private Key key;           // sorted by key
            private Value val;         // associated data
            private Node left, right;  // left and right subtrees

            public Node(Key key, Value val) {
                this.key = key;
                this.val = val;
            }
        }

    }


    public boolean isValidBST(BSTTest bst) {






        return false;
    }

    public static void main(String[] args) {

    }
}

Solutions

Expert Solution

The following is definition of Binary Search Tree(BST)

Binary Search Tree, is a node-based binary tree data structure which has the following properties:

  • The left subtree of a node contains only nodes with keys lesser than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • The left and right subtree each must also be a binary search tree.
    There must be no duplicate nodes.

// Java program to demonstrate insert operation in binary search tree
class BinarySearchTree {

   /* Class containing left and right child of current node and key value*/
   class Node {
       int key;
       Node left, right;

       public Node(int item) {
           key = item;
           left = right = null;
       }
   }

   // Root of BST
   Node root;

   // Constructor
   BinarySearchTree() {
       root = null;
   }

   // This method mainly calls insertRec()
   void insert(int key) {
   root = insertRec(root, key);
   }
  
   /* A recursive function to insert a new key in BST */
   Node insertRec(Node root, int key) {

       /* If the tree is empty, return a new node */
       if (root == null) {
           root = new Node(key);
           return root;
       }

       /* Otherwise, recur down the tree */
       if (key < root.key)
           root.left = insertRec(root.left, key);
       else if (key > root.key)
           root.right = insertRec(root.right, key);

       /* return the (unchanged) node pointer */
       return root;
   }

   // This method mainly calls InorderRec()
   void inorder() {
   inorderRec(root);
   }

   // A utility function to do inorder traversal of BST
   void inorderRec(Node root) {
       if (root != null) {
           inorderRec(root.left);
           System.out.println(root.key);
           inorderRec(root.right);
       }
   }

   // Driver Program to test above functions
   public static void main(String[] args) {
       BinarySearchTree tree = new BinarySearchTree();

       /* Let us create following BST
           50
       /   \
       30   70
       / \ / \
   20 40 60 80 */
       tree.insert(50);
       tree.insert(30);
       tree.insert(20);
       tree.insert(40);
       tree.insert(70);
       tree.insert(60);
       tree.insert(80);

       // print inorder traversal of the BST
       tree.inorder();
   }
}


Related Solutions

Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
MAKE node class outside of stack class class Stack{    private:        class node{   ...
MAKE node class outside of stack class class Stack{    private:        class node{            public:                node *next;                int data;                node(int d,node *n = NULL){                    data = d;                    next = n;                }        };        node *start;           public:        Stack();        Stack(const Stack& original);...
Laboratory Tasks public class LinkedList {              Node head;               class Node {    &nbsp
Laboratory Tasks public class LinkedList {              Node head;               class Node {                       int data;                     Node next;                     Node(int d) {                             data = d;                             next = null;                     }                 } } Complete the above java program by adding the following methods: Part1: isEmpty() checks if the linked list is empty or not. (return type is boolean) printList() prints all data in the linked list. (void method) insertFirst(int newData) add newData at the head of the linked list. (void method) insertLasL(int newData) add newData at...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get;...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get; set; } public Node (T item, Node<T> next) { … } } public class Polynomial { // A reference to the first node of a singly linked list private Node<Term> front; // Creates the polynomial 0 public Polynomial ( ) { } // Inserts term t into the current polynomial in its proper order // If a term with the same exponent already exists...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT