Binary Tree

Definition

  • Is a Tree like data structure where every node has at most two children.
    • There is one left and right child node.

What you need to know

  • Designed to optimize searching and sorting.
  • A degenerate tree is an unbalanced tree, which if entirely one-sided, is essentially a linked list.
  • They are comparably simple to implement than other data structures.

How many nodes are there in a balanced binary tree?

This is important to reason out, as opposed to taking it for granted. Used for reasoning out Segment Tree.

0th level has one node, 1st-level has two nodes, 2nd level has 4 nodes, … i-th level has nodes.

In total, we have at most nodes (see Geometric Series formula), where is the height of the tree, .

  • The last layer has at most nodes, it is when it is completely filled as a row.
  • Also, I defined as the number of leaf nodes that you want to represent, because I was doing Segment Tree analysis

So we can actually solve this,

If it is a perfectly balanced tree, so , then you have exactly nodes.

So there are at most total nodes in a balanced binary tree!

How do you prove that there are at most nodes used for segment tree?

  • There are leaf nodes
  • A binary tree with leaf nodes has total nodes

Can you bound it by 2n?

Seems like i was able to. That’s also why Segment Tree has number of nodes implementation. There is a more careful analysis. Because the analysis actually assumes that all the layers are filled.

What is 2^{\lceil \log_2 n \rceil}?

Answer: . gives us the smallest number such that . We have two cases:

  1. is a power of two, so
  2. is not a power of two, so , where is the smallest power.

Ex: For , we would have . Then, which is the smallest power of 2 that is greater of equal to . For , that is .

We can see more generally, that always works.