Hello, I've been working on this for a while and I can't figure the rest out. Need asap if anyone can help.
maxBSt,minBST,isBST, and inOrder
package lab7;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class TreeExercise {
/*
* Construct BST from preorder traversal
*/
public static Node<Integer>
consBSTfromPreOrder(int[] arr, int start, int end)
{
if(start > end) return
null;
Node<Integer> root = new
Node<Integer>(arr[start], null, null);
int index = start+1;
while(index <= end)
{
if (arr[index]
< arr[start])
{
index ++;
}
else
{
break;
}
}
root.left =
consBSTfromPreOrder(arr, start + 1, index -1);
root.right =
consBSTfromPreOrder(arr, index, end);
return root;
}
/*
* Tree level traversal
*/
public static void levelOrder(Node<Integer>
root)
{
Queue<Node<Integer>>
que = new LinkedList<Node<Integer>>();
que.offer(root);
while(!que.isEmpty())
{
Node<Integer> temp = que.poll();
System.out.print(temp + " ");
if (temp.left !=
null)
que.offer(temp.left);
if(temp.right !=
null)
que.offer(temp.right);
}
}
/**
*
* @param root - root of the given BST
* @param ret - an ArrayList of the data following
inOrder tree traversal
*/
public static void inOrder(Node<Integer> root,
ArrayList<Integer> ret)
{
}
/**
*
* @param root - the root of given BST
* @return the maximum data in the given BST or 0 if
empty BST
*/
public static int maxBST(Node<Integer>
root)
{
//TODO: please add your code
here
return 0; // please remove this
line after your coding
}
/**
*
* @param root - the root of given binary search
tree
* @return - the minimum data in the given BST or 0 if
empty BST
*/
public static int minBST(Node<Integer>
root)
{
//TODO: please add your code
here
return 0; // please remove this
line after your coding
}
/**
*
* @param root - the given root of binary tree
* @return true if a binary search tree; otherwise
false
*/
public static boolean isBST(Node<Integer>
root)
{
//TODO: please add your code
here
return false; // please remove this
line after your coding
}
/* ====== Extra Credit (10%)
==============================*/
/**
*
* @param root - the root of the given BST
* @return data following tree level traversal
*/
/*
* Example: 7
* / \
* 5 10
* / \ \
* 3 6 12
*
* return: 7
* 5 10
* 3 6 12
*/
public static
ArrayList<ArrayList<Integer>>
levelOrderBST(Node<Integer> root)
{
//TODO: add your code here
return null; //remove this line
after your coding
}
}
/** * * @param root - root of the given BST * @param ret - an ArrayList of the data following inOrder tree traversal */ public static void inOrder(Node<Integer> root, ArrayList<Integer> ret) { if(root.left != null) { inOrder(root.left, ret); } ret.add(root.data); if(root.right != null) { inOrder(root.right, ret); } } /** * * @param root - the root of given BST * @return the maximum data in the given BST or 0 if empty BST */ public static int maxBST(Node<Integer> root) { if(root == null) { return 0; } while(root.right != null) { root = root.right; } return root.data; } /** * * @param root - the root of given binary search tree * @return - the minimum data in the given BST or 0 if empty BST */ public static int minBST(Node<Integer> root) { if(root == null) { return 0; } while(root.left != null) { root = root.left; } return root.data; } /** * * @param root - the given root of binary tree * @return true if a binary search tree; otherwise false */ public static boolean isBST(Node<Integer> root) { if(root == null) { return true; } if(root.left == null && root.right == null) { return true; } int leftValue = root.data - 1; int rightValue = root.data + 1; if(root.left != null) { leftValue = root.left.data; } if(root.right != null) { rightValue = root.right.data; } return (leftValue < root.data) && (rightValue > root.data) && isBST(root.left) && isBST(root.right); }
please upvote. Thanks!
Hello, I've been working on this for a while and I can't figure the rest out....
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...
Please help me with me. I did the first part to write the operations but in the driver its shows me errors, can't fix it. Help me. Need help with this to run the application. /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * * @author Java Foundations * @version 4.0 */ public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * Creates a new tree node with...
In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...
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...
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 take a look at my code that why the maxDepth function is not working? public class BinaryTree { class Node{ int key; Node left,right; public Node(int item) { key = item; left = right = null; } } Node root; public void BinaryTree(){ root = null; } void...
package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...
Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...
JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet { /** * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList * containing all the indexes of Strings in arr that match String s. For this method, two Strings, * p and q, match when p.equals(q) returns true or if both of the compared references are null. * * @param s The string...
You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...