Middle To Up-Down Order traversal of a Binary Tree
Last Updated :
12 Jul, 2025
Given a binary tree, the task is to traverse this binary tree from the middle to the up-down order.
In Middle to up-down order traversal, the following steps are performed:
- First, print the middle level of the tree.
- Then, print the elements at one level above the middle level of the tree.
- Then, print the elements at one level below the middle level of the tree.
- Then, print the elements at two levels above the middle level of the tree.
- Then, print the elements at two levels below the middle level of the tree and so on.
Note: In this problem, the middle level is considered at ((H / 2) + 1)th level where H is the height of the ree and the level of the root is considered as 1.
Examples:
Input:
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Output:
14, 15,
12, 13,
21, 22, 23, 24,
10,
Explanation:
There are 4 levels in the tree.
Therefore, Middle level = ((4 / 2) + 1) = 3
Now, the tree is traversed in the following way:
Middle level: 14, 15
One level above the middle level: 12, 13
One level below the middle level: 21, 22, 23, 24
Two levels above the middle level: 10
Input:
5
/ \
2 13
/ \ \
4 25 6
/ / \
11 3 21
\
9
Output:
4, 25, 6
2, 13
11, 3, 21
5
9
Explanation:
There are 5 levels in the tree.
Therefore, Middle level = ((5 / 2) + 1) = 3.
Now, the tree is traversed in the following way:
Middle level: 4, 25, 6
One level above the middle level: 2, 13
One level below the middle level: 11, 3, 21
Two levels above the middle level: 5
Two levels below the middle level: 9
Approach: The idea is to store the tree in a matrix. In order to do that,

