Question
Due to the changes, the original implementation of the following methods will no longer work. Please based on this change, make necessary changes to the
Due to the changes, the original implementation of the following methods will no longer work. Please based on this change, make necessary changes to the following methods
public int size() //return the number of nodes
public Node
Add and implement the following methods within the class:
public boolean isIdentical(Node
public ArrayList
public Node
package simpleBinaryTree;
import java.util.ArrayList;
public class testClass {
public static void main(String[] args)
{
simpleLinkedBinaryTree
myBtree.addRoot(5);
Node
myBtree.addLeft(left, 8);
myBtree.addRight(left, 10);
ArrayList
for(Node
System.out.print(p.getElement()+"\t");
System.out.println();
System.out.println("Number of Node on the tree:\t"+myBtree.size());
simpleLinkedBinaryTree
System.out.println("Those two trees are identical:\t"+myBtree.isIdentical(myBtree.root, oBtree.root));
System.out.println("The parent node of "+ left.getElement() +"\t is" + myBtree.parent(myBtree.root, left));
ArrayList
for(Integer i: ancestorlist) System.out.print(i+"\t");
}
}
-----------------------------------------------------------------
package simpleBinaryTree;
import java.util.ArrayList;
public class simpleLinkedBinaryTree
protected Node
public simpleLinkedBinaryTree() { }//constructor_Construts an empty binary tree.
// Returns the root of the tree (or null if tree is empty).
public Node
return root;
}
public boolean isEmpty() { return root == null; }
public int numChildren(Node
int count=0;
if(p.getLeft()!=null) count++;
if(p.getRight()!=null) count++;
return count;
}
public ArrayList
ArrayList
if (left(p) != null)
snapshot.add(left(p));
if (right(p) != null)
snapshot.add(right(p));
return snapshot;
}
public boolean isInternal(Node
public boolean isExternal(Node
public Node
return p.getLeft();
}
public Node
return p.getRight();
}
// update methods supported by this class
/**
* Places element e at the root of an empty tree and returns its new Position.
*
* @param e the new element
* @return the Position of the new element
* @throws IllegalStateException if the tree is not empty
*/
public Node
if (!isEmpty()) throw new IllegalStateException("Tree is not empty");
root = new Node
return root;
}
/**
* Creates a new left child of Position p storing element e and returns its Position.
*/
public Node
throws IllegalArgumentException {
if (p.getLeft() != null)
throw new IllegalArgumentException("p already has a left child");
Node
p.setLeft(child);
return child;
}
/**
* Creates a new right child of Position p storing element e and returns its Position.
*/
public Node
throws IllegalArgumentException {
if (p.getRight() != null)
throw new IllegalArgumentException("p already has a right child");
Node
p.setRight(child);
return child;
}
/**
* Replaces the element at Position p with element e and returns the replaced element.
*/
public E set(Node
E temp = p.getElement();
p.setElement(e);
return temp;
}
public void attach(Node
simpleLinkedBinaryTree
if (isInternal(p)) throw new IllegalArgumentException("p must be a leaf");
if (!t1.isEmpty()) { // attach t1 as left subtree of node
p.setLeft(t1.root);
t1.root = null;
}
if (!t2.isEmpty()) { // attach t2 as right subtree of node
p.setRight(t2.root);
t2.root = null;
}
}
public E remove(Node
if (numChildren(p) == 2)
throw new IllegalArgumentException("p has two children");
Node
if (p == root)
root = child; // child becomes root
else {
Node
if (p == parent.getLeft())
parent.setLeft(child);
else
parent.setRight(child);
}
E temp = p.getElement();
p.setElement(null); // help garbage collection
p.setLeft(null);
p.setRight(null);
return temp;
}
//pre_order traversal
private void preorderSubtree(Node
snapshot.add(p); // for preorder, we add position p before exploring subtrees
for (Node
preorderSubtree(c, snapshot);
}
public ArrayList
ArrayList
if (!isEmpty())
preorderSubtree(root(), snapshot); // fill the snapshot recursively
return snapshot;
}
//in_order traversal
private void inorderSubtree(Node
if (left(p) != null)
inorderSubtree(left(p), snapshot);
snapshot.add(p);
if (right(p) != null)
inorderSubtree(right(p), snapshot);
}
public ArrayList
ArrayList
if (!isEmpty())
inorderSubtree(root(), snapshot); // fill the snapshot recursively
return snapshot;
}
//post_order traversal
private void postorderSubtree(Node
for (Node
postorderSubtree(c, snapshot);
snapshot.add(p); // for postorder, we add position p after exploring subtrees
}
public ArrayList
ArrayList
if (!isEmpty())
postorderSubtree(root(), snapshot); // fill the snapshot recursively
return snapshot;
}
/****************************************************************************************/
/*****************Your LAB starts here!******************/
/****************************************************************************************/
//Returns the number of nodes in the tree.
public int size() {
}
//Returns the Node of p's parent (or null if p is root).
public Node
}
public boolean isIdentical(Node
}
public ArrayList
{
return null;
}
}
-------------------------------------------------------------------------------
package simpleBinaryTree;
public class Node
private E element; // an element stored at this node
private Node
private Node
/**
* Constructs a node with the given element and neighbors.
*
* @param e the element to be stored
* @param leftChild reference to a left child node
* @param rightChild reference to a right child node
*/
public Node(E e, Node
element = e;
left = leftChild;
right = rightChild;
}
// accessor methods
public E getElement() { return element; }
public Node
public Node
// update methods
public void setElement(E e) { element = e; }
public void setLeft(Node
public void setRight(Node
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started