1. import java.util.Queue;
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class BinaryTree<T extends Comparable<T>> {
BTNode root;
public BinaryTree() {
root = null;
}
public BinaryTree(BTNode root) {
root = null;
}
public void purge(){
root = null;
}
public boolean isEmpty(){
return root == null;
}
public void insert(T key){
if (root == null) {
root = new BTNode(key);
return;
}
BTNode temp;
Queue<BTNode> q = new LinkedList<BTNode>();
q.add(root);
// Do level order traversal until we find the first empty left or right child.
while (!q.isEmpty()) {
temp = q.poll();
if (temp.left == null) {
temp.left = new BTNode(key);
break;
}
else
q.add(temp.left);
if (temp.right == null) {
temp.right = new BTNode(key);
break;
}
else
q.add(temp.right);
}
}
2. // delete by copying last node
public void deleteByCopying(T data){
if(root == null)
throw new UnsupportedOperationException("Tree is empty!");
else if(root.left == null && root.right == null){
if(root.data.equals(data))
root = null;
else
throw new NoSuchElementException(data + " not in the tree.");
return;
}
Queue<BTNode> queue = new LinkedList<BTNode>();
queue.add(root);
BTNode keyNode = null;
BTNode currentNode = null;
BTNode parentNode = root;
boolean found = false;
while(! queue.isEmpty()){
currentNode = queue.poll();
if(currentNode.data.equals(data)){
if(! found){
keyNode = currentNode;
found = true;
}
}
if(currentNode.left != null){
queue.add(currentNode.left);
parentNode = currentNode;
}
if(currentNode.right != null){
queue.add(currentNode.right);
parentNode = currentNode;
}
}
if(! found)
throw new NoSuchElementException(data + " not in tree.");
while(! queue.isEmpty()){
currentNode = queue.poll();
System.out.print(currentNode.data + " ");
if(currentNode.left != null){
queue.add(currentNode.left);
parentNode = currentNode;
6. }
Given the information and the code above, using Java solve this task:
(Please make sure that the method is boolean not int)
BinaryTree insertion and deletion There are no fixed rules for inserting and deleting from a Binary-
tree. In our Binary-tree implementation, we use the insertion and deletion algorithms given below: -
Given a binary tree and a key, insert the key into the binary tree at the first position available in
level order traversal. Note: We can create a Binary tree without using the insert method. We do
this by creating the root node and then linking it with other nodes: BinaryTree> tree = new
BinaryTree Integer >(); BTNode node1 = new BTNode (7); BTNode node2 = new BTNode (4);
BTNode node 3= new BTNode (28); tree.root = node 1 node1.left = node 2; node2.right = node3; -
Given a binary tree, delete a node from it by making sure that tree shrinks from the bottom (i.e. the
deleted node is replaced by the last leaf node).Lab Tasks 1. Write a recursive instance method
public boolean subtreesHaveEqualNumberofNodes() of the BinaryTree <T> class that returns true
if the invoking Binary T ree <T> object has an equal number of nodes in its left and right subtrees.
Your method must throw java.lang.UnsupportedOperationException if the invoking tree is empty.