Category: Data structures and algorithms
-
Recursive Inorder Traversal of a Binary Tree
1. Introduction Inorder traversal is a type of depth-first traversal used in binary trees. It follows the order: Why Use Inorder Traversal? 2. Example Inorder Traversal Output for This Tree 3. Pseudocode 4. Java Implementation 5. Time and Space Complexity Time Complexity: Space Complexity: 8. When to Use Recursive Inorder Traversal
-
Iterative Preorder traversal of a Binary Tree in Java
1. Introduction PreOrder traversal is a type of Depth-First Search (DFS) for binary trees. The visiting order is: This means for each node: 2. Why Use Iterative Instead of Recursive? Recursive traversal is elegant, but it: The iterative approach simulates recursion using an explicit stack and is preferred when: 3. PreOrder Traversal Pattern In PreOrder…
-
Recursive PreOrder Traversal of a Binary Tree in Java
1. What is PreOrder Traversal? PreOrder traversal is a type of Depth-First Traversal of a binary tree where you: This is also known as the Root-Left-Right traversal order. 2. PreOrder Traversal Pattern For each node: 3. Java Implementation 8. Time and Space Complexity Time Complexity: O(n) Space Complexity:
-
Traversal orders in a Binary Tree
1. What is Tree Traversal? Tree traversal means visiting every node in a tree exactly once in a specific order. Since a binary tree is non-linear and hierarchical, there are multiple ways to traverse it. Traversals help to: There are two primary traversal types: 2. Depth-First Traversals A. Inorder Traversal (Left → Root → Right)…
-
Binary Tree in Java
1. What is a Binary Tree? A Binary Tree is a hierarchical data structure in which each node has at most two children, referred to as the left child and right child. Each node in a binary tree contains: A Binary Tree follows a recursive structure—every child is itself a binary tree. 2. Types of…
-
Quick Sort in Java
1. Introduction Quick Sort is a widely-used, efficient, and comparison-based divide-and-conquer sorting algorithm. It was developed by Tony Hoare in 1959. Quick Sort works by selecting a pivot element from the array, partitioning the remaining elements into two sub-arrays: The same process is then recursively applied to the sub-arrays. 2. Step-by-Step Explanation of the Algorithm…
-
Sorting an array of 0s, 1s, and 2s – Dutch National Flag Algorithm
1. Problem Statement You are given an array consisting of only 0s, 1s, and 2s. Your task is to sort the array in-place so that all 0s come first, followed by all 1s, and then all 2s. Example: 2. Why Is It Called the “Dutch National Flag Problem”? The algorithm is called the Dutch National…
-
Binary Search in Java
1. Introduction Binary Search is an efficient algorithm to find the position of a target element in a sorted array by dividing the search interval in half repeatedly. Unlike Linear Search, which checks elements one by one, Binary Search uses the divide and conquer approach to eliminate half of the array at each step. 2.…
-
Linear Search In Java
1. Introduction Linear Search, also known as sequential search, is a simple search algorithm that checks every element in a list sequentially until the target value is found or the end of the list is reached. It is best used when: 2. Characteristics of Linear Search 3. Linear Search Algorithm Steps 4. Java Code Example…
-
Merge Sort in Java
1. Introduction Merge Sort is a well-known, efficient, and stable sorting algorithm based on the Divide and Conquer paradigm. Instead of sorting the array directly, it divides the array into smaller subarrays, sorts them individually, and then merges them into a fully sorted array. 2. Key Properties of Merge Sort Property Description Time Complexity O(n…
