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()
{
root = null;
}
public void printTree()
{
printTree(root);
}
private void printTree(Node current)
{
if(current != null)
{
String content = "Current:"+current.data.toString();
if(current.left != null)
{
content += "; Left side:"+current.left.data.toString();
}
if(current.right != null)
{
content += "; Right side:"+current.right.data.toString();
}
System.out.println(content);
printTree(current.left);
printTree(current.right);
}
}
public void printInOrder()
{
System.out.print("In order:");
printInOrder(root);
System.out.println();
}
private void printInOrder(Node current)
{
if(current != null)
{
printInOrder(current.left);
System.out.print(current.data.toString()+",");
printInOrder(current.right);
}
}
public boolean contains(E val)
{
Node result = findNode(val, root);
if(result != null)
return true;
else
return false;
}
private Node findNode(E val, Node current)
{
//base cases
if(current == null)
return null;
if(current.data.equals(val))
return current;
//recursive cases
int result = current.data.compareTo(val);
if(result < 0)
return findNode(val, current.right);
else
return findNode(val, current.left);
}
public E findMin()
{
Node result = findMin(root);
if(result == null)
return null;
else
return result.data;
}
private Node findMin(Node current)
{
while(current.left != null)
{
current = current.left;
}
return current;
}
public E findMax()
{
Node current = root;
while(current.right != null)
{
current = current.right;
}
return current.data;
}
public void insert(E val)
{
root = insertHelper(val, root);
}
public Node insertHelper(E val, Node current)
{
/* for showing steps to insert a given value
if(val.equals(9) && current != null)
{
System.out.println(current.data);
}
*/
if(current == null)
{
return new Node(val);
}
int result = current.data.compareTo(val);
if(result < 0)
{
current.right = insertHelper(val, current.right);
}
else if(result > 0)
{
current.left = insertHelper(val, current.left);
}
else//update
{
current.data = val;
}
return current;
}
public void remove(E val)
{
root = removeHelper(val, root);
}
private Node removeHelper(E val, Node current)
{
if(current.data.equals(val))
{
if(current.left == null && current.right == null)//no children
{
return null;
}
else if(current.left != null && current.right != null)//two children
{
Node result = findMin(current.right);
result.right = removeHelper(result.data, current.right);
result.left = current.left;
return result;
}
else//one child
{
return (current.left != null)? current.left : current.right;
}
}
int result = current.data.compareTo(val);
if(result < 0)
{
current.right = removeHelper(val, current.right);
}
else if(result > 0)
{
current.left = removeHelper(val, current.left);
}
return current;
}
private class Node
{
E data;
Node left, right;
public Node(E d)
{
data = d;
left = null;
right = null;
}
}
}
BinarySearchTree.java
import java.util.*;
public class BinarySearchTree<E extends Comparable<? super
E>>
{
private Node root;
public BinarySearchTree()
{
root = null;
}
public void printInLevelOrder()
{
printInLevelOrder(root);
}
private void printInLevelOrder(Node current)
{
//If tree is empty then simply
return
if(current==null)
return;
//Queue which is used for
levelOrder Traversal
//in Queue add(),size(),remove()
and peek() takes O(1) time
Queue<Node> queue =new
LinkedList<Node>();
//add current node to queue O(1)
time
queue.add(current);
//Iterate until queue is
empty
while(queue.size()>0)
{
//Create a node
with front of the queue O(1) time
//Here treeNode
is a parent node
Node treeNode =
queue.peek();
//Print data of
parent node O(1) time
System.out.print(treeNode.data + " ");
//Remove parent node from queue O(1) time
queue.remove();
//Go to left subtree and add the node to queue
if exist O(1) time
if(treeNode.left != null)
queue.add(treeNode.left);
//Go to right subtree and add the node to queue if exist O(1)
time
if(treeNode.right != null)
queue.add(treeNode.right);
}
//Since we are adding n nodes to
the queue
//For each node we are taking O(1)
time in the while loop
//Hence the compplexity O(n)*O(1) =
O(n)
//Where n is the number of nodes in
the tree
}
public void printTree()
{
printTree(root);
}
private void printTree(Node current)
{
if(current != null)
{
String content =
"Current:"+current.data.toString();
if(current.left
!= null)
{
content += "; Left
side:"+current.left.data.toString();
}
if(current.right
!= null)
{
content += "; Right
side:"+current.right.data.toString();
}
System.out.println(content);
printTree(current.left);
printTree(current.right);
}
}
public void printInOrder()
{
System.out.print("In
order:");
printInOrder(root);
System.out.println();
}
private void printInOrder(Node current)
{
if(current != null)
{
printInOrder(current.left);
System.out.print(current.data.toString()+",");
printInOrder(current.right);
}
}
public boolean contains(E val)
{
Node result = findNode(val,
root);
if(result != null)
return
true;
else
return
false;
}
private Node findNode(E val, Node current)
{
//base cases
if(current == null)
return
null;
if(current.data.equals(val))
return
current;
//recursive cases
int result =
current.data.compareTo(val);
if(result < 0)
return
findNode(val, current.right);
else
return
findNode(val, current.left);
}
public E findMin()
{
Node result = findMin(root);
if(result == null)
return
null;
else
return
result.data;
}
private Node findMin(Node current)
{
while(current.left != null)
{
current =
current.left;
}
return current;
}
public E findMax()
{
Node current = root;
while(current.right != null)
{
current =
current.right;
}
return current.data;
}
public void insert(E val)
{
root = insertHelper(val,
root);
}
public Node insertHelper(E val, Node current)
{
if(current == null)
{
return new
Node(val);
}
int result =
current.data.compareTo(val);
if(result < 0)
{
current.right =
insertHelper(val, current.right);
}
else if(result > 0)
{
current.left =
insertHelper(val, current.left);
}
else//update
{
current.data =
val;
}
return current;
}
public void remove(E val)
{
root = removeHelper(val,
root);
}
private Node removeHelper(E val, Node current)
{
if(current.data.equals(val))
{
if(current.left
== null && current.right == null)//no children
{
return null;
}
else
if(current.left != null && current.right != null)//two
children
{
Node result = findMin(current.right);
result.right = removeHelper(result.data,
current.right);
result.left = current.left;
return result;
}
else//one
child
{
return (current.left != null)? current.left :
current.right;
}
}
int result =
current.data.compareTo(val);
if(result < 0)
{
current.right =
removeHelper(val, current.right);
}
else if(result > 0)
{
current.left =
removeHelper(val, current.left);
}
return current;
}
private class Node
{
E data;
Node left, right;
public Node(E d)
{
data = d;
left =
null;
right =
null;
}
}
}
Demo.java
public class Demo
{
public static void main(String[] args)
{
BinarySearchTree<Integer> bst
= new BinarySearchTree<Integer>( );
bst.insert(10);
bst.insert(6);
bst.insert(13);
bst.insert(12);
bst.insert(5);
bst.insert(14);
bst.insert(11);
bst.insert(7);
/*
10
/ \
6 13
/ \
/ \
5 7
12 14
/
11
*/
bst.printInLevelOrder();
}
}
output screenshot:

Java binary search tree Add the following print method to the binary search tree class created...
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;...
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...
C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...
please make a pretty JAVA GUI for this code
this is RED BLACK TREE and i Finished code
already
jus need a JAVA GUI for this code ... if poosible make
it pretty to look thanks and
please make own GUI code base on my code
thanks
ex:
(GUI only have to show RBTree)
----------------------------------------
RBTree.java
import java.util.Stack;
public class RBTree{
private Node current;
private Node parent;
private Node grandparent;
private Node header;
private Node...
Write a method that determines the key of the successor of the root node in a binary search tree. For any input binary search tree, find the key of successor of the root node.Note: Successor is the node with the next highest key, should work for any binary search tree - not just the given example input. #include <iostream> using namespace std; class Node { private: int key; string val; Node* left; Node* right; friend class BinarySearchTree; }; class BinarySearchTree {...
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; } }...
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...
In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...
Instructions Create a class BettterTree that extends OurTree, to facilitate the following. Update: if extending the class is too much, you can modify class OurTree. In our discussions about OurTree, we focused on nodes and their left and right children. For each node in the tree, is there another node of interest in addition to its children? If yes, how would you extend OurTree for that? How will the behavior of addNode method change? OurTree Code: public class OurTree {...
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...