- Then, simply iterate over the matrix and print the values from the matrix as per the given condition.
Below is the implementation of the above approach:
C++
// C++ program to traverse the tree
// from the middle to up and down
#include <bits/stdc++.h>
using namespace std;
// A Tree node
struct Node {
int key;
struct Node *left, *right;
};
// Utility function to create
// a new node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
// Function to calculate the
// height of the tree
int findHeight(struct Node* node)
{
// Base condition
if (node == NULL)
return 0;
int leftHeight = findHeight(node->left);
int rightHeight = findHeight(node->right);
// Return the maximum of the height
// of the left and right subtree
return 1 + (leftHeight > rightHeight
? leftHeight
: rightHeight);
}
// Function to find the width of the tree
void findWidth(struct Node* node, int& maxValue,
int& minValue, int hd)
{
// Base cases
if (node == NULL)
return;
if (hd > maxValue) {
maxValue = hd;
}
if (hd < minValue) {
minValue = hd;
}
// Recursively call the function twice to find
// the maximum width
findWidth(node->left, maxValue, minValue, hd - 1);
findWidth(node->right, maxValue, minValue, hd + 1);
}
// Function to traverse the tree and
// store level order traversal in a matrix
void BFS(int** mtrx, struct Node* node)
{
// Create queue for storing
// the addresses of nodes
queue<struct Node*> qu;
qu.push(node);
int i = -1, j = -1;
struct Node* poped_node = NULL;
// Iterating over the queue to perform
// breadth-first search traversal
while (!qu.empty()) {
i++;
int qsize = qu.size();
while (qsize--) {
j++;
poped_node = qu.front();
// Store the data of the node into
// the matrix
mtrx[i][j] = poped_node->key;
qu.pop();
// Performing BFS for the remaining
// nodes in the queue
if (poped_node->left != NULL) {
qu.push(poped_node->left);
}
if (poped_node->right != NULL) {
qu.push(poped_node->right);
}
}
j = -1;
}
}
// Function for Middle to Up Down
// Traversal of Binary Tree
void traverse_matrix(int** mtrx, int height,
int width)
{
// Variables to handle rows and columns
// of the matrix
int up = (height / 2);
int down = up + 1;
bool flag = true;
int k = 0;
// Print the middle row
for (int j = 0; j < width; j++) {
if (mtrx[up][j] != INT_MAX) {
cout << mtrx[up][j] << ", ";
}
}
cout << endl;
up--;
// Loop to print the remaining rows
for (int i = 0; i < (height - 1); i++) {
// Condition to manage up and
// down indices in the matrix
if (flag) {
k = up;
up--;
flag = !flag;
}
else {
k = down;
down++;
flag = !flag;
}
// Loop to print the value
// of matrix cells in particular row
for (int j = 0; j < width; j++) {
if (mtrx[k][j] != INT_MAX) {
cout << mtrx[k][j] << ", ";
}
}
cout << endl;
}
}
// A utility function for middle to
// up down traversal
void printPattern(struct Node* node)
{
// max, min has been taken for
// calculating the width of tree
int max_value = INT_MIN;
int min_value = INT_MAX;
int hd = 0;
// Calculate the width of a tree
findWidth(node, max_value, min_value, hd);
int width = max_value + abs(min_value);
// Calculate the height of the tree
int height = findHeight(node);
// Double pointer to create 2D array
int** mtrx = new int*[height];
// Initialize the width for
// each row of the matrix
for (int i = 0; i < height; i++) {
mtrx[i] = new int[width];
}
// Initialize complete matrix with
// MAX INTEGER(purpose garbage)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
mtrx[i][j] = INT_MAX;
}
}
// Store the BFS traversal of the tree
// into the 2-D matrix
BFS(mtrx, node);
// Print the circular clockwise spiral
// traversal of the tree
traverse_matrix(mtrx, height, width);
// release extra memory
// allocated for matrix
free(mtrx);
}
// Driver Code
int main()
{
/*
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
*/
// Creating the above tree
Node* root = newNode(10);
root->left = newNode(12);
root->right = newNode(13);
root->right->left = newNode(14);
root->right->right = newNode(15);
root->right->left->left = newNode(21);
root->right->left->right = newNode(22);
root->right->right->left = newNode(23);
root->right->right->right = newNode(24);
printPattern(root);
return 0;
}
Java
// Java program to traverse the tree
// from the middle to up and down
import java.util.*;
class GFG{
// A Tree node
static class Node {
int key;
Node left, right;
};
static int maxValue, minValue;
// Utility function to create
// a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Function to calculate the
// height of the tree
static int findHeight(Node node)
{
// Base condition
if (node == null)
return 0;
int leftHeight = findHeight(node.left);
int rightHeight = findHeight(node.right);
// Return the maximum of the height
// of the left and right subtree
return 1 + (leftHeight > rightHeight
? leftHeight
: rightHeight);
}
// Function to find the width of the tree
static void findWidth(Node node, int hd)
{
// Base cases
if (node == null)
return;
if (hd > maxValue) {
maxValue = hd;
}
if (hd < minValue) {
minValue = hd;
}
// Recursively call the function twice to find
// the maximum width
findWidth(node.left, hd - 1);
findWidth(node.right, hd + 1);
}
// Function to traverse the tree and
// store level order traversal in a matrix
static void BFS(int [][]mtrx, Node node)
{
// Create queue for storing
// the addresses of nodes
Queue<Node> qu = new LinkedList<Node>();
qu.add(node);
int i = -1, j = -1;
Node poped_node = null;
// Iterating over the queue to perform
// breadth-first search traversal
while (!qu.isEmpty()) {
i++;
int qsize = qu.size();
while (qsize-- > 0) {
j++;
poped_node = qu.peek();
// Store the data of the node into
// the matrix
mtrx[i][j] = poped_node.key;
qu.remove();
// Performing BFS for the remaining
// nodes in the queue
if (poped_node.left != null) {
qu.add(poped_node.left);
}
if (poped_node.right != null) {
qu.add(poped_node.right);
}
}
j = -1;
}
}
// Function for Middle to Up Down
// Traversal of Binary Tree
static void traverse_matrix(int [][]mtrx, int height,
int width)
{
// Variables to handle rows and columns
// of the matrix
int up = (height / 2);
int down = up + 1;
boolean flag = true;
int k = 0;
// Print the middle row
for (int j = 0; j < width; j++) {
if (mtrx[up][j] != Integer.MAX_VALUE) {
System.out.print(mtrx[up][j]+ ", ");
}
}
System.out.println();
up--;
// Loop to print the remaining rows
for (int i = 0; i < (height - 1); i++) {
// Condition to manage up and
// down indices in the matrix
if (flag) {
k = up;
up--;
flag = !flag;
}
else {
k = down;
down++;
flag = !flag;
}
// Loop to print the value
// of matrix cells in particular row
for (int j = 0; j < width; j++) {
if (mtrx[k][j] != Integer.MAX_VALUE) {
System.out.print(mtrx[k][j]+ ", ");
}
}
System.out.println();
}
}
// A utility function for middle to
// up down traversal
static void printPattern(Node node)
{
// max, min has been taken for
// calculating the width of tree
maxValue = Integer.MIN_VALUE;
minValue = Integer.MAX_VALUE;
int hd = 0;
// Calculate the width of a tree
findWidth(node, hd);
int width = maxValue + Math.abs(minValue);
// Calculate the height of the tree
int height = findHeight(node);
// Double pointer to create 2D array
int [][]mtrx = new int[width][height];
// Initialize complete matrix with
// MAX INTEGER(purpose garbage)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
mtrx[i][j] = Integer.MAX_VALUE;
}
}
// Store the BFS traversal of the tree
// into the 2-D matrix
BFS(mtrx, node);
// Print the circular clockwise spiral
// traversal of the tree
traverse_matrix(mtrx, height, width);
// release extra memory
// allocated for matrix
mtrx =null;
}
// Driver Code
public static void main(String[] args)
{
/*
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
*/
// Creating the above tree
Node root = newNode(10);
root.left = newNode(12);
root.right = newNode(13);
root.right.left = newNode(14);
root.right.right = newNode(15);
root.right.left.left = newNode(21);
root.right.left.right = newNode(22);
root.right.right.left = newNode(23);
root.right.right.right = newNode(24);
printPattern(root);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the
# above approach
from collections import deque
# A Tree node
class Node:
def __init__(self, x):
self.key = x
self.left = None
self.right = None
minValue, maxValue = 0, 0
# Function to calculate the
# height of the tree
def findHeight(node):
# Base condition
if (node == None):
return 0
leftHeight = findHeight(node.left)
rightHeight = findHeight(node.right)
# Return the maximum of the height
# of the left and right subtree
if leftHeight > rightHeight:
return leftHeight + 1
return rightHeight + 1
# Function to find the width
# of the tree
def findWidth(node, hd):
global minValue, maxValue
# Base cases
if (node == None):
return
if (hd > maxValue):
maxValue = hd
if (hd < minValue):
minValue = hd
# Recursively call the
# function twice to find
# the maximum width
findWidth(node.left,
hd - 1)
findWidth(node.right,
hd + 1)
# Function to traverse the
# tree and store level order
# traversal in a matrix
def BFS(mtrx, node):
# Create queue for storing
# the addresses of nodes
qu = deque()
qu.append(node)
i = -1
j = -1
poped_node = None
# Iterating over the queue
# to perform breadth-first
# search traversal
while (len(qu) > 0):
i += 1
qsize = len(qu)
while (qsize):
j += 1
poped_node = qu.popleft()
# Store the data of the
# node into the matrix
mtrx[i][j] = poped_node.key
#qu.pop()
# Performing BFS for
# the remaining nodes
# in the queue
if (poped_node.left != None):
qu.append(poped_node.left)
if (poped_node.right != None):
qu.append(poped_node.right)
qsize -= 1
j = -1
#Function for Middle to Up Down
#Traversal of Binary Tree
def traverse_matrix(mtrx,
height,width):
# Variables to handle rows
# and columns of the matrix
up = (height // 2)
down = up + 1
flag = True
k = 0
# Print middle row
for j in range(width):
if (mtrx[up][j] != 10 ** 9):
print(mtrx[up][j], end = ", ")
print()
up -= 1
# Loop to print the remaining rows
for i in range(height - 1):
# Condition to manage up and
# down indices in the matrix
if (flag):
k = up
up -= 1
flag = not flag
else:
k = down
down += 1
flag = not flag
# Loop to print the value
# of matrix cells in
# particular row
for j in range(width):
if (mtrx[k][j] != 10 ** 9):
print(mtrx[k][j], end = ", ")
print()
# A utility function for middle to
# up down traversal
def printPattern(node):
global maxValue, minValue
# max, min has been taken for
# calculating the width of tree
maxValue = -10 ** 9
minValue = 10 ** 9
hd = 0
# Calculate the width
# of a tree
findWidth(node, hd)
width = maxValue + abs(minValue)
# Calculate the height of the tree
height = findHeight(node)
# print(height,width)
#Double pointer to create 2D array
mtrx = [[10 ** 9 for i in range(width + 1)]
for i in range(height + 1)]
# Store the BFS traversal of the tree
# into the 2-D matrix
BFS(mtrx, node)
# Print circular clockwise spiral
# traversal of the tree
traverse_matrix(mtrx, height, width)
# Driver Code
if __name__ == '__main__':
# /*
# 10
# / \
# 12 13
# / \
# 14 15
# / \ / \
# 21 22 23 24
# */
# Creating the above tree
root = Node(10)
root.left = Node(12)
root.right = Node(13)
root.right.left = Node(14)
root.right.right = Node(15)
root.right.left.left = Node(21)
root.right.left.right = Node(22)
root.right.right.left = Node(23)
root.right.right.right = Node(24)
printPattern(root)
# This code is contributed by Mohit Kumar 29
C#
// C# program to traverse the tree
// from the middle to up and down
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// A Tree node
class Node
{
public int key;
public Node left, right;
};
static int maxValue, minValue;
// Utility function to create
// a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Function to calculate the
// height of the tree
static int findHeight(Node node)
{
// Base condition
if (node == null)
return 0;
int leftHeight = findHeight(node.left);
int rightHeight = findHeight(node.right);
// Return the maximum of the height
// of the left and right subtree
return 1 + (leftHeight > rightHeight
? leftHeight
: rightHeight);
}
// Function to find the width of the tree
static void findWidth(Node node, int hd)
{
// Base cases
if (node == null)
return;
if (hd > maxValue) {
maxValue = hd;
}
if (hd < minValue) {
minValue = hd;
}
// Recursively call the function twice to find
// the maximum width
findWidth(node.left, hd - 1);
findWidth(node.right, hd + 1);
}
// Function to traverse the tree and
// store level order traversal in a matrix
static void BFS(int [,]mtrx, Node node)
{
// Create queue for storing
// the addresses of nodes
Queue qu = new Queue();
qu.Enqueue(node);
int i = -1, j = -1;
Node poped_node = null;
// Iterating over the queue to perform
// breadth-first search traversal
while (qu.Count != 0)
{
i++;
int qsize = qu.Count;
while (qsize-- > 0) {
j++;
poped_node = (Node)qu.Peek();
// Store the data of the node into
// the matrix
mtrx[i,j] = poped_node.key;
qu.Dequeue();
// Performing BFS for the remaining
// nodes in the queue
if (poped_node.left != null) {
qu.Enqueue(poped_node.left);
}
if (poped_node.right != null) {
qu.Enqueue(poped_node.right);
}
}
j = -1;
}
}
// Function for Middle to Up Down
// Traversal of Binary Tree
static void traverse_matrix(int [,]mtrx, int height,
int width)
{
// Variables to handle rows and columns
// of the matrix
int up = (height / 2);
int down = up + 1;
bool flag = true;
int k = 0;
// Print the middle row
for (int j = 0; j < width; j++) {
if (mtrx[up, j] != 100000000) {
Console.Write(mtrx[up,j]+ ", ");
}
}
Console.WriteLine();
up--;
// Loop to print the remaining rows
for (int i = 0; i < (height - 1); i++)
{
// Condition to manage up and
// down indices in the matrix
if (flag) {
k = up;
up--;
flag = !flag;
}
else {
k = down;
down++;
flag = !flag;
}
// Loop to print the value
// of matrix cells in particular row
for (int j = 0; j < width; j++)
{
if (mtrx[k, j] != 100000000) {
Console.Write(mtrx[k,j]+ ", ");
}
}
Console.WriteLine();
}
}
// A utility function for middle to
// up down traversal
static void printPattern(Node node)
{
// max, min has been taken for
// calculating the width of tree
maxValue = -100000000;
minValue = 100000000;
int hd = 0;
// Calculate the width of a tree
findWidth(node, hd);
int width = maxValue + Math.Abs(minValue);
// Calculate the height of the tree
int height = findHeight(node);
// Double pointer to create 2D array
int [,]mtrx = new int[width,height];
// Initialize complete matrix with
// MAX INTEGER(purpose garbage)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
mtrx[i, j] = 100000000;
}
}
// Store the BFS traversal of the tree
// into the 2-D matrix
BFS(mtrx, node);
// Print the circular clockwise spiral
// traversal of the tree
traverse_matrix(mtrx, height, width);
// release extra memory
// allocated for matrix
mtrx = null;
}
// Driver Code
public static void Main(string[] args)
{
/*
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
*/
// Creating the above tree
Node root = newNode(10);
root.left = newNode(12);
root.right = newNode(13);
root.right.left = newNode(14);
root.right.right = newNode(15);
root.right.left.left = newNode(21);
root.right.left.right = newNode(22);
root.right.right.left = newNode(23);
root.right.right.right = newNode(24);
printPattern(root);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// Javascript program to traverse the tree
// from the middle to up and down
// A Tree node
class Node
{
// Utility function to create
// a new node
constructor(key)
{
this.key = key;
this.left = this.right = null;
}
}
// Function to calculate the
// height of the tree
function findHeight(node)
{
// Base condition
if (node == null)
return 0;
let leftHeight = findHeight(node.left);
let rightHeight = findHeight(node.right);
// Return the maximum of the height
// of the left and right subtree
return 1 + (leftHeight > rightHeight ?
leftHeight : rightHeight);
}
// Function to find the width of the tree
function findWidth(node,hd)
{
// Base cases
if (node == null)
return;
if (hd > maxValue)
{
maxValue = hd;
}
if (hd < minValue)
{
minValue = hd;
}
// Recursively call the function twice
// to find the maximum width
findWidth(node.left, hd - 1);
findWidth(node.right, hd + 1);
}
// Function to traverse the tree and
// store level order traversal in a matrix
function BFS(mtrx, node)
{
// Create queue for storing
// the addresses of nodes
let qu = [];
qu.push(node);
let i = -1, j = -1;
let poped_node = null;
// Iterating over the queue to perform
// breadth-first search traversal
while (qu.length != 0)
{
i++;
let qsize = qu.length;
while (qsize-- > 0)
{
j++;
poped_node = qu[0];
// Store the data of the node into
// the matrix
mtrx[i][j] = poped_node.key;
qu.shift();
// Performing BFS for the remaining
// nodes in the queue
if (poped_node.left != null)
{
qu.push(poped_node.left);
}
if (poped_node.right != null)
{
qu.push(poped_node.right);
}
}
j = -1;
}
}
// Function for Middle to Up Down
// Traversal of Binary Tree
function traverse_matrix(mtrx, height, width)
{
// Variables to handle rows and columns
// of the matrix
let up = Math.floor(height / 2);
let down = up + 1;
let flag = true;
let k = 0;
// Print the middle row
for(let j = 0; j < width; j++)
{
if (mtrx[up][j] != Number.MAX_VALUE)
{
document.write(mtrx[up][j] + ", ");
}
}
document.write("<br>");
up--;
// Loop to print the remaining rows
for(let i = 0; i < (height - 1); i++)
{
// Condition to manage up and
// down indices in the matrix
if (flag)
{
k = up;
up--;
flag = !flag;
}
else
{
k = down;
down++;
flag = !flag;
}
// Loop to print the value
// of matrix cells in particular row
for(let j = 0; j < width; j++)
{
if (mtrx[k][j] != Number.MAX_VALUE)
{
document.write(mtrx[k][j] + ", ");
}
}
document.write("<br>");
}
}
// A utility function for middle to
// up down traversal
function printPattern(node)
{
// max, min has been taken for
// calculating the width of tree
maxValue = Number.MIN_VALUE;
minValue = Number.MAX_VALUE;
let hd = 0;
// Calculate the width of a tree
findWidth(node, hd);
let width = maxValue + Math.abs(minValue);
// Calculate the height of the tree
let height = findHeight(node);
// Double pointer to create 2D array
let mtrx = new Array(width);
for(let i = 0; i < width; i++)
{
mtrx[i] = new Array(height);
}
// Initialize complete matrix with
// MAX INTEGER(purpose garbage)
for(let i = 0; i < height; i++)
{
for(let j = 0; j < width; j++)
{
mtrx[i][j] = Number.MAX_VALUE;
}
}
// Store the BFS traversal of the tree
// into the 2-D matrix
BFS(mtrx, node);
// Print the circular clockwise spiral
// traversal of the tree
traverse_matrix(mtrx, height, width);
// release extra memory
// allocated for matrix
mtrx = null;
}
// Driver Code
/*
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
*/
// Creating the above tree
let root = new Node(10);
root.left = new Node(12);
root.right = new Node(13);
root.right.left = new Node(14);
root.right.right = new Node(15);
root.right.left.left = new Node(21);
root.right.left.right = new Node(22);
root.right.right.left = new Node(23);
root.right.right.right = new Node(24);
printPattern(root);
// This code is contributed by unknown2108
</script>
Output: 14, 15,
12, 13,
21, 22, 23, 24,
10,
The time complexity of the findHeight function is O(V), as it visits each node in the tree exactly once and performs a constant amount of work for each node. The time complexity of the findWidth function is also O(V), as it similarly visits each node in the tree exactly once
The Auxiliary Space of this implementation is O(V), where V is the number of nodes in the tree. This is because the BFS function uses a queue to store the addresses of the nodes during the traversal, and the size of the queue is at most equal to the number of nodes in the tree.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem