Binary Tree

  • Binary Tree → A hierarchical data structure in which each node has at most two children
  • Binary trees can be classified into complete and incomplete binary trees based on their structural properties
  • Complete Binary Trees
    • In a complete binary tree, all levels except possibly the last are completely filled, and all nodes are as far left as possible.
    • It is filled from top to bottom, left to right.
    • In a complete binary tree, if a node has a right child, it must also have a left child.
  • Incomplete Binary Tree
    • In an incomplete binary tree, not all levels are completely filled, and nodes may not be as far left as possible.
    • It does not follow a strict pattern of filling nodes.

Binary Heap Data Structure

  • A binary heap is a complete binary tree that satisfies the heap property (either min-heap or max-heap).
    • In a min-heap, each parent node has a value less than or equal to its children.
    • In a max-heap, each parent node has a value greater than or equal to its children.

Binary Heap Operations

Extra

In a binary heap, each parent has two children. The indexing formula (2i+1 for the left child and 2i+2 for the right child) comes from the way the binary heap is structured as a complete binary tree but represented in an array.

  1. Starting Index: Arrays are zero-indexed, so starting from an index i for a parent node,
  2. Left Child: The next layer of the tree (the children) starts at 2i + 1 because:
    • Doubling i (2i) shifts to the position at the start of the next level of a complete binary tree.
    • Adding 1 adjusts for the zero-based indexing, pointing to the first child (left).
  3. Right Child: Similarly, for the right child at 2i + 2, you add one more to reach the second child’s position.

This arrangement helps in maintaining a compact, easily navigable tree structure within a simple array, allowing quick calculations to move between parent and child nodes.