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)
- 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_forwardAdd a recursive function to BST called avgCompares() that computes the average number of comparisons required by a random search hit in a particular BST (the internal path length of the tree divided by its size plus one). Create two implementations: a recursive approach (which requires linear time and space proportionate to the height) and a way similar to size() that adds a field to each node in the tree (which requires linear space and constant time each query).arrow_forwardConsider the following recursive function to compute factorialint fact (int n) {if (n <= 1)return 1;elsereturn n * fact(n-1);}int main() {cout << fact(5) << endl;return 0;} Show the stack of activation records each time the function fact is called (either directly or recursively). Each activation record needs to be identified by the name of the function and the parameter value, but there is no need to elaborate the specific content of each activation record.arrow_forward
- Recursive 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_forwardAvgCompares(), a recursive function that calculates the average number of comparisons needed by a random search hit in a given BST (the internal path length of the tree divided by its size plus one), should be added to the BST. Create two implementations: a recursive method that adds a field to each node in the tree and takes linear space and constant time every query, and a method similar to size() that takes linear space and constant time per query.arrow_forwardCompare top-down table parsing versus recursive descent.arrow_forward
- C++ Recall that a "leaf" node in a binary tree is one which has no child nodes, and the height of a tree is the maximum number of edges from its root to one of its leaves). We also saw how easily a binary tree can become poorly structured, where the heights to various leaves in the tree vary dramatically. A full binary tree is one where each node other than the leaf nodes has two child nodes, Write a recursive function which returns true if a binary tree is full, false otherwise. Please avoid the use of variables declared outside of the function.arrow_forwardThis essay will compare and contrast recursive descent with top-down table parsing.arrow_forwardTo accomplish recursion termination, explain three forms of recursion, each with a high-level description and a specific technique that falls into each category.arrow_forward
- Draw a recursion tree for the following function for myFunc(5), then give the output of the int myFunc(int n){ if (n==1) return 1; else return (myFunc(n-1) + n); }arrow_forwardGiven below is a recursive function fib for computing the nth fibonacci number. Draw the recursive tree for computing fib(4) def fib(n): if n <= 1: return 1 else: return fib(n - 1) + fib(n - 2)arrow_forwardDescribe the advantages that a recursive algorithm can give. What are the disadvantages of utilizing recursion?arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning