Algorithms and Data Structures/Data Structures 11

[Data Structures] Graph Search (DFS and BFS)

The two most common ways to search a graph are depth-first search and breadth-first search. In depth-first search (DFS), we start at the root (or another arbitrarily selected node) and explore each branch completely before moving on to the next branch. That is, we go deep first (hence the name depth-first search) before we go wide. In breadth-first search (BFS), we start at the root (or another ..

[Data Structures] Binary Heaps (Min-Heaps and Max-Heaps)

Min Heap: A complete binary tree (that is filled other than the rightmost elements on the last level) where each node is smaller than its children. The root, therefore, is the minimum element in the tree. We have two key operations on a min-heap: insert and extract_min Insert When we insert into a min-heap, we always start by inserting the element at the bottom. We insert it at the rightmost spo..

[Data Structures] Binary Tree Traversals with Implementations

In Order: visit the left branch, then the current node, and finally visit the right branch. When performed on a binary search tree, it visits the nodes in ascending order (hence the name "in-order") void inOrderTraversal(Node node) { if (node != null){ inOrderTraversal(node.left); visit(node); inOrderTraversal(node.right); } } Pre Order: visits the current node before its child nodes (hence the ..