Binary Tree/Heap



A Binary Max Heap


Definition: A binary tree is a data structure in which each node (piece of data) has 0, 1, or 2 other nodes that it is pointing towards in a tree-like structure (see above). A binary heap is a specific type of binary tree in which certain elements are filtered towards the top of the structure. There are two main types of binary heaps:

  1. Binary Max Heap: A binary tree in which the greater elements are filtered towards the top. Every node's value is greater than the values of all the other individual nodes that stem from underneath it.

  2. Binary Min Heap: A binary tree in which the smaller elements are filtered towards the top. Every node's value is less than the values of all the other nodes that stem from underneath it.



Real Life Example: To envision a typical binary tree/heap (min heap for this example), think of a reverse family tree in terms of age. You are the youngest in the family and therefore are at the top of the tree. You were born from your two parents, who will be the two nodes that your node points towards. After this each of your parents will point towards their parents and so on. In summary, any given node is smaller than all nodes directly under it (you can't be older than your own parents or grandparents!) making this a min heap.


Basic Properties (See operations section below for applications):



Basic Max Heap Operations:

Note: Min heaps have the same exact procedures as below but checking for nodes being less than those below them rather than greater.

  1. Inserting a node. This is done by first entering the node directly to the right of the right-most node on the bottom level (if the bottom level is full, insert it as the left child node of the left-most node of the bottom level). The node is then shifted directly upwards as many times as needed so that max heap properties are satisfied, meaning that all nodes are greater than all those stemming from below them. This means that if big enough, a value could be swapped all the way up to become the root node.

  2. Removing a node. In a heap structure, this is typically done to the root node in order to remove the absolute extreme of the tree data set. The process involves swapping the node to be removed with the right-most node at the bottom level of the tree. After this swap, the node to be removed (now in a leaf node position) is removed. The node that was swapped is shifted directly downwards as many times as needed so that max heap properties are satisfied, meaning that all nodes are greater than all those stemming from below them.








Fun Fact: Repeated deletion of the root node in a heap is the basis a sorting algorithm called heap sort. The idea is that the most extreme element always rises to the top, therefore each time an element is removed from the heap it becomes sorted in comparison to the rest of the removed elements.



Commonly Used In:



Other Similar Data Structures: