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
EBK COMPUTER SCIENCE: AN OVERVIEW
Additional Engineering Textbook Solutions
Degarmo's Materials And Processes In Manufacturing
Starting Out With Visual Basic (8th Edition)
Starting Out with Python (4th Edition)
HEAT+MASS TRANSFER:FUND.+APPL.
SURVEY OF OPERATING SYSTEMS
Elementary Surveying: An Introduction To Geomatics (15th Edition)
- Given the following infix expression: (28 * (33 - 3) + 10) / (4 * 0.5) % 4 Give the equivalent postfix expression Draw the tree corresponding to the recursive calls to evaluate the postfix expression. What is the depth of your tree?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_forwardWhen iterating over a hierarchical data structure, such as a tree,Group of answer choices 1. Iterating must be done recursively and it must start at the root, visiting each node once. 2. Iterating must start at the children, and must be done with recursion. 3. Iterating starts at the root but can continue depth first or breadth first, and must be done recursively. 4. Iterating must start at the root and it must traverse nodes exactly once.arrow_forward
- Design 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_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_forward1. 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_forward
- In divide and conquer algorithm after dividing the problem into two or more smaller subproblems, the next step will be: Conquer the subproblems by solving them recursively. O Combine the solutions to the subproblems into the solutions for the original problem. O Skip Conquer the subproblems by solving them respectively. Combine the solutions to the subproblems and leave them separated.arrow_forwardQuestion 5: 2.1 Sum Problem View Past Answers Given a positive number n, the sum of all digits is obtained by adding the digit one-by-one. For example, the sum of 52634 is 5+ 2 +6 +3 + 4 = 20. Write a recursive and iterative function sum (n) to compute the sum of all the digits in n. You may assume that n > 0. template.py 1 def sum R(n): if len(str(n)) == 1: 2- return n else: return 20 7. def sum_I(n): add = 0 8 while n: 10 add += n % 10 n //= 10 return add 11 12arrow_forwardalgorithm to the given array "arr" following alphabetical order (a < barrow_forwardRECURSIVE PYTHON The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the sum of the previous two, for example: 0, 1, 1, 2, 3, 5, 8, 13. Complete the fibonacci() function, which takes in an index, n, and returns the nth value in the sequence. Any negative index values should return -1. Ex: If the input is: 7 the output is: fibonacci(7) is 13 Note: Use recursion and DO NOT use any loops. # TODO: Write recursive fibonacci() functiondef fibonacci(): if __name__ == "__main__": start_num = int(input()) print('fibonacci({}) is {}'.format(start_num, fibonacci(start_num)))arrow_forwardRecursive function tracing: drawing Recursion Tree for Smallest(a, 0, 6), where vector a contains the following numbers: a = {4, 5, 10, 1, 20, 23, 2}. • clearly label each recursive call’s parameters• clearly label what each call returns to its caller //Return smallest element in sublist a[first...last] int Smallest (vector<int> a, int first, int last){ if (first==last) return a[first]; mid = (first+last)/2; //integer division l1 = Smallest(a, first, mid); l2 = Smallest (a, mid+1, last); if (l1>l2) return l2; else return l1; }arrow_forwardArtificial Intelligence (Part - 1) ==================== The Towers of Hanoi is a famous problem for studying recursion in computer science and searching in artificial intelligence. We start with N discs of varying sizes on a peg (stacked in order according to size), and two empty pegs. We are allowed to move a disc from one peg to another, but we are never allowed to move a larger disc on top of a smaller disc. The goal is to move all the discs to the rightmost peg (see figure). To solve the problem by using search methods, we need first formulate the problem. Supposing there are K pegs and N disk. (1) Propose a state representation for the problem?arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning