1. Given the following code
package data1;
import java.util.*;
/*public class BST> implementing a set ADT and containing the following:
* private inner class BinaryNode representing a node with a (possibly) left child and a (possibly)
right child
** instance fields BinaryNode root, int size
* contains/insert/remove method - w/ O(height) complexity
** size O(1), isEmpty O(1), clear O(1),
*** findMin, findMax, findHeight methods - w/ O(height) complexity
**** toString() method printing set (in-order traversal) and the tree (BFS traversal)
**
*/
publicclass BST> {
private BinaryNode root;
privateint size;
//Search opertion of Set ADT
publicboolean contains(E value) {
return contains(root, value);
}
//Insert opertion of Set ADT
publicvoid insert(E value) {
root = insert(root, value);
size++;
}
//Remove operation of Set ADT
publicvoid remove(E value){
root = remove(root, value);
size--;
}
publicint size(){
return size;
}
publicboolean isEmpty(){
2. return root == null;
}
publicvoid clear(){
root = null;
size = 0;
}
public E findMin(){
return findMin(root);
}
public E findMax(){
return findMax(root);
}
publicint findHeight(){
return root.height();
}
public String toString(){
if(root == null)
return "set {} stored in an empty tree.n";
String elements = root.inOrder().toString();
return "set {" + elements.substring(1,elements.length()-1) + "} stored in tree n" + root;
}
private E findMin(BinaryNode node){
if(node == null)
returnnull;
if(node.left == null)
return node.element;
return findMin(node.left);
}
private E findMax(BinaryNode node){
if(node == null)
returnnull;
if(node.right == null)
return node.element;
return findMax(node.right);
}
privateboolean contains(BinaryNode node, E value) {
3. if (node == null)//node represents an empty substree
returnfalse;
int comparisonResult = value.compareTo(node.element);
if(comparisonResult < 0)//if value is less than root's value
return contains(node.left, value);
if(comparisonResult > 0)//if value is greater than root's value
return contains(node.right, value);
returntrue;//successful search
}
private BinaryNode insert(BinaryNode node, E value) {
if (node == null)//base case: empty tree
returnnew BinaryNode<>(value);
if (value.compareTo(node.element) < 0)
node.left = insert(node.left, value);//insert recursively to left-subtree
elseif (value.compareTo(node.element) > 0)
node.right = insert(node.right, value);//insert recursively to right-subtree
else {//duplicate value cannot be inserted in a BST implementing the Set ADT
size--;//insertion failed!
//System.out.print("BST.insert: Warning: " + value + " is already stored in the tree n" + node);
}
return node;
}
private BinaryNode remove(BinaryNode node, E value) {
if (node == null) {//base case: empty tree
System.out.println("BST.remove: Warning: " + value + " doesn't exist in the tree.");
size++;//removal failed!
return node;
}
if (value.compareTo(node.element) < 0)
node.left = remove(node.left, value);
elseif (value.compareTo(node.element) > 0)
node.right = remove(node.right, value);
else {//we have found the node that needs to be removed!
if (node.left != null && node.right != null) {//remove a node with two children
node.element = findMin(node.right);//replace it by the leftmost node at right subtree
node.right = remove(node.right, node.element);//then, remove the leftmost node in the right
4. subtree
/*alternative:
node.element = findMax(node.left);//replace it by the rightmost node at left subtree
node.left = remove(node.left, node.element);//then, remove the rightmost node in the left subtree
*/
} elseif (node.left != null)//remove a node with a left child only
return node.left;
elseif (node.right != null)//remove a node with a right child only
return node.right;
else//remove a node with no child!
returnnull;
}
return node;
}
privateclass BinaryNode {
public E element;//data
public BinaryNode left;//left child
public BinaryNode right;//right child
//constructor for leaves
public BinaryNode(E element) {
this(element, null, null);
}
//constructor for internal nodes
public BinaryNode(E element, BinaryNode left, BinaryNode right) {
this.left = left;
this.right = right;
this.element = element;
}
publicint height() {
if (left == null && right == null)
return 0;
if (left == null)
return 1 + right.height();
if (right == null)
return 1 + left.height();
5. return 1 + Math.max(left.height(), right.height());
}
publicint size() {
int size = 1;//counting root
if (left != null)//counting left subtree nodes
size += left.size();
if (right != null)//counting right subtree nodes
size += right.size();
return size;
}
publicvoid printPreOrder() {
System.out.print(element + " ");
if (left != null)
left.printPreOrder();
if (right != null)
right.printPreOrder();
}
publicvoid printPostOrder() {
if (left != null)
left.printPostOrder();
if (right != null)
right.printPostOrder();
System.out.print(element + " ");
}
publicvoid printInOrder() {
if (left != null)
left.printInOrder();
System.out.print(element + " ");
if (right != null)
right.printInOrder();
}
public ArrayList inOrder(){
ArrayList list = new ArrayList<>();
Stack stack = new Stack<>();
stack.push(this);
6. while(!stack.empty()){
Object cur = stack.pop();
if(cur instanceof BinaryNode) {
BinaryNode node = (BinaryNode) cur;
if (node.right != null)
stack.push(node.right);
stack.push(node.element);
if (node.left != null)
stack.push(node.left);
}else
list.add((E)cur);
}
return list;
}
publicvoid printBFS() {
Queue q = new LinkedList<>();
q.add(this);
while (!q.isEmpty()) {
BinaryNode cur = q.remove();
System.out.print(cur.element + " ");
if (cur.left != null)
q.add(cur.left);
if (cur.right != null)
q.add(cur.right);
}
}
publicvoid printDFS() {
Stack stack = new Stack<>();
stack.add(this);
while (!stack.empty()) {
BinaryNode cur = stack.pop();
7. System.out.print(cur.element + " ");
if (cur.right != null)
stack.push(cur.right);
if (cur.left != null)
stack.push(cur.left);
}
}
@Override
public String toString() {
if (left == null && right == null && element == null)
return "";
Queue list = new LinkedList<>();
String result = "";
list.add(this);
list.add(null);
int level = (int) Math.pow(2, height());
BinaryNode dummy = new BinaryNode(null);
while (!list.isEmpty()) {
boolean allDummies = true;
for (BinaryNode b : list)
if (b != dummy && b != null) {
allDummies = false;
break;
}
BinaryNode cur = list.remove();
if (cur == null || allDummies)
break;
for (int i = 0; i < level - 1; i++)
8. result += 't';
if (cur != dummy)
result += cur.element;
for (int i = 0; i < level + 1; i++)
result += 't';
if (cur.left != null)
list.add(cur.left);
else
list.add(dummy);
if (cur.right != null)
list.add(cur.right);
else
list.add(dummy);
if (list.peek() == null) {
for (int i = 0; i < height(); i++)
result += 'n';
list.remove();
list.add(null);
level /= 2;
}
}
return result + "n";
}
}
Q:
Write a Java method with the following signature that receives the post-order traversal of a
binary search tree (BST) in the form of an array of integers and constructs the BST. For example,
if the array is [4, 5, 6], the tree looks like this(notice the structure of it):
6
5
4
but, if the array is [4, 6, 5], the tree looks like this:
9. 5
4 6
and if the array is [5, 4, 6], the tree looks like this:
6
4
5
public static BST constructFromPostOrder(int[] postorder){...}
Q:
Write a Java method with the following signature that receives the post-order traversal of a
binary search tree (BST) in the form of an array of integers and constructs the BST. For example,
if the array is [4, 5, 6], the tree looks like this(notice the structure of it):
6
5
4
but, if the array is [4, 6, 5], the tree looks like this:
5
4 6
and if the array is [5, 4, 6], the tree looks like this:
6
4
5
public static BST constructFromPostOrder(int[] postorder){...}