Question

In: Computer Science

Given a stack S and an element x, write a recursive algorithm that returns True if...

Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?

Solutions

Expert Solution

Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise.

Answer:

Stack is an Abstract Datatype, Hence We are assuming that a basic implementation of stack has below functionalities.

  • push() − Pushing (inserting) an element on top of the stack.

  • pop() − Removing (deleting) an element from the top of the stack.

  • isEmpty() − check if stack is empty. and return True or False

Now the basic recursive algorithm to search an item X in a stack S.

Algorithm:

begin function Search_in_stack(stack S, item X):

   if S.is_empty() equals True:   #checks if stack is empty.
      return False 
   endif

   top_element ← S.pop()          #pop and store top element in a variable

   if top_element equals X:       #checks if the item is found
      S.push(top_element)         #to undo the pop operation, we push item again
      return True
   else:
      result ← Search_in_Stack(S,X) #if item not found, we recursively call algo.
      S.push(top_element)           #no matter what, we need to push item again
      return result                 #return the result of recursive step
   endif 
   
end function

Explanation :

1. This algortihm will pop the top element of stack until the stack becomes empty or required element is found.

2. To not change the content of stack , we need to push again the item, that we've poped.

Time Complexity : Big O (n)

Stack will be poped until it is empty, or until the item is found. Hence worst case time complexity for algorithm will be O(n). Because it will pop out all the elements.

Algorithm should not change the content in Stack, so before each return statement, the item will be pushed again. So stack will be unchanged finally.

Example :

S= [3,5,-1,2], X=-1

function calls -

search_in_stack( [3,5,-1,2], -1) => search_in_stack( [5,-1,2], -1) =>  search_in_stack( [-1,2], -1) => return True(element found)

stack will be restored, because push statements will push the corresponding items before returning the results in each recursive call.


Related Solutions

Given a stack S and an element x, write a recursive algorithm that returns True if...
Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Use java .Given a stack S and an element x, write a recursive algorithm that returns...
Use java .Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm? use java
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm?
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
Write recursive method to return true if a given array has element equal to employee emp,...
Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp, and returns...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Write a modification of the recursive binary search algorithm that always returns the smallest index whose...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose element matches the search element. Your algorithm should still guarantee logarithmic runtime. Give a brief discussion of the best- and worst-case runtimes for this new algorithm as they compare to the original. NOTE: You do not have to re-write the entire algorithm. You just need to indicate any changes you would make and show the pseudocode for any portions that are changed. Example: Given...
Give a recursive algorithm to compute a list of all permutations of a given set S....
Give a recursive algorithm to compute a list of all permutations of a given set S. (That is, compute a list of all possible orderings of the elements of S. For example, permutations({1, 2, 3}) should return {〈1, 2, 3〉, 〈1, 3, 2〉, 〈2, 1, 3〉, 〈2, 3, 1〉, 〈3, 1, 2〉, 〈3, 2, 1〉}, in some order.) Prove your algorithm correct by induction.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT