binary trees

binary tree

graph where nodes are restricted to having at max 2 children (L and R).
a binary tree is complete when

types of binary trees:

Heap

complete binary tree where the children are smaller than the parent.
Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at the root of the tree.

used for heap sort.

useful for finding the min or max value in a collection.
Pasted image 20240803214834.png
getting min/max value is O(logn)

binary search tree

binary tree where the left children is <= than parent and right child is >=
used for searching algorithms, or rather checking whether a val exists in the structure.

time complexity is O(logn) for search, insertion, and deletion.

balanced binary tree

tree where the difference of height of the left subtree and the right subtree is no more than 1.

perfect binary tree

all interior nodes have two children and all leaves have the same level. ex: ancestry chart of a person

complete binary tree (or almost complete)

all interiors nodes have two children except maybe on the last level, where nodes get added from left to right.
sometimes referred as almost complete.

full binary tree

binary tree where each node has two children or no children at all