public class Buildbst {
private int data;
private Buildbst left;
private Buildbst right;
//Set the binary search tree
public Buildbst(int data) {
this.data = data;
this.left = null;
this.right =null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Buildbst getLeft() {
return left;
}
public void setLeft(Buildbst left) {
this.left = left;
}
public Buildbst getRight() {
return right;
}
public void setRight(Buildbst right) {
this.right = right;
}
}
package Assignment5;
public class BinarySearchTree {
private Buildbst root;
public BinarySearchTree() {
this.root = null;
}
//Search
Buildbst search(Buildbst root, int data) {
if (root == null)
return null;
if (data < root.getData())
return search(root.getLeft(),data);
else if (data > root.getData())
return search(root.getRight(),data);
else
return root;
}
Buildbst insert(Buildbst root, int data) {
if (root == null)
root = new Buildbst(data);
else {
if (data < root.getData())
root.setLeft(insert(root.getLeft(),data));
else if (data > root.getData())
root.setRight(insert(root.getRight(),data));
}
return root;
}
//To find the smallest root
Buildbst findMin(Buildbst root) {
if (root == null)
return null;
else if (root.getLeft() == null)
return root;
else
return findMin(root.getLeft());
}
//delete the root that the user want
Buildbst delete(Buildbst root, int data) {
Buildbst temp;
if (root == null)
System.out.println("isempty");
else if (data < root.getData())
root.setLeft(delete(root.getLeft(),data));
else if (data > root.getData())
root.setRight(delete(root.getRight(),data));
else {
if (root.getLeft() != null && root.getRight() != null) {
temp = findMin(root.getRight());
root.setData(temp.getData());
root.setRight(delete(root.getRight(),root.getData()));
}else {
temp = root;
if (root.getLeft() == null)
root = root.getRight();
else if (root.getRight() == null)
root = root.getLeft();
temp = null;
}
}
return root;
}
void InOrderRecursive(Buildbst root) {
if(root == null)
return;
InOrderRecursive(root.getLeft());
System.out.print(root.getData() + " ");
InOrderRecursive(root.getRight());
}
//return a boolean number
public boolean callSearch(int data) {
root = search(root, data);
if (root != null) {
return true;
}
else {
return false;
}
}
public void callInsert(int data) {
root = insert(root, data);
}
public void callDelete(int data) {
root = delete(root, data);
}
public void callInOrderRecursive() {
InOrderRecursive(root);
}}
package Assignment5;
public class Testbst {
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
tree.callInsert(8);
tree.callInsert(4);
tree.callInsert(12);
tree.callInsert(2);
tree.callInsert(6);
tree.callInsert(10);
tree.callInsert(14);
tree.callInsert(1);
tree.callInsert(3);
tree.callInsert(5);
tree.callInsert(7);
tree.callInsert(9);
tree.callInsert(11);
tree.callInsert(13);
tree.callInsert(1);
System.out.println("inorder:\n");
tree.callInOrderRecursive();
tree.callDelete(2);
System.out.println("\nafter remove:");
tree.callInOrderRecursive();
long startTime=System.nanoTime();
System.out.println("\n");
System.out.print(tree.callSearch(15));
long endTime=System.nanoTime();
System.out.println("\n");
System.out.println("cputime:"+(endTime-startTime)+"ns");
}
}
}
How to write the result into a txt file?
Answer:
Just place the below code at the under of the main function.
This works perfectly and it has no error.
Code:
try {
long diff=endTime-startTime;
String result="CPU TIME :
"+Long.toString(diff);
FileWriter myWriter = new
FileWriter("C:\\Users\\Public\\result.txt");
myWriter.write(result);
myWriter.close();
}
catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary...
Have to write the tree into a text file?
JAVA CODE
Binary search tree
This is the tree
public class Buildbst {
private int data;
private Buildbst left;
private Buildbst right;
//Set the binary search tree
public Buildbst(int data)
{
this.data = data;
this.left = null;
this.right =null;
}
public int getData() {
return data;
}
public void
setData(int data) {
this.data = data;
}
public Buildbst getLeft() {
return left;
}
public void setLeft(Buildbst
left) {
this.left = left;...
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() {...
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){ ...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
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...
Take the following code for a binary source tree and make it include the following operations. bool replace(const Comparable & item, const Comparable & replacementItem); int getNumberOfNodes() const; (a) The replace method searches for the node that contains item in a binary search tree, if it is found, it replaces item with replacementItem. The binary tree should remain as a binary search tree after the replacement is done. Add the replace operation to the BinarySearchTree class. Test your replace using...
Preliminaries Download the template class and the driver file. Objective Learn how to traverse a binary search tree in order. Description For the template class BinarySearchTree, fill in the following methods: insert - Inserts values greater than the parent to the right, and values lesser than the parent to the left.parameters elem - The new element to be inserted into the tree. printInOrder - Prints the values stored in the tree in ascending order. Hint: Use a recursive method to...
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...
TreeNode.java
public class TreeNode {
public int key;
public TreeNode p;
public TreeNode left;
public TreeNode right;
public TreeNode () {
p = left = right = null;
}
public TreeNode (int k) {
key = k;
p = left = right = null;
}
}
BinarySearchTree.java
public class BinarySearchTree {
public TreeNode root;
public BinarySearchTree () {
root...
Removing Nodes from a Binary Tree in Java This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node....