Question

In: Computer Science

one can tell by comparing nodes between two given trees whether they relate to each other,...

one can tell by comparing nodes between two given trees whether they relate to each other, by having a reflected symmetric structure (i.e. being mirror images of each other), having identical structure, or not being related at all. In this assignment, you are asked to implement the following features.
Hard-code some paired lists of integers.
Build binary search trees from each list
Print the binary search trees in the three orders discussed in class.
Determine if the two binary search trees are identical, mirrors of each other, or neither
Remove a number in each tree at random and compare the tree pair again

Further, since the methods of the binary search tree class have been presented with recursive function calls, it is now up to you to implement these recursive functions with iterative loops.

You must write a Class Node, a class TreeChecker and a class BinarySearchTree. For Java users, they must implement the following interfaces respectively:
public interface INode {

   //Getter of node data
   T getData();

   //Setter of node data
   void setData(T data);

    INode getLeftChild() ;

   void setLeftChild(INode leftChild) ;

   INode getRightChild() ;

   void setRightChild(INode rightChild);
  
}
public interface ITree {
   void setRoot(INode root);
INode getRoot();
void preorder();   // print tree in a preorder traversal
void inorder();   // print tree in an inorder traversal
void postorder();   // print tree in a postorder traversal
INode insert(INode root, T data); // insert data into tree
   INode remove(INode root, T data); //search/remove node with data
   T search(INode root, T data); //search and return data if it exists
   INode getMax(INode root); //return node with maximum value of subtree
   INode getMin(INode root); //return node with minimum value of subtree
}  
public interface ITreeChecker {
  
boolean isMirror(ITree root1, ITree root2); // check if two trees are mirror
// images
   boolean isSame(ITree root1, ITree root2); // check if two trees are identical
}

Then if you could run the code in this main class.

public class Main {

   public static void main(String[] args) throws IOException {  
       // build a tree for the following lists
// int array1[] = [4,1,9,12,3,2,8,7,16,20,13,11];
       // int array2[] = [4,1,9,12,3,2,8,7,16,20,13,11];

       //add more examples here

       Bst1 = new BinarySearchTree();
       Bst2 = new BinarySearchTree();
      
      
       //more trees for each example

       treeChecker = new TreeChecker();

       //build each BST

       //print each tree with each order of traversal

       //compare bst1 and bst2 as mirror images with tree checker
       If(treeChecker.isMirror(bst1, bst2))
           Print messages saying the trees are mirrors of each other
       // compare bst1 and bst2 as being identical with tree checker
       Else If(treeChecker.isSame(bst1, bst2))
           Print messages saying the trees are identical to each other
       Else
           Print message saying the trees are not related

       //randomly remove a number from each tree
       //compare bst1 and bst2 again

// more code here to finish regarding comparing the other BST pairs…
}

Solutions

Expert Solution

P.S : No doubt its a huge code please check it attentively before dislike it. I don't need a thumbsup but I just want you to check it minutely.

I have provided the class as asked in the question with the implementation of the interfaces.

**I have considered the data as Integer for the sack of simplicity you may edit it as per requirement**

class Node implements INode
{
int data;
Node left, right;
  
public Node(int item)
{
data = item;
left = right = null;
}
public setData(int data)
{
this.data = data;
}
public int getData()
{
return data;
}
public setLeftChild(Node left)
{
this.left = left;
}
public setRightChild(Node Right)
{
this.right = right;
}
public getLeftChild()
{
return left;
}
public getRightChild()
{
return right;
}
  
}
class BinarySearchTree implements ITree
{
Node root;
void setRoot(Node node)
{
Node(node);
}
Node getRoot()
{
return root;
}
public void preorder()
{
iterativePreorder(root);
}
  
// An iterative process to print preorder traversal of Binary tree
void iterativePreorder(Node node)
{
  
// Base Case
if (node == null) {
return;
}
  
// Create an empty stack and push root to it
Stack<Node> ns = new Stack<Node>();
ns.push(root);
  
/* Pop all items one by one. Do following for every popped item
a) print it
b) push its right child
c) push its left child
Note that right child is pushed first so that left is processed first */
while (ns.empty() == false) {
  
// Pop the top item from stack and print it
Node n = ns.peek();
System.out.print(n.data + " ");
ns.pop();
  
// Push right and left children of the popped node to stack
if (n.right != null) {
ns.push(n.right);
}
if (n.left != null) {
ns.push(n.left);
}
}
}
void inorder()
{
if (root == null)
return;
  
  
Stack<Node> ns = new Stack<Node>();
Node curr = root;
  
// traverse the tree
while (curr != null || ns.size() > 0)
{
  
/* Reach the left most Node of the
curr Node */
while (curr != null)
{
/* place pointer to a tree node on
the stack before traversing
the node's left subtree */
ns.push(curr);
curr = curr.left;
}
  
/* Current must be NULL at this point */
curr = ns.pop();
  
System.out.print(curr.data + " ");
  
/* we have visited the node and its
left subtree. Now, it's right
subtree's turn */
curr = curr.right;
}
}
ArrayList<Integer> res = new ArrayList<Integer>();
public void postorder()
{
res=postOrderIterative(root);
for (int i = 0; i < res.size(); i++)
System.out.print(res.get(i) + " ");
  
}
ArrayList<Integer> list = new ArrayList<Integer>();

// An iterative function to do postorder traversal
// of a given binary tree
ArrayList<Integer> postOrderIterative(Node node)
{
Stack<Node> S = new Stack<Node>();

// Check for empty tree
if (node == null)
return list;
S.push(node);
Node prev = null;
while (!S.isEmpty())
{
Node current = S.peek();

/* go down the tree in search of a leaf an if so process it
and pop stack otherwise move down */
if (prev == null || prev.left == current ||
prev.right == current)
{
if (current.left != null)
S.push(current.left);
else if (current.right != null)
S.push(current.right);
else
{
S.pop();
list.add(current.data);
}

/* go up the tree from left node, if the child is right
push it onto stack otherwise process parent and pop
stack */
}
else if (current.left == prev)
{
if (current.right != null)
S.push(current.right);
else
{
S.pop();
list.add(current.data);
}

/* go up the tree from right node and after coming back
from right node process parent and pop stack */
}
else if (current.right == prev)
{
S.pop();
list.add(current.data);
}

prev = current;
}

return list;
}
static Node newNode(int data)
{
Node temp = new Node();

temp.key = data;

temp.left = null;
temp.right = null;

return temp;
}
public Node insert(Node root, int key)
{
// Create a new Node containing
// the new element
Node newnode = newNode(key);

// Pointer to start traversing from root and
// traverses downward path to search
// where the new node to be inserted
Node x = root;

// Pointer y maintains the trailing
// pointer of x
Node y = null;

while (x != null) {
y = x;
if (key < x.key)
x = x.left;
else
x = x.right;
}

// If the root is null i.e the tree is empty
// The new node is the root node
if (y == null)
y = newnode;

// If the new key is less then the leaf node key
// Assign the new node to be its left child
else if (key < y.key)
y.left = newnode;

// else assign the new node its right child
else
y.right = newnode;

// Returns the pointer where the
// new node is inserted
return y;
}
public void remove(Node root,int value) {

if (root == null) {
System.out.println("There are no nodes in this Binary Search Tree");
}
else
{

Node currentNode = root;
Node parentNode = null;

while (true) {

if (value > currentNode.getData()) {

if (currentNode.getRightChild() != null) {

parentNode = currentNode;
currentNode = currentNode.getRightChild();
} else {

System.out.println("No Node is present with this value");
break;
}
} else if (value < currentNode.getData()) {

if (currentNode.getLeftChild() != null) {

parentNode = currentNode;
currentNode = currentNode.getLeftChild();
} else {

System.out.println("No Node is present with this value");
break;
}
} else {

if (currentNode.getLeftChild() == null
&& currentNode.getRightChild() == null) {
if (parentNode == null) {
rootNode = null;
} else if (parentNode.getLeftChild().getData() == currentNode.getData()) {

parentNode.setLeftChild(null);
} else {
parentNode.setRightChild(null);
}
} else if (currentNode.getLeftChild() == null) {
if (parentNode == null) {
root = currentNode.getRightChild();
} else if (parentNode.getLeftChild().getData() == currentNode.getData()) {

parentNode.setLeftChild(currentNode.getRightChild());
} else {
parentNode.setRightChild(currentNode.getRightChild());
}
} else if (currentNode.getRightChild() == null) {
if (parentNode == null) {
root = currentNode.getLeftChild();
} else if (parentNode.getLeftChild().getData() == currentNode.getData()) {

parentNode.setLeftChild(currentNode.getLeftChild());
} else {
parentNode.setRightChild(currentNode.getLeftChild());
}
} else {
int successorNodeValue = getSuccessorNodeValue(currentNode.getRightChild());
delete(successorNodeValue);
currentNode.setData(successorNodeValue);
}
}
}
}
}

private int getSuccessorNodeValue(Node currentNode) {

while (true) {
if (currentNode.getLeftChild() != null) {

currentNode = currentNode.getLeftChild();
} else {
break;
}
}

return currentNode.getData();
}
public int search(Node root, int key)
{
// Traverse untill root reaches to dead end
while (root != null) {
// pass right subtree as new tree
if (key > root.data)
root = root.right;
  
// pass left subtree as new tree
else if (key < root.data)
root = root.left;
else
return key; // if the key is found return it otherwise return -1
}
return -1;
}
public Node getMax(Node root)
{
Node current = root;
  
/* loop down to find the rightmost leaf */
while (current.right != null) {
current = current.right;
}
return (current);
  
}
public Node getMin(Node root) {
Node current = root;
  
/* loop down to find the leftmost leaf */
while (current.left != null) {
current = current.left;
}
return (current);
}
}
class TreeChecker implements ITreeChecker
{
public boolean isMirror(Node root1, Node root2) {
Stack<Node> st1 = new Stack<Node> ();
Stack<Node> st2 = new Stack<Node> ();
while (true)
{
// iterative inorder traversal of 1st tree and
// reverse inoder traversal of 2nd tree
while (root1 != null && root2 != null)
{
// if the corresponding nodes in the two traversal
// have different data values, then they are not
// mirrors of each other.
if (root1.data != root2.data)
return false;
  
st1.push(root1);
st2.push(root2);
root1 = root1.left;
root2 = root2.right;
}
  
// if at any point one root becomes null and
// the other root is not null, then they are
// not mirrors. This condition verifies that
// structures of tree are mirrors of each other.
if (!(root1 == null && root2 == null))
return false;
  
if (!st1.isEmpty() && !st2.isEmpty())
{
root1 = st1.peek();
root2 = st2.peek();
st1.pop();
st2.pop();
  
/* we have visited the node and its left subtree.
Now, it's right subtree's turn */
root1 = root1.right;
  
/* we have visited the node and its right subtree.
Now, it's left subtree's turn */
root2 = root2.left;
}
  
// both the trees have been completely traversed
else
break;
}
  
// tress are mirrors of each other
return true;
}
public boolean isSame(Node root1, Node root2)
{
// Return true if both trees are empty
if (root1 == null && root2 == null) return true;
  
// Return false if one is empty and other is not
if (root1 == null || root2 == null) return false;
  
// Create an empty queues for simultaneous traversals
Queue<Node > q1 = new LinkedList<Node> ();
Queue<Node> q2 = new LinkedList<Node> ();
  
// Enqueue Roots of trees in respective queues
q1.add(root1);
q2.add(root2);
  
while (!q1.isEmpty() && !q2.isEmpty())
{
// Get front nodes and compare them
Node n1 = q1.peek();
Node n2 = q2.peek();
  
if (n1.data != n2.data)
return false;
  
// Remove front nodes from queues
q1.remove();
q2.remove();
  
/* Enqueue left children of both nodes */
if (n1.left != null && n2.left != null)
{
q1.add(n1.left);
q2.add(n2.left);
}
  
// If one left child is empty and other is not
else if (n1.left != null || n2.left != null)
return false;
  
// Right child code (Similar to left child code)
if (n1.right != null && n2.right != null)
{
q1.add(n1.right);
q2.add(n2.right);
}
else if (n1.right != null || n2.right != null)
return false;
}
  
return true;
}

}

I hope this answer is helpful to you, please upvote my answer. I'm in need of it.. Thank you..


Related Solutions

With one sentence or two explain how are the two key terms relate to each other​:...
With one sentence or two explain how are the two key terms relate to each other​: 1-Marketing (The 4P's) and Value Proposition &Value Creation: 2-Marketing (The 4P's) and Logos: 3- Marketing (The 4P's) and Organizational Culture: 4-Marketing (The 4P's) and Customer experience mapping: 5-Opportunity Cost and Marketing (The 4P's): 6-Brand Loyalty and Marketing (The 4P's): 7- Customer Relationship Building and Marketing (The 4P's): 8- Competitive Advantage and Marketing (The 4P's): 9- Digital and Online Marketing and Marketing (The 4P's):
Tell whether each of the following questions is biased or fair and why? Given the great...
Tell whether each of the following questions is biased or fair and why? Given the great tradition of space exploration in the united states, do you favor continued funding for space flights?
Define the two major categories of quality cost and how they relate to each other.
Define the two major categories of quality cost and how they relate to each other.
Write a python function comparing two binary trees and returns whethere they are same. Input two...
Write a python function comparing two binary trees and returns whethere they are same. Input two binary tress and output true or false. True= They are the same, False= They are not . Test the function .
Two investment advisers are comparing performance. One averaged a 15.16% rate of return and the other...
Two investment advisers are comparing performance. One averaged a 15.16% rate of return and the other a 20.74% rate of return. However, the β of the first investor was 1.5, whereas that of the second investor was 1. Required: Suppose that the T-bill rate was 3% and the market return during the period was 15%. Aside from the issue of general movements in the market, outline the difference between the superior and inferior portfolios. Do not round intermediate calculations. Input...
Two investment advisers are comparing performance. One averaged a 19% return and the other a 16%...
Two investment advisers are comparing performance. One averaged a 19% return and the other a 16% return. However, the beta of the first adviser was 1.5, while that of the second was 1.0. If the T-bill rate were 3% and the market return were 15%, which adviser would be the superior stock selector?
Two investment advisers are comparing performance. One averaged a 16.42% rate of return and the other...
Two investment advisers are comparing performance. One averaged a 16.42% rate of return and the other a 20.59% rate of return. However, the β of the first investor was 1.5, whereas that of the second investor was 1. Required: Suppose that the T-bill rate was 3% and the market return during the period was 15%. Aside from the issue of general movements in the market, outline the difference between the superior and inferior portfolios. _____% Do not round intermediate calculations....
1-For each of the descriptions given, tell whether this most closely describes TH1 cells, TH2 cells,...
1-For each of the descriptions given, tell whether this most closely describes TH1 cells, TH2 cells, TH17 cells, CD8+ cells, or all T cells. (a) Causes macrophage over-activation leading to granulomas in leprosy (b) Involved in asthma by producing IL-5, a growth factor for eosinophils (c) Produces IL-10, which reduces inflammatory responses (d) Produces IL-17 and IL-22 to protect against some fungal infections (e) Kills tumor cells that have tumor-specific peptides bound to their surface MHC class I molecules (f)...
Measuring the distance between two trees, you measure that there are 75.5 steps between the two...
Measuring the distance between two trees, you measure that there are 75.5 steps between the two trees. Give an estimate of the unit of measurement of one step in meters and give an estimate of what you think its uncertainty is. Give the distance that separates the trees in meters as well as feet/inches and round to the correct amount of significant digits. What would you say are the uncertainties for the these two distances
Tell me two differences and one common ideal shared between Buffon and Linnaeus.
Tell me two differences and one common ideal shared between Buffon and Linnaeus.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT