Concept explainers
Design a nonrecursive
def Search(Tree, TargetValue):
if (Tree is None):
return None # Search failed
elif (TargetValue = = Tree.Value):
return Tree # Search succeeded
elif (TargetValue < Tree.Value):
return Search(Tree.Left, TargetValue)
# Apply the function Search to see if TargetValue is in the subtree identified by the root's left child pointer, and report the result of that search.
elif (TargetValue > Tree.Value):
return Search(Tree.Right, TargetValue)
# Apply the function Search to see if TargetValue is in the subtree identified by the ‘ right child pointer, and report the result of that search.
Figure 8.21
The binary search as it would appear if the list were implemented as a linked binary tree
Want to see the full answer?
Check out a sample textbook solutionChapter 8 Solutions
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Additional Engineering Textbook Solutions
Experiencing MIS
C How to Program (8th Edition)
C++ How to Program (10th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Concepts of Programming Languages (11th Edition)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
- please answer in scheme racket flavor 1. Create the recursive function far-left that will find the left-most value 2.Create the recursive function far-right that will find right-most value 3. Write depth, a procedure that takes a tree as an argument and returns the largest number of nodes connected through parent-child links. That is, a leaf node has depth 1; a tree in which all the children of the root node are leaves has depth 2. Our world tree has depth 4 (because the longest path from the root to a leaf is, for example, world, country, state, city). 4. Write count-nodes, a procedure that takes a tree as an argument and returns the total number of nodes in the tree. (Earlier we counted the number of leaf nodes.) (define (leaf? node) (null? (children node))) (define (datum node) (car node)) (define (children node) (cdr node)) (define (count-leaves tree) (if (leaf? tree) 1 (count-leaves-in-forest (children tree)) ) ) (define (count-leaves-in-forest forest) (if (null? forest) 0 (+…arrow_forwardTrue/False 6. A sequence can be viewed as a recursive data collection.arrow_forwardRecursive Functions 1. Without looking at the standard prelude, define the following library functions using recursion: o Decide if all logical values in a list are true: and [Bool] -> Bool o Concatenate a list of lists: concat: [[a]] -> [a] o Produce a list with n identical elements: replicate :: Int -> a => [a] o Select the nth element of a list: (!!) [a] -> Int -> a o Decide if a value is an element of a list: elem: Eq a => a => [a] -> Boolarrow_forward
- Implements the clone_node function that duplicates. If the node has a next it also duplicates it Hint: implement the following recursive pseudocode function clone_node(node) if node is NULL returns NULL returns new_node(node.value, clone_node(node.next)) Complete the code: def clone_node(node): r = <... YOUR CODE HERE ...> return rarrow_forward1. Recursion & List processing Write a recursive function flatten that takes a list as an argument and returns the flat version of that list. For example, (flatten '(a (b c) d)) -> (a b c d) (flatten '(((a)) (b (c)) d)-> (a b c d)arrow_forwardPart A: Maze exploration using recursive function (30%)A Maze is given as N*N binary matrix of blocks where source block is the upper leftmost block i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N1][N-1]. A rat starts from source and has to reach the destination. The rat can move infour directions: left, right, up or down.In the maze matrix, 0 means the block is a dead end and 1 means the block can beused in the path from source to destination. Backtracking is an algorithmic techniquefor solving problems recursively by trying to build a solution incrementally in mazeexploration.Suggested Approach: Form a recursive function, which will follow a pathand check if the path reaches the destination or not. If the path does notreach the destination then backtrack and try other paths.Algorithm:1. Create a solution matrix, initially filled with 0’s.2. Create a recursive function, which takes initial matrix, output matrixand position of rat (i, j).3. if the position…arrow_forward
- 1. Recursive Algorithm for Sequential Search(L,i,j,x)arrow_forwardАСTIVITY 3 RECURSION Instructions: Use any size of bond paper. Write your name, course and year, date in your answer sheets. Use ALL CAPS. I. Recursion: Prove the following using recursion. 1. Ifg is defined recursively by g(0) = 3 and g(n + 1) = 2" – n. Find g(1), g(3), g(5), g(7) and g(9)arrow_forwardWhy does dynamic programming provide faster solutions that recursive algorithms solving the same problem? 1.avoids resolving overlapping subproblems 2.dynamic programming uses dynamic memory that is more efficient than stack memory 3.loops are always faster than recursion 4.dynamic uses arrays that are faster than function callsarrow_forward
- 1. Give a recursive algorithm in pseudocode to compute the diameter of a binary tree. Recall that diameter is defined as the number of nodes on the longest path between any two nodes in the tree. Nodes have left and right references, but nothing else. You must use the height function, defined as follows, in your solution. Your solution will return the diameter of the tree as an integer. function height (Node n) 1. if n = null 2. return -1 3. return 1 + max (height (n.left), height (n.right)) Write your solution below. function diameter (Node n)arrow_forwardDesign a top-down algorithm (based on dynamic programming and recursion; i.e. memory function) for checking the existence of a winning strategy for the “Rocks” game. Hints: • Give it the name: RocksMF(n,m) where n,m are the number of rocks on the two piles • Use recursion to call the algorithm itself to solve smaller subproblems • Use a global variable for the two-dimensional tablearrow_forwardMazeSolver attempts to recursively traverse a Maze. The goal is to get from the * given starting position to the bottom right, following a path of 1’s. Arbitrary * constants are used to represent locations in the maze that have been TRIED * and that are part of the solution PATH.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning