[Solved] OPTIONS B1 AND B2
5.0
1 customer review
Digital download
Digital download
$25.00
Message us on WhatsApp for payment or download support.
boolean deleted; // true if the node has been removed from the tree
The idea is that when a client asks this class to delete a node, all we will do is set this boolean to true and return without further ado.
You will have to rename the classes from FHtreeNode and FHtree (from the lectures) to FHsdTreeNode and FHsdTree, respectively, to distinguish them from the normal-deletion implementations in those lectures.
Thats it in a nutshell. The purpose of this new, deleted, member is to avoid ever physically removing a node when the client wants to delete it. Instead we just set deleted = true and it will be considered gone.
Of course, this is like passing a law. Unless everyone obeys the law, it wont have any effect. So we have to modify most of the methods to take advantage of it.
Some might be easier, some not. For example, remove() may be simpler and shorter than before, since, once it finds the node, all it has to do is set deleted = true no actual unlinking of the nodes, destruction of objects, etc. Other methods, like find(), will be more subtle. Finding a node means more than matching the data ( if (x.equals(root.data)) ). Even though we might find the data, x, in the tree, this does not mean x is really there. It may have been deleted at some point in the past. Remember: now, deleting it leaves it physically in the tree with the deleted flag set. You have to add a second test, if (deleted) , before you know whether or not you have really found x. Same thing with display(), size(), etc. They will only report on the virtual tree, not the physical one that is stored in memory. If a node is deleted, it may not be counted or displayed by the various methods.
On the other hand, there will be some new methods that dont have general tree counterparts. sizePhysical() will report on the size of the tree, counting both deleted and non-deleted nodes (so it will basically be exactly our old size() method. displayPhysical() will likewise show everything, including deleted nodes (it will not be required to show the deleted status for this assignment, although thats a nice touch). displayPhysical(), also, can be engineered to reuse the code of the old display() method since that method, by virtue of its ignorance of the newly added deleted flag, already shows all the nodes.
In soft deletion, trees dont normally ever shrink; they keep getting larger. Even though the client may be adding and removing the same number of objects, the add()s will always add nodes, but the remove()s will never take nodes away, just mark them as deleted. This is the meaning and expected behavior of soft deletion. It is used when normal deletion (which we can now call hard or physical deletion to emphasize the difference) has some disruptive effect on the tree and we want to avoid such physical disruption for some reason.
[When we get to binary trees and probing hash tables in the next course, we will be able to re-use the deleted nodes when a client wants to add something that is physically present but marked as deleted. However, for general trees, this refinement is both trickier and less useful, so there is no need for us to burden ourselves with that technique.]
Finally, we will need a collectGarbage() method that goes through the entire tree and physically removes all deleted nodes. This is the only method that will shrink the tree size and bring its physical and virtual contents, at least temporarily, into alignment.
For your convenience, the complete listing of the hard-deletion-based general tree presented in the lectures can he downloaded here
zipped FHgTree files (Links to an external site.)
Links to an external site.
Download the .zip file, unziop it and drag a copy of FHtree.java, FHtreeNode.java and Traverser.java into your projects src folder. Then, incorporate these classes into your project (in Eclipse, use File Refresh). Also, copy and past the contents of the client, Foothill.java into your main .java file. If your main class is not named Foothill, make adjustments, accordingly. If you compile and run the project after doing this, you should get the original output as shown in the modules.
Understand the Client
The client main() that is provided in the lesson (and in your zipped download file, above) will eventually need to be modified in two ways for this assignment:
import java.text.*;import java.util.*;//------------------------------------------------------public class Foothill{ // ------- main -------------- public static void main(String[] args) throws Exception { FHsdTree<String> sceneTree = new FHsdTree<String>(); FHsdTreeNode<String> tn; System.out.println("Starting tree empty? " + sceneTree.empty() + "n"); // create a scene in a room tn = sceneTree.addChild(null, "room"); // add three objects to the scene tree sceneTree.addChild(tn, "Lily the canine"); sceneTree.addChild(tn, "Miguel the human"); sceneTree.addChild(tn, "table"); // add some parts to Miguel tn = sceneTree.find("Miguel the human"); // Miguel's left arm tn = sceneTree.addChild(tn, "torso"); tn = sceneTree.addChild(tn, "left arm"); tn = sceneTree.addChild(tn, "left hand"); sceneTree.addChild(tn, "thumb"); sceneTree.addChild(tn, "index finger"); sceneTree.addChild(tn, "middle finger"); sceneTree.addChild(tn, "ring finger"); sceneTree.addChild(tn, "pinky"); // Miguel's right arm tn = sceneTree.find("Miguel the human"); tn = sceneTree.find(tn, "torso", 0); tn = sceneTree.addChild(tn, "right arm"); tn = sceneTree.addChild(tn, "right hand"); sceneTree.addChild(tn, "thumb"); sceneTree.addChild(tn, "index finger"); sceneTree.addChild(tn, "middle finger"); sceneTree.addChild(tn, "ring finger"); sceneTree.addChild(tn, "pinky"); // add some parts to Lily tn = sceneTree.find("Lily the canine"); tn = sceneTree.addChild(tn, "torso"); sceneTree.addChild(tn, "right front paw"); sceneTree.addChild(tn, "left front paw"); sceneTree.addChild(tn, "right rear paw"); sceneTree.addChild(tn, "left rear paw"); sceneTree.addChild(tn, "spare mutant paw"); sceneTree.addChild(tn, "wagging tail"); // add some parts to table tn = sceneTree.find("table"); sceneTree.addChild(tn, "north east leg"); sceneTree.addChild(tn, "north west leg"); sceneTree.addChild(tn, "south east leg"); sceneTree.addChild(tn, "south west leg"); System.out.println("n------------ Loaded Tree ----------------- n"); sceneTree.display(); sceneTree.remove("spare mutant paw"); sceneTree.remove("Miguel the human"); sceneTree.remove("an imagined higgs boson"); System.out.println("n------------ Virtual (soft) Tree --------------- n"); sceneTree.display(); System.out.println("n----------- Physical (hard) Display ------------- n"); sceneTree.displayPhysical(); System.out.println("------- Testing Sizes (compare with above) -------- n"); System.out.println("virtual (soft) size: " + sceneTree.size() ); System.out.println("physiical (hard) size: " + sceneTree.sizePhysical() ); System.out.println("------------ Collecting Garbage ---------------- n"); System.out.println("found soft-deleted nodes? " + sceneTree.collectGarbage() ); System.out.println("immediate collect again? " + sceneTree.collectGarbage() ); System.out.println("--------- Hard Display after garb col ------------ n"); sceneTree.displayPhysical(); System.out.println("Semi-deleted tree empty? " + sceneTree.empty() + "n"); sceneTree.remove("room"); System.out.println("Completely-deleted tree empty? " + sceneTree.empty() + "n"); sceneTree.display(); }}OPTION B-1 (Intermediate) This is instead of, not in addition to, Option A. Implement the above soft deletion using derived class generics:
public class FHsdTreeNode<E> extends FHtreeNode<E> public class FHsdTree<E> extends FHtree<E> implements CloneableIn this solution, you may not touch a single character or the base classes download them by using the same link given in Option A, but dont modify them after they are copied into your project directory. Thus your project will have the old FHtree.java and FHtreeNode.java files, as well as new file FHsdTreeNode.java and FHsdTree.java files that you will write. These two files will have the derived classes FHsdTreeNode and FHsdTree defined. This is a realistic approach that prepares you for a situation in which you do not have access to the original source. The only members that the derived class may have are as follows: Class FHsdTreeNode: