Binary Tree
A Binary Tree is a hierarchical data structure where each node has at most two children, referred to as the left child and right child.
Key Properties
- The topmost node is called the root.
- Nodes with no children are called leaf nodes.
- The depth of a node is the number of edges from the root to that node.
- The height of a tree is the number of edges in the longest path from root to a leaf.
Structure of a Binary Tree
Types of Binary Trees
- Full Binary Tree – Every node has either 0 or 2 children.
- Complete Binary Tree – All levels are completely filled except possibly the last, which is filled from left to right.
- Perfect Binary Tree – All internal nodes have two children, and all leaves are at the same level.
- Degenerate (Skewed) Binary Tree – Each parent has only one child (like a linked list).
Catalan Number and Binary Trees
The Catalan number counts the number of possible distinct Binary Search Trees (BSTs) that can be formed with n
distinct keys.
Formula:
Or recursively:
where .
Example: For keys,
So, 5 distinct BSTs can be formed.
Number of Labelled Binary Trees
If we have n
labelled nodes (distinct labels), the number of labelled binary trees is:
Where:
- = Catalan number (structure count)
- = permutations of labels
Example: For ,
Total labelled binary trees:
Max and Min Number of Nodes Given Height
Let height be number of edges in longest path from root to leaf.
- Max Nodes in height (Perfect Binary Tree):
- Min Nodes in height (Skewed Tree):
Example: For , Max nodes:
Min nodes:
Max and Min Height Given Number of Nodes
Let = total nodes.
- Min Height (Perfect Binary Tree):
- Max Height (Skewed Tree):
Example: For , Min height:
Max height:
Internal and External Nodes
Definitions
- Internal Node: A node with at least one child (non-leaf).
- External Node: A node with no children (leaf).
Properties in a full binary tree:
where:
- = number of external nodes
- = number of internal nodes
Summary Table
Concept | Formula |
---|---|
Catalan Number | |
Number of labelled binary trees | |
Max nodes for height | |
Min nodes for height | |
Min height for nodes | |
Max height for nodes | |
Full Binary Tree relation |