We can better organize the cases if we use a top-level function find to take care...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
We can better organize the cases if we use a top-level function "find" to take care of special cases, and let it call a helper function "find-node" for the non-special cases. The reason this is an improvement is because the "find-node" function can more easily be written in a recursive style. Option #1: (find-node n v stack) Create a new parameter named "stack" that "find" will pass to "find-node" the helper function. This will be a list of nodes from root to insertion point. Initial value of "stack" is (list (bst-root t)), a list containing just the root of the tree. In the helper function, when the base cases are reached, "(cons in stack)" is the final value. For other cases, call "stack-node" function recursively with n replaced by left of n or right of n and "stack" replaced by (cons n stack). (define (find tv) (cond ((empty? (bst-root t)) null) (else (find-node (bst-root t) v null)) top-level function ; special case tree is empty general case let find-node do the work ; initial value of stack is null (define (find-node n v stack) (cond » Option #2: (find-node n v) Don't use extra parameter "stack". ; helper function ;cases go here In the base case for "find-node", the result is (list n). In the recursive case, result is the append of recursive call with (list n). Be sure to put recursive call on the left and (list n) on the right, because we want the root at the tail of the list and the insertion point at the head. (define (find tv) (cond ((empty? (bst-root t)) null) (else (find-node (bst-root t) v)) }) (define (find-node nv) (cond ; top-level function ;special case tree is empty ; general case let find-node do the work ;no parameter "stack" in option #2 ; helper function ; cases go here Convert list of nodes to list of values: (nl-to-vi x) When printing out results, it will be convenient to convert a list of nodes into a list of values. When nodes are displayed by the interpreter, each node is displayed along with any nested nodes that are connected to it, which is correct but hard to read on the screen. If we just want to check that the nodes are in the correct order, it's convenient to display a list of values of the nodes in the list, rather than the actual list of nodes. This function is merely for formatting convenience to print information out and display it. (define (nl-to-vlx) (cond " If x only contains one node Else ; base case, recursive case Take the value of the node and create a list of one item Take the value of the head node, cons it to the result of the recursive call to (cdr x) Traverse: (traverset) This function implements the recursive definition of BST in-order traversal described earlier. Its result is a list of nodes with the node values in ascending order. We will use the same trick we used for (find tv) and divide it into a top-level function and a recursive helper function. (define (traverset) ; top-level function (traverse-node (bst-root t));"traverse" calls "traverse-node" to do all the work (define (traverse-node n) (cond :recursive helper function base case, recursive case The top-level function "(traverse t)" will simply call the helper function "(traverse-node (bst-root t))" passing the root node as its parameter. In this way, the helper function can be simplified and written as a recursive function using only a node reference, without having to handle special cases involving the root of the BST structure. For reference, here is the recursive definition of traverse that we will use for the traverse-node function. If n is empty Else Return the empty list Return the append of (1) traverse on left of n, (2) list of n, and (3) traverse of right of n We can better organize the cases if we use a top-level function "find" to take care of special cases, and let it call a helper function "find-node" for the non-special cases. The reason this is an improvement is because the "find-node" function can more easily be written in a recursive style. Option #1: (find-node n v stack) Create a new parameter named "stack" that "find" will pass to "find-node" the helper function. This will be a list of nodes from root to insertion point. Initial value of "stack" is (list (bst-root t)), a list containing just the root of the tree. In the helper function, when the base cases are reached, "(cons in stack)" is the final value. For other cases, call "stack-node" function recursively with n replaced by left of n or right of n and "stack" replaced by (cons n stack). (define (find tv) (cond ((empty? (bst-root t)) null) (else (find-node (bst-root t) v null)) top-level function ; special case tree is empty general case let find-node do the work ; initial value of stack is null (define (find-node n v stack) (cond » Option #2: (find-node n v) Don't use extra parameter "stack". ; helper function ;cases go here In the base case for "find-node", the result is (list n). In the recursive case, result is the append of recursive call with (list n). Be sure to put recursive call on the left and (list n) on the right, because we want the root at the tail of the list and the insertion point at the head. (define (find tv) (cond ((empty? (bst-root t)) null) (else (find-node (bst-root t) v)) }) (define (find-node nv) (cond ; top-level function ;special case tree is empty ; general case let find-node do the work ;no parameter "stack" in option #2 ; helper function ; cases go here Convert list of nodes to list of values: (nl-to-vi x) When printing out results, it will be convenient to convert a list of nodes into a list of values. When nodes are displayed by the interpreter, each node is displayed along with any nested nodes that are connected to it, which is correct but hard to read on the screen. If we just want to check that the nodes are in the correct order, it's convenient to display a list of values of the nodes in the list, rather than the actual list of nodes. This function is merely for formatting convenience to print information out and display it. (define (nl-to-vlx) (cond " If x only contains one node Else ; base case, recursive case Take the value of the node and create a list of one item Take the value of the head node, cons it to the result of the recursive call to (cdr x) Traverse: (traverset) This function implements the recursive definition of BST in-order traversal described earlier. Its result is a list of nodes with the node values in ascending order. We will use the same trick we used for (find tv) and divide it into a top-level function and a recursive helper function. (define (traverset) ; top-level function (traverse-node (bst-root t));"traverse" calls "traverse-node" to do all the work (define (traverse-node n) (cond :recursive helper function base case, recursive case The top-level function "(traverse t)" will simply call the helper function "(traverse-node (bst-root t))" passing the root node as its parameter. In this way, the helper function can be simplified and written as a recursive function using only a node reference, without having to handle special cases involving the root of the BST structure. For reference, here is the recursive definition of traverse that we will use for the traverse-node function. If n is empty Else Return the empty list Return the append of (1) traverse on left of n, (2) list of n, and (3) traverse of right of n
Expert Answer:
Related Book For
Managerial Accounting Creating Value in a Dynamic Business Environment
ISBN: 978-0078025662
10th edition
Authors: Ronald Hilton, David Platt
Posted Date:
Students also viewed these accounting questions
-
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...
-
Read the article below and answer the questions posed, the compose response should be two to three pages in length. 1. What are 2 major planning failures pre-event at Memorial that led to it becoming...
-
In the Diffie-Hellman protocol, what happens if, x and y have the same value? That is, Alice and Bob have accidentally chosen the same number. Are the values of R1 and R2 the same? Are the values of...
-
Using its definition in terms of SS values, explain why R2 = 1 only when all the residuals are 0, and R2 = 0 when each = y. Explain what this means in practical terms.
-
Oil (absolute viscosity \(=0.0003 \mathrm{lb} \cdot \mathrm{s} / \mathrm{ft}^{2}\), density \(=50\) \(\mathrm{lbm} / \mathrm{ft}^{3}\) ) flows in the boundary layer, as shown in Fig. P1.82. The plate...
-
Indisurers Ltd finds that the variances for direct labour efficiency for processing motor insurance claims, since the beginning of the year, are as follows: The average total cost of labour...
-
The following are independent audit situations for which you will recommend an appropriate audit report from the types listed. For each situation, identify the appropriate type of audit report and...
-
Show all work for credit!!!! 2 Evaluate the line integral (2+zy) d (2 y) where C is the curve given by r(t) = (t+1)+(-2t)j, 0
-
10 points The administrator at the general hospital in Texas Presbyterian, Dallas (TPD) is ordering antidote for copperhead bites for 2023. Based on prior experience, the admin has built the...
-
Nasiraan company started operations in the current month. During the month. 10,000 units were started in Department 1. Of the units started, 1,000 units were spoiled at the end of the process which...
-
A merchandising company is considering a $1,300,000 upgrade to its retail and warehousing facilities that will allow the company to handle more products and attract more customers. The company...
-
Pappy's Toys makes two models of a metal toyStandard and DeLuxe. Both models are produced on a single machine. The price and costs of the two models appear as follows. StandardDeLuxePrice per...
-
Roscoe Construction Company (RCC) is a company specializing in building roads and highways. As a part of its business, it receives supplies of aggregate from two suppliers: Toorak Gravel (TG) and...
-
Paynesville Corporation manufactures and sells a preservative used in food and drug manufacturing. The company carries no inventories. The master budget calls for the company to manufacture and sell...
-
Discuss the key factors for future competitive success. List the common types of key success factors and answer the following questions: On what basis do buyers of the industrys product choose...
-
You purchase a bond with a coupon rate of 6.7 percent, a par value $1,000, and a clean price of $905. Assume a par value of $1,000. If the next semiannual coupon payment is due in two months, what is...
-
Do permanent differences cause the accounting carrying value and tax basis of the related item on the statement of financial position to differ?
-
A company bought \(\$ 1,000,000\) of capital assets at the beginning of year 1. Year 1 amortization was \(\$ 200,000\) and CCA was \(\$ 100,000\). What is the tax basis of the assets at the end of...
-
Explain two different approaches to account for an investment tax credit for a capital asset.
![Mobile App Logo](https://dsd5zvtm8ll6.cloudfront.net/includes/images/mobile/finalLogo.png)
Study smarter with the SolutionInn App