hi, I am suppose to implement that method in java for bst but the result doesn't work as excpected
* _Part 8: Implement this method._
*
* toString() is a method defined by Java's Object class
* that can be used to provide a String representation for your
* class. It is called anytime you concatenate an Object to
* a String.
*
* Build a string representation of the BST that describes both
* the values stored inside and the shape of the structure; the
* approach is described below.
*
* 1) Build a String representation of the tree
* performing a pre-order traversal of the tree.
* 2) As each node is "visited" call the .toString() method on the
* data payload for that node, and add this value to the string.
* 3) The printed contents of each node should follow the following format
* "( )"
* 4) A null Node reference should also be delimited with ()
*
* For example an empty tree (no nodes) should return the following:
* "()"
* A tree with one root node, whose data is "A" should return:
* "(A ()())"
* A tree with a root whose data is "B" and whose left child contains
* "A" and whose right child contains "C" should return:
* "(B (A ()())(C ()()))"
*
* @return - A String representation of the tree
*
*/
public String toString() {
String openpa = "(";
if (root!=null) {
openpa = openpa + root.getData() + " ";
//left child
Node current = getRoot().getLeft();
while (current != null) {
//if left child is null, print the current node, then move to right child
if (current.getLeft() == null) {
openpa = openpa + "(" + current.getData()+ "()())";
current = current.getRight();
} else {
//find predecessor
Node inopre = current.getLeft();
while (inopre.getRight() != null && inopre.getRight() != current) {
inopre = inopre.getRight();
}
//if the right child of predecessor = current
if (inopre.getRight() == current) {
inopre.right = null;
//set predecessor right child to null
// move to the right
current = current.getRight();
} else {
//if not, then print this node
openpa = openpa + "(" + current.getData() + ")";
//reset inorder predecessor right child to this node
inopre.right = current;
//move left
current = current.getLeft();
}
}
}
}
return openpa + ")";
}
/**
*
* The Node class for our BST. The BST
* as defined above is constructed from zero or more
* Node objects. Each object has between 0 and 2 children
* along with a data member that must implement the
* Comparable interface.
*
* @param <T>
*/
public static class Node<T extends Comparable<T>>
{
private Node<T> parent;
private Node<T> left;
private Node<T> right;
private T data;
private Node(T d) {
data = d;
parent = null;
left = null;
right = null;
}
public Node<T> getParent() { return parent; }
public Node<T> getLeft() { return left; }
public Node<T> getRight() { return right; }
public T getData() { return data; }
}
public static void main (String[] args){
BST tree=new BST();
tree.add(10);
tree.add(8);
tree.add(50);
tree.add(9);
tree.add(30);
tree.add(70);
tree.add(35);
System.out.println(tree.rank(10));
System.out.println(tree.toString());
}
}
OUTPUT
(10 (8()())(9()())(50)(30()())(35()())(70()()))
Process finished with exit code 0
Since the remaining code of the BST class is not provided, I’m attaching only the toString() method which I have fixed. Also an overloaded version of toString() method is given, both of them are needed, the second one actually performs recursion and return the String in pre order traversal format. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
//returns the string representation of pre order traversal of elements
public String toString() {
// calling the recursive method to do the traversal and return the
// string encapsulated inside ()
return "(" + toString(root) + ")";
}
// helper method to traverse bst in pre order and return a String
// representation of pre order traversal
private String toString(Node<T> node) {
if (node == null) {
// end condition
return "";
}
// concatenating this node's data, with the result of preorder traversal
// of left subtree and that of right subtree in proper format
return node.data + " (" + toString(node.left) + ")("
+ toString(node.right) + ")";
}
hi, I am suppose to implement that method in java for bst but the result doesn't...
In need of JAVA CODE FOR THIS OBJECTIVE: Modify the BST class I gave you as follows: In the case in which the deleted node has two children, write the code in two ways. a) always replace the deleted node with the largest node on the left. b) count how many deletions have been done, and for even deletions replace the deleted node with the largest node on the left, for odd deletions, replace the deleted node with the smallest...
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 implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the new root of the subtree to the parent so the parent can set it to be its child package trees; public class BinaryTree> { private Node root; //private int size; public static int sumTree(Node root) { if(root== null) { return 0; } int center = 0; if( root.item % 2 == 0) { center = root.item; } int left = sumTree(root.left); int right =...
Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...
Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...
*****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search trees. We have provided with you a standard implementation of a generic BST in BinarySearchTree.java. Note that this class is an abstract class, which means that some of its methods are not implemented. In previous assignments, you have implemented interfaces which specified methods that you needed to write. Very similarly, an abstract class is a class with some unimplemented methods (it can be thought...
Need help for three BinaryTree class public class BinaryTree { //Implements a Binary Tree of Strings private class Node { private Node left; private String data; private Node right; private Node parent; // reference to the parent node // the parent is null for the root node private Node(Node L, String d, Node r, Node p) { left = L; data...
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...
BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> { private TreeNode<E> overallRoot; public BST() { overallRoot = null; } // ************ ADD ************ // public void add(E addThis) { if (overallRoot == null) { overallRoot = new TreeNode<>(addThis); } else { add(overallRoot, addThis); } } private TreeNode<E> add(TreeNode<E> node, E addThis) { if...
Since we do not want to have to rewrite this code when the element type changes, this classes uses a Comparator to assist in ordering the elements. You will need to complete the siftUpComparator() and siftDownComparator() methods in the LinkedHeap Class. siftUp §Added element may violate heap-order property §siftUp() restores order starting at added node §Processing will continue working up the tree until: úFinds properly ordered node & parent úReaches the root (reaches node without parent) siftDown §Restores heap’s order...