Question: Figure 6.2 is the function shown. what other information do you need? 6.4: Write a function that takes as input a general tree and returns

 Figure 6.2 is the function shown. what other information do you
need? 6.4: Write a function that takes as input a general tree
Figure 6.2 is the function shown. what other information do you need?

6.4: Write a function that takes as input a general tree and returns the number of nodes in that tree. Write your function to use the GenTree and GTNode ADTs of Figure 6.2. /** General tree node ADT */ interface GTNode { public E value(); public boolean isLeaf (); public GTNode parent(); public GTNode leftmost Child(); public GTNode rightSibling(); public void setValue (E value); public void setParent (GTNode par); public void insertFirst (GTNode n); public void insertNext (GTNode n); public void removeFirst(); public void removeNext(); } /** General tree ADT */ interface GenTree public void clear(); // clear the tree public GTNode root(); // Return the root 1/ Make the tree have a new root, give first child and sib public void newroot (E value, GTNode first, GTNode sib); public void newleftchild (E value); // Add left child } Figure 6.2 Interfaces for the general tree and general tree node One choice would be to provide a function that takes as its parameter the index for the desired child. That combined with a function that returns the number of children for a given node would support the ability to access any node or process all children of a node. Unfortunately, this view of access tends to bias the choice for node implementations in favor of an array-based approach, because these functions favor random access to a list of children. In practice, an implementation based on a linked list is often preferred. An alternative is to provide access to the first (or leftmost) child of a node, and to provide access to the next (or right) sibling of a node. Figure 6.2 shows class declarations for general trees and their nodes. Based on these two access functions, the children of a node can be traversed like a list. Trying to find the next sibling of the rightmost sibling would return null. 6.4: Write a function that takes as input a general tree and returns the number of nodes in that tree. Write your function to use the GenTree and GTNode ADTs of Figure 6.2. /** General tree node ADT */ interface GTNode { public E value(); public boolean isLeaf (); public GTNode parent(); public GTNode leftmost Child(); public GTNode rightSibling(); public void setValue (E value); public void setParent (GTNode par); public void insertFirst (GTNode n); public void insertNext (GTNode n); public void removeFirst(); public void removeNext(); } /** General tree ADT */ interface GenTree public void clear(); // clear the tree public GTNode root(); // Return the root 1/ Make the tree have a new root, give first child and sib public void newroot (E value, GTNode first, GTNode sib); public void newleftchild (E value); // Add left child } Figure 6.2 Interfaces for the general tree and general tree node One choice would be to provide a function that takes as its parameter the index for the desired child. That combined with a function that returns the number of children for a given node would support the ability to access any node or process all children of a node. Unfortunately, this view of access tends to bias the choice for node implementations in favor of an array-based approach, because these functions favor random access to a list of children. In practice, an implementation based on a linked list is often preferred. An alternative is to provide access to the first (or leftmost) child of a node, and to provide access to the next (or right) sibling of a node. Figure 6.2 shows class declarations for general trees and their nodes. Based on these two access functions, the children of a node can be traversed like a list. Trying to find the next sibling of the rightmost sibling would return null

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!