You are on page 1of 5

Recursion

Problem 1- Towers of Hanoi puzzle[O(2n)]:


1- Shift “N-1” disks from ‘A’ to ‘B’, using tower ‘C’

2- Shift the disk from ‘A’ to ‘C’

3- Shift ‘N-1’ disks from ‘B’ to ‘C’, using ‘A’

Problem 2- check whether the array is sorted[O(n)]:


1- Assign the base case to: length of array <= 1
2- Check if the first element is <= to the second:
• True: perform a recursive call with array[1:]
• False: the array is not sorted
3- Step 2 will be repeated when all the elements are sorted

Problem 3- Get the factorial[O(n)]:


1- Assign the base case to: n = 0 this will return 1
2- In recursive case n > 0 the function will call itself
to find (n-1) and multiply that by n
Linked-List

Problem 1- Implement stack using linked-list:


- First, we will make a node of singly linked list
- Push operation implementation same as insert at
beginning:
• Create a node
• Point the Node to the stack head
• Make the node is the top element
- Pop operation is implemented by deleting the node from
beginning:
• Check if the stack is empty(error)
• Assign top element to variable(temp)
• Set the head value to temp.next
Time complexity: book(pg:101)

Problem 2- find the nth node from the end of linked-list:


1- Calculate the length of the linked list = Len
2- Print the (Len – n+ 1) th node from the beginning
Time complexity: O(M) where M is the size of linked list
Stack
Problem 1- Reverse Stack elements[O(n2)]:
1- First, pop all the elements from the stack till it becomes empty
2- For each upward step in recursion, insert the element at the
bottom of the stack

Problem 2- Check whether the string is


palindrome[O(n)]:
1- Make to indexes one at the beginning ‘i’and the other at the
end ‘j’
2- Compare i and j values if they are the same:
True: increment ‘i’, decrease ‘j’
False: the string is not palindrome
3- If ‘i’ and ‘j’ met at the middle(X) the n the string is
palindrome.
Code in pg: 110

Problem 3- checking balancing of symbols[O(n)]:


1- Create a stack
2- Iterate for symbols in the given string
a. Ignore any symbol expect the parentheses
b. Push the symbol to the stack if it is opening (,{,[
c. If it closing, check if the stack empty report an error, if
not pop the stack
d. If the popped symbol does not correspond to an opening
symbol, report, and error
3- At the end, if the stack is empty then the symbols are balanced

Problem 4- Postfix evaluation:


.Step 1: Scan the Infix Expression from left to right
Step 2: If the scanned character is an operand, append it with final
.Infix to Postfix string
,Step 3: Else
Step 3.1: If the precedence order of the scanned(incoming) operator is
greater than the precedence order of the operator in the stack (or the
.stack is empty or the stack contains a ‘(‘or ‘[‘ or ‘{‘), push it on stack
Step 3.2: Else, Pop all the operators from the stack which are greater
than or equal to in precedence than that of the scanned operator. After
doing that Push the scanned operator to the stack. (If you encounter
parenthesis while popping then stop there and push the scanned
operator in the stack.)

Step 4: If the scanned character is an ‘(‘or ‘[‘ or ‘{‘, push it to the


.stack

Step 5: If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and
output it until a ‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and
.discard both the parenthesis
.Step 6: Repeat steps 2-6 until infix expression is scanned

Step 7: Print the output

.Step 8: Pop and output from the stack until it is not empty

Problem 5- Infix to Postfix:


Page (103)

You might also like