Category: Queue

Level Order Traversal of Binary Tree

Given a binary tree, print its nodes level by level. i.e. all nodes present at level 1 should be printed first followed by nodes of level 2 and so on.. All nodes for any level should be printed from left to right.

Delete given Binary Tree | Iterative & Recursive

Given a binary tree, write an efficient algorithm to delete entire binary tree.   The program should de-allocate every single node present in the tree, not just change reference of the root node to null.     The idea is to traverse the tree in post-order fashion and delete left and right subtree of a …

Calculate height of binary tree | Iterative & Recursive

Write an efficient algorithm to compute the height of binary tree. The height or depth of a tree is number of edges or nodes on longest path from root node to leaf node.   The program should consider number of nodes in the longest path. For example, height of an empty tree is 0 and …

Count the number of islands

Given a binary matrix where 0 represents water and 1 represents land, count the number of islands in it. A island is formed by connected one’s.   For example, consider below image.    Above image highlights water in blue and land in grey in a 10 x 10 matrix. There are total five islands present in the above …

Flood Fill Algorithm

Flood fill (also known as seed fill) is an algorithm that determines the area connected to a given node in a multi-dimensional array.

Find shortest safe route in a field with sensors present

Given a rectangular field with few sensors present on it, cross it by taking the shortest safe route without activating the sensors.   The rectangular field is given in the form of M x N matrix and we need to find the shortest path from any cell in first column to any cell in the …

Queue Implementation in Java

A queue is a linear data structure which follows the FIFO (first-in first-out) principle. That means the object which is inserted first will be the first one out, followed by the object which was inserted next.

Queue Implementation using Templates in C++

In previous post, we have discussed C++ implementation of queue data structure using class and standard libraries. In this article, we will make the class code generic by using C++ templates to support all data-types.

Queue implementation in C++

A queue is a linear data structure that serves as a container of objects that are inserted & removed according to the FIFO (first-in first-out) principle.