We can define a general rooted tree to be a tree that has any number of...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
We can define a general rooted tree to be a tree that has any number of subtrees hanging from each node, not just two. To implement such a tree, we can just define a list of subtrees in each node: datatype 'a tree= Node of 'a 'a tree list; (A pair: Note no 'I' *) We won't need Empty here because we can just use nil) in the second slot for leaves of the tree, and to keep things simple for this problem we're not going to worry about having an altogether empty tree. You only have to process Nodes. A sample definition of a tree would be: Node(1, [Node(2, nil), Node(3, [Node(4, nil), Node(5, [Node(7,nil)]), Node (6,nil)])]) val t- which looks like the following: 2 - 1 treesum t; val it 28: int; 4 3 5 7 6 Implement a function, treesum, that adds up all the numbers in a non-empty general rooted tree of ints: We can define a general rooted tree to be a tree that has any number of subtrees hanging from each node, not just two. To implement such a tree, we can just define a list of subtrees in each node: datatype 'a tree= Node of 'a 'a tree list; (A pair: Note no 'I' *) We won't need Empty here because we can just use nil) in the second slot for leaves of the tree, and to keep things simple for this problem we're not going to worry about having an altogether empty tree. You only have to process Nodes. A sample definition of a tree would be: Node(1, [Node(2, nil), Node(3, [Node(4, nil), Node(5, [Node(7,nil)]), Node (6,nil)])]) val t- which looks like the following: 2 - 1 treesum t; val it 28: int; 4 3 5 7 6 Implement a function, treesum, that adds up all the numbers in a non-empty general rooted tree of ints:
Expert Answer:
Answer rating: 100% (QA)
The following is a pseudocode implementation of the treesum function def treesumnode ... View the full answer
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
Demanding Nursing profession. It has become an especially challenging hiring and ongoing management category in recent years. Covid, overall workload demands, advanced education requirements, and...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
List three specific parts of the Case Guide, Objectives and Strategy Section (See below) that you had the most difficulty understanding. Describe your current understanding of these parts. Provide...
-
The AND function can be realized by using only n number of NOR gates. The value of nequal to
-
A researcher tests whether cocaine use increases impulsive behavior in a sample of cocaine-dependent and cocaine-inexperienced mice. (a) Identify the independent variable in this study. (b) Identify...
-
American Chip Corporation's fiscal year-end is December 31. The following is a partial adjusted trial balance as of December 31, 2018. Required: Prepare the necessary closing entries at December 31,...
-
Suppose Apex Electrical Supplys Inventory account showed a balance of \($43,000.\) A physical count showed \($41,800\) of goods on hand. To adjust the inventory account, Apex Electrical Supply, Inc.,...
-
On January 1, 2017, Schipper Ltd. had the following shareholders' equity accounts: Common shares (1,000,000 issued)......................$1,500,000 Retained...
-
(1 point) Suppose that f(x, y, z) = xyz xyz is a function of three variables. 1. Find the gradient of f(x, y, z). Answer: Vf(x, y, z) = - 2. Evaluate the gradient at the point P(2, -2, -1). Answer:...
-
You have just been hired as a brand manager at Kelsey-White, an American multinational consumer goods company. Recently the firm invested in the development of K-W Vision, a series of systems and...
-
You are driving up a long, inclined road. After 1.1 miles you notice that signs along the roadside indicate that your elevation has increased by 550 ft. a. What is the angle of the road above the...
-
Why cant EPL teams like Arsenal exert as much monopoly power as the NFLs Chicago Bears?
-
Despite the wording of most statutes proscribing the offense of escape, courts increasingly require the prosecution to prove the defendants specific intent to avoid lawful confinement. Are courts...
-
Use a standard monopoly firm graph to show and explain how the monopoly power of a team is changed when another team locates nearby.
-
Jurors are generally selected from among those citizens who have registered to vote or who have registered motor vehicles. Do these methods produce juries drawn from a representative cross-section of...
-
Do you think that the introduction of a victim impact statement during the sentencing phase of a capital trial is appropriate, or do you think it might inject too much emotionalism into the process?
-
You have been asked to calculate the enthalpy change in the following reaction: 2Al(s) + Fe2O3(s) Al2O3(s) + 2Fe(s) The reaction occurs in the following stages: 1. 2Al(s) + Fe2O3(s) Al2O3(s) + 2Fe(1)...
-
DEPARTMENT DATA EMPLOYEE DATA EmployeeNumber FirstName Mary Rosalie Richard George Alan 3 4 5 7 8 9 855555ES 12 13 14 15 16 17 Create the database tables in SQL or ACCESS: 18 19 20 PROJECT DATA Ken...
-
How can we use the output of the Floyd-Warshall algorithm to detect the presence of a negative-weight cycle?
-
Show that case 3 of the master theorem is overstated, in the sense that the regularity condition af (n/b) cf (n) for some constant c < 1 implies that there exists a constant > 0 such that f (n) =...
-
Show how to implement the generic push-relabel algorithm using O(V) time per relabel operation, O(1) time per push, and O(1) time to select an applicable operation, for a total time of O(V 2 E).
-
Which of the following is not advice for closing projects or phases? A. You dont need to celebrate completing a project, especially if it did not go well. B. You should capture lessons learned as...
-
What are the six different versions of project management software offered by Microsoft as of April 2016? Briefly describe how they differ and who uses them.
-
A __________ is a procedure that has been shown by research and experience to produce optimal results and that is established or proposed as a standard suitable for widespread adoption. A. benchmark...
Study smarter with the SolutionInn App