Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed
image text in transcribed
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

(2, 3) Plot the given points in a coordinate system.

Answered: 1 week ago