# Tag Archives: Stack

Algorithms involving stacks

## Next Greater Element in an array. [NGE]

Question: There is an array A[N] of N numbers. You have to compose an array Output[N] such that each element in Output[i] will tell the next greater element to the right in the original array. If there is no greater number to the right, then the output array should contain -1 in that position. Array 1: {4, 6,… Read More »

## PostOrder Traversal in a binary tree

We discussed about the basic tree traversals in this post – Binary Tree Traversals Lets take our own sample tree for understanding post-order traversal. In Post-Order traversal, the root is visited after both sub-trees are completed. Post-order traversal is defined as follows:- Traverse the left sub-tree in post-order. (Step 1) Traverse the right sub-tree in post-order. (Step 2)… Read More »

## InOrder Traversal in a binary tree

We discussed about the tree traversal techniques in this post- Binary Tree Traversals Please see pre-order traversal to understand the basics of visiting nodes. We have our same sample tree Now let us try to understand the In-order traversal. In in-order traversal, the root is traversed between the sub trees. In-order traversal is defined as follows. Traverse the… Read More »

## PreOrder Traversal in a binary tree

We learned about the different type of traversals in this post Binary Tree Traversals In pre-order traversal, each node is processed before (pre) either of its sub-trees. This is the simplest traversal to understand. However, even though each node is processed before the sub-trees, it still requires that some information must be maintained while moving down the tree.… Read More »

## How will you reverse a queue?

Question: Give an algorithm to reverse a queue. You can only use generic functions of the Queue ADT. Input: 4, 8, 15, 16, 23, 42 Output: 42,23,16,15,8,4 To solve this question will take the help of an auxiliary stack. The steps involved will be:- Create an auxiliary stack S. Until the queue Q is not empty, push the… Read More »

## Finding spans in an array.

Question: Find spans in an array. Given an array arr[], the SPAN s[i] of arr[i] is the maximum number of consecutive elements arr[j] immediately before arr[i] such that arr[j] <= arr[i]. Let us try to understand the question once again. This is a very common problem in stock markets to find the peaks. Spans have applications to financial… Read More »

## How will you implement 2 stacks using one array?

Question: How do we implement 2 stacks using only one array? Our stack routines should not indicate an exception unless every slot in the array is used? SOLUTION: Algorithm: Start with two indexes, one at the left end and other at the right end The left index simulates the first stack and the right index simulates the second… Read More »

## Given a stack, how will you reverse it using only stack operations PUSH and POP?

Question: You are given a stack and you have to reverse it, using only stack operations push and pop. The algorithm for the above problem can be given in this way:- First POP all elements of the stack till it becomes empty Now for each upward step in the recursion, push the elements at the bottom of the… Read More »

## How to design a stack such that we can get the minimum in O(1)?

Suppose we want to find the minimum element available in a stack at any point of time. The most trivial and common method would be to traverse the entire stack and keep a record of the least element encountered. At the end, we can return this element. This method no doubt is easy, but it consists of traversing… Read More »

## Discuss Postfix Evaluation using stacks

Post-fix expressions have a very special place in computer science. Most of the computations are performed using stacks. In-fact a very simple calculator works on the principle of post-fix evaluation to get the answer. A post-fix notation is also known as the reverse Polish notation. The steps involved in evaluating a post-fix expression can be defined as follows.… Read More »