I am having trouble with my constructors for this project. If you are able, coul
ID: 3768850 • Letter: I
Question
I am having trouble with my constructors for this project. If you are able, could you please give me some direction on how to complete these constructors and also how to implement the 3rd constructor (copy constructor). It is supposed to be copied using a deep copy not a shallow copy. For example, if the splay tree had around 10 different pieces of data stored in it, how could I iterate over each node in the tree and copy them to the new tree? I am not looking for anyone to do my homework specifically, but if you could give me some direction on how to approach this and possibly provide an algorithm or pseudocode to implement it, that would be great! Thanks in advance!
2.1 Tree Construction, Methods, and Iterator Three constructors are provided for the SplayTree class public SplayTree() public SplayTree (E data) public SplayTree(SplayTree tree) The first constructor creates an empty tree (with null root). The second constructor merely creates the tree root to store given data. The rest of the tree needs to be constructed by repeatedly calling the following method which performs a binary search tree addition: public boolean addBST(E data) For efficiency of tree initialization, the method does not splay at the newly created node. The third constructor copies over the structure of an existing splay tree. Tree operations shall be implemented following their descriptions in the lecture notes. Also, refer to their javadocs for extra requirements if any An iterator method does not splay The class has a private iterator class SplayTreeIterator. at any node.Explanation / Answer
Please find the required solution:(solution in bold)
import java.util.AbstractSet;
import java.util.Iterator;
public class SplayTree<E extends Comparable<? super E>> extends AbstractSet<E> {
protected Node root;
protected int size;
private class Node {
public E data;
public Node left;
public Node parent;
public Node right;
public Node(E data) {
this.data = data;
}
@Override
protected Node clone() throws CloneNotSupportedException {
return new Node(data);
}
Node copyNodes() {
if (this.left != null) {
Node left = this.left.copy();
}
if (this.right != null) {
Node right = this.right.copy();
}
return new Node(data);
}
}
public SplayTree() {
size = 0;
root = null;
}
public SplayTree(E data) {
Node newNode = new Node(data);
root = newNode;
addBST(root.data);
}
//copy using Node copy
public SplayTree(SplayTree<E> tree) {
SplayTree<E> newTree = new SplayTree<E>();
newTree.size = size;
Node newRoot=tree.root.copyNodes();
newTree.root=newRoot;
}
public boolean addBST(E data) {
if (root == null) {
root = new Node(data);
++size;
return true;
}
Node current = root;
while (true) {
int compare = current.data.compareTo(data);
if (compare == 0)
return false;
else if (compare > 0) {
if (current.left != null)
current = current.left;
} else {
current.left = new Node(data);
{
size++;
return true;
}
}
}
}
@Override
public Iterator<E> iterator() {
return null;
}
@Override
public int size() {
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.