Writing traversal methods
You will create two files in the csc403 package: one called A3SimplerBST.java and the other TestA3.java. Details on these programs appears below.
Copy the file SimplerBST.java from the week 1 examples package and rename the file and class A3SimplerBST. Add two public methods:
As with the get, put, and delete methods, you will need each of the public methods described above to call a corresponding private recursive method.
Next, write a program called TestA3 that:
// Having issue with sameValueCount, dont exactly know if Im comparing correctly, give stack overflow issue
package csc;
/*
* This is a simplified version of the BST class
* from the algs32 package.
*
*/
import stdlib.*;
import algs13.Queue;
import csc.A3SimplerBST.Node;
public class A3SimplerBST, V> {
private Node root; // root of BST
public static class Node, V> {
public K key; // sorted by
key
public V val; // associated
data
public Node left, right; // left
and right subtrees
public Node(K key, V val)
{
this.key =
key;
this.val =
val;
}
}
public A3SimplerBST() {
}
/*
*
*********************************************************************
* Search BST for given key, and return associated
value if found, return
* null if not found
***********************************************************************/
// does there exist a key-value pair with given
key?
public boolean contains(K key) {
return get(key) != null;
}
// return value associated with the given key, or
null if no such key exists
public V get(K key) {
return get(root, key);
}
private V get(Node x, K key) {
if (x == null)
return
null;
int cmp =
key.compareTo(x.key);
if (cmp < 0)
return
get(x.left, key);
else if (cmp > 0)
return
get(x.right, key);
else
return
x.val;
}
/*
*
*********************************************************************
* Insert key-value pair into BST If key already
exists, update with new
* value
***********************************************************************/
public void put(K key, V val) {
if (val == null) {
delete(key);
return;
}
root = put(root, key, val);
}
private Node put(Node x, K key, V val) {
if (x == null)
return new
Node<>(key, val);
int cmp =
key.compareTo(x.key);
if (cmp < 0)
x.left =
put(x.left, key, val);
else if (cmp > 0)
x.right =
put(x.right, key, val);
else
x.val =
val;
return x;
}
/*
*
*********************************************************************
* Delete
***********************************************************************/
public void delete(K key) {
root = delete(root, key);
}
private Node delete(Node x, K key) {
if (x == null)
return
null;
int cmp =
key.compareTo(x.key);
if (cmp < 0)
x.left =
delete(x.left, key);
else if (cmp > 0)
x.right =
delete(x.right, key);
else {
// x is the node
to be deleted.
// The value
returned in each of these cases below
// becomes the
value of the child reference from
// the parent of
x. Returning a null makes that
// reference a
null and so cuts x off, causing its
// automatic
deletion.
// Determine
how many children x has.
if (x.right ==
null && x.left == null) {
// This is a leaf node.
return null;
} else if
(x.right == null) {
// One child, to the left.
return x.left;
} else if
(x.left == null) {
// One child, to the right.
return x.right;
} else {
// Node x has two children.
// Find the node in x's right subtree with
// the minimum key.
Node rightTreeMinNode = findMin(x.right);
x.key = rightTreeMinNode.key;
x.val = rightTreeMinNode.val;
x.right = delete(x.right,
rightTreeMinNode.key);
}
}
return x;
}
private Node findMin(Node x) {
if (x.left == null)
return x;
else
return
findMin(x.left);
}
public void printKeys() {
printKeys(root);
}
private void printKeys(Node x) {
if (x == null)
return;
printKeys(x.left);
StdOut.println(x.key);
printKeys(x.right);
}
public Iterable keys() {
Queue q = new
Queue<>();
inOrder(root, q);
return q;
}
private void inOrder(Node x, Queue q) {
if (x == null)
return;
inOrder(x.left, q);
q.enqueue(x.key);
inOrder(x.right, q);
}
public int height() {
return height(root);
}
private int height(Node x) {
if (x == null)
return -1;
return 1 + Math.max(height(x.left),
height(x.right));
}
/*
*
*************************************************************************
* ** Visualization
*****************************************************************************/
public void drawTree() {
if (root != null) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setCanvasSize(1200, 700);
drawTree(root,
.5, 1, .25, 0);
}
}
private void drawTree(Node n, double x, double y,
double range, int depth) {
int CUTOFF = 10;
StdDraw.text(x, y,
n.key.toString());
StdDraw.setPenRadius(.007);
if (n.left != null && depth
!= CUTOFF) {
StdDraw.line(x -
range, y - .08, x - .01, y - .01);
drawTree(n.left,
x - range, y - .1, range * .5, depth + 1);
}
if (n.right != null &&
depth != CUTOFF) {
StdDraw.line(x +
range, y - .08, x + .01, y - .01);
drawTree(n.right, x + range, y - .1, range * .5, depth + 1);
}
}
public long sameValueCount(String key) {
return
sameValueCount(root, key);
}
// one named sameValueCount that takes a parameter of generic type V
private long sameValueCount(Node x, String
key) {
// when called, traverses every
node of the tree to count the number of
// nodes
// whose value (not its key) is
equal to the value passed as a parameter
if (x == null) {
return 0;
}
if (x.val.equals(key))
{
return 1 + sameValueCount(x.left, key) + sameValueCount(x.right, key);
}
return sameValueCount(x.left, key)
+ sameValueCount(x.right, key);
}
}
Because your tree is generic, the method should also be
generic.. I ran this code on data/tale.txt file and it works fine..
Just see the screenshot below:
public long sameValueCount(V key) {
return sameValueCount(root, key);
}
// one named sameValueCount that takes a parameter of generic type V
private long sameValueCount(Node<K, V> x, V key) {
// when called, traverses every node of the tree to count the number of
// nodes
// whose value (not its key) is equal to the value passed as a parameter
if (x == null) {
return 0;
}
if (x.val.equals(key)) {
return 1 + sameValueCount(x.left, key) + sameValueCount(x.right, key);
}
return sameValueCount(x.left, key) + sameValueCount(x.right, key);
}
I can say with full confidence
that the method has been coded correctly. If you are still facing
issue, then just look at how you are creating the tree. If some
loops are getting created while making the tree, then that can be a
issue..
i am putting my class also for your help:
package csc403;
/*
* This is a simplified version of the BST class
* from the algs32 package.
*
*/
import stdlib.*;
import algs13.Queue;
public class A3SimplerBST<K extends Comparable<? super K>, V> {
private Node<K, V> root; // root of BST
private static class Node<K extends Comparable<? super K>, V> {
public K key; // sorted by key
public V val; // associated data
public Node<K, V> left, right; // left and right subtrees
public Node(K key, V val) {
this.key = key;
this.val = val;
}
}
public A3SimplerBST() {
}
/*
* ********************************************************************* Search
* BST for given key, and return associated value if found, return null if not
* found
***********************************************************************/
// does there exist a key-value pair with given key?
public boolean contains(K key) {
return get(key) != null;
}
// return value associated with the given key, or null if no such key exists
public V get(K key) {
return get(root, key);
}
private V get(Node<K, V> x, K key) {
if (x == null)
return null;
int cmp = key.compareTo(x.key);
if (cmp < 0)
return get(x.left, key);
else if (cmp > 0)
return get(x.right, key);
else
return x.val;
}
/*
* ********************************************************************* Insert
* key-value pair into BST If key already exists, update with new value
***********************************************************************/
public void put(K key, V val) {
if (val == null) {
delete(key);
return;
}
root = put(root, key, val);
}
private Node<K, V> put(Node<K, V> x, K key, V val) {
if (x == null)
return new Node<>(key, val);
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left, key, val);
else if (cmp > 0)
x.right = put(x.right, key, val);
else
x.val = val;
return x;
}
/*
* ********************************************************************* Delete
***********************************************************************/
public void delete(K key) {
root = delete(root, key);
}
private Node<K, V> delete(Node<K, V> x, K key) {
if (x == null)
return null;
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = delete(x.left, key);
else if (cmp > 0)
x.right = delete(x.right, key);
else {
// x is the node to be deleted.
// The value returned in each of these cases below
// becomes the value of the child reference from
// the parent of x. Returning a null makes that
// reference a null and so cuts x off, causing its
// automatic deletion.
// Determine how many children x has.
if (x.right == null && x.left == null) {
// This is a leaf node.
return null;
} else if (x.right == null) {
// One child, to the left.
return x.left;
} else if (x.left == null) {
// One child, to the right.
return x.right;
} else {
// Node x has two children.
// Find the node in x's right subtree with
// the minimum key.
Node<K, V> rightTreeMinNode = findMin(x.right);
x.key = rightTreeMinNode.key;
x.val = rightTreeMinNode.val;
x.right = delete(x.right, rightTreeMinNode.key);
}
}
return x;
}
private Node<K, V> findMin(Node<K, V> x) {
if (x.left == null)
return x;
else
return findMin(x.left);
}
public void printKeys() {
printKeys(root);
}
private void printKeys(Node<K, V> x) {
if (x == null)
return;
printKeys(x.left);
StdOut.println(x.key);
printKeys(x.right);
}
public Iterable<K> keys() {
Queue<K> q = new Queue<>();
inOrder(root, q);
return q;
}
private void inOrder(Node<K, V> x, Queue<K> q) {
if (x == null)
return;
inOrder(x.left, q);
q.enqueue(x.key);
inOrder(x.right, q);
}
public int height() {
return height(root);
}
private int height(Node<K, V> x) {
if (x == null)
return -1;
return 1 + Math.max(height(x.left), height(x.right));
}
/*
* ***************************************************************************
* Visualization
*****************************************************************************/
public void drawTree() {
if (root != null) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setCanvasSize(1200, 700);
drawTree(root, .5, 1, .25, 0);
}
}
private void drawTree(Node<K, V> n, double x, double y, double range, int depth) {
int CUTOFF = 10;
StdDraw.text(x, y, n.key.toString());
StdDraw.setPenRadius(.007);
if (n.left != null && depth != CUTOFF) {
StdDraw.line(x - range, y - .08, x - .01, y - .01);
drawTree(n.left, x - range, y - .1, range * .5, depth + 1);
}
if (n.right != null && depth != CUTOFF) {
StdDraw.line(x + range, y - .08, x + .01, y - .01);
drawTree(n.right, x + range, y - .1, range * .5, depth + 1);
}
}
/*
* ***************************************************************************
* Below method finds the number of leaves in tree
*****************************************************************************/
public int leafCount() {
return leafCount(root);
}
private int leafCount(Node<K, V> startNode) {
// if node is null, leaf count is 0
if (startNode == null) {
return 0;
}
if (startNode.right == null && startNode.left == null) {
// this node is a leaf node.
return 1;
}
// else, node may have 1 or 2 child, so this node is not a leaf
// Node can have child which will have leaves
int leavesOnLeft = leafCount(startNode.left);
int leavesOnRight = leafCount(startNode.right);
// return the total count of leaves
return leavesOnLeft + leavesOnRight;
}
public long sameValueCount(V key) {
return sameValueCount(root, key);
}
// one named sameValueCount that takes a parameter of generic type V
private long sameValueCount(Node<K, V> x, V key) {
// when called, traverses every node of the tree to count the number of
// nodes
// whose value (not its key) is equal to the value passed as a parameter
if (x == null) {
return 0;
}
if (x.val.equals(key)) {
return 1 + sameValueCount(x.left, key) + sameValueCount(x.right, key);
}
return sameValueCount(x.left, key) + sameValueCount(x.right, key);
}
}
plz upvote.
Writing traversal methods You will create two files in the csc403 package: one called A3SimplerBST.java and...
Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception. public class MyIntSET { private Node root; private static class Node { public final int key; public Node left, right; public Node(int key) { this.key = key; } } public int sizeAboveDepth(int...
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...
Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; class BSTTreeNode{ BSTTreeNode left, right; String data; public BSTTreeNode(){ left = null; right = null; data = null; } public BSTTreeNode(String n){ left = null; right = null; data = n; } public void setLeft(BSTTreeNode n){ left = n; } public void setRight(BSTTreeNode n){ ...
Hi,
So I have a finished class for the most part aside of the toFile
method that takes a file absolute path +file name and writes to
that file. I'd like to write everything that is in my run method
also the toFile method. (They are the last two methods in the
class). When I write to the file this is what I get.
Instead of the desired
That I get to my counsel. I am
having trouble writing my...
JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth * * Returns the number of nodes with depth == d * Precondition: none * * param: d the depth to search for * * hint: use a recursive helper function * * ToDo 4 */ public int numNodesAtDepth(int d) { return -1; } **********USEFUL CODE FROM THE ASSIGNMENT:*********** public class simpleBST<Key extends Comparable<Key>, Value> { private Node root; ...
Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method: InOrder(Node theRoot), PreOrder(Node theRoot), PostOrder(Node theRoot), FindMin(), FindMax(), Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...
Java Question: Can you guys please help me write below code in RECURSION. // add a key-value pair, replacing old key-value pair if key is already present public void put(K key, V val) { if (val == null) { delete(key); return; } for (Node<K,V> x = first; x != null; x = x.next) if (key.equals(x.key)) { x.val = val; return; } first = new Node<>(key, val, first); N++;...
Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...
Add printRang method to BST.java that, given a low key value, and high key value, print all records in a sorted order whose values fall between the two given keys. (Both low key and high key do not have to be a key on the list). BST.java import java.lang.Comparable; /** Binary Search Tree implementation for Dictionary ADT */ class BST<Key extends Comparable<? super Key>, E> implements Dictionary<Key, E> { private BSTNode<Key,E> root; // Root of the BST int nodecount; //...
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...