๐ณ What is a Binary Tree?
A Binary Tree is a hierarchical tree data structure where each node has at most two children, referred to as the left child and right child.
๐ Think of a binary tree like a family tree where each person (node) can have up to two children (left and right).
๐ Structure of a Node in Python
๐ง Terminology in Binary Tree
Term | Explanation |
---|---|
Root | The topmost node in the tree |
Leaf | A node with no children |
Parent | A node that has children |
Child | A node that has a parent |
Subtree | A tree formed from a node and its descendants |
Height | Length of the longest path from the node to a leaf |
Depth | Length of the path from the root to a given node |
Level | The number of edges from the root to the node |
Representation of Binary Tree
Each node in a Binary Tree has three parts:-
- Data
- Pointer to the left child
- Pointer to the right child
๐ท️ Types of Binary Trees
Type Description Full Binary Tree Every node has 0 or 2 children Perfect Binary Tree All internal nodes have 2 children & all leaves are at the same level Complete Binary Tree All levels are completely filled except possibly the last, which is filled from left to right Balanced Binary Tree For every node, the difference in height of left and right subtree is at most 1 Degenerate / Skewed Tree Each parent has only one child, either left or right (like a linked list)
Type | Description |
---|---|
Full Binary Tree | Every node has 0 or 2 children |
Perfect Binary Tree | All internal nodes have 2 children & all leaves are at the same level |
Complete Binary Tree | All levels are completely filled except possibly the last, which is filled from left to right |
Balanced Binary Tree | For every node, the difference in height of left and right subtree is at most 1 |
Degenerate / Skewed Tree | Each parent has only one child, either left or right (like a linked list) |
⚙️ Basic Operations on Binary Tree
๐ฑ Insertion
-
New nodes are added in level-order (left to right).
-
Use a queue to find the first empty spot.
New nodes are added in level-order (left to right).
Use a queue to find the first empty spot.
๐ Traversal
Visiting all nodes in a specific order:
-
Inorder: Left → Root → Right
-
Preorder: Root → Left → Right
-
Postorder: Left → Right → Root
-
Level Order: Level by Level (BFS using Queue)
Inorder: Left → Root → Right
Preorder: Root → Left → Right
Postorder: Left → Right → Root
Level Order: Level by Level (BFS using Queue)
๐ Searching
-
Binary Tree: Use DFS or BFS.
-
Binary Search Tree (BST): Use sorted rule — left < root < right.
Binary Tree: Use DFS or BFS.
Binary Search Tree (BST): Use sorted rule — left < root < right.
๐️ Deletion
-
Replace the target node with the deepest rightmost node, then delete the deepest one.
๐ง Summary Table
Operation Key Point Time Complexity Insertion Insert at first empty place (level-order) O(n) Traversal Visit all nodes (DFS or BFS) O(n) Searching Check each node (BT) or use BST property O(n) or O(log n) Deletion Replace with deepest node, delete it O(n)
Operation | Key Point | Time Complexity |
---|---|---|
Insertion | Insert at first empty place (level-order) | O(n) |
Traversal | Visit all nodes (DFS or BFS) | O(n) |
Searching | Check each node (BT) or use BST property | O(n) or O(log n) |
Deletion | Replace with deepest node, delete it | O(n) |
✅ Advantages of Binary Tree
-
Hierarchical Structure: Represents data with a natural hierarchy (like file systems).
-
Efficient Searching: Especially in Binary Search Tree (BST) – average time is O(log n).
-
Faster Insertion/Deletion: Compared to arrays or linked lists (especially in BSTs).
-
Memory Efficient: Doesn’t require contiguous memory like arrays.
-
Scalable: Easily expanded or reduced without affecting the structure.
❌ Disadvantages of Binary Tree
-
Unbalanced Trees Perform Poorly: If not balanced, worst-case time complexity becomes O(n).
-
Overhead of Pointers: Each node stores two additional pointers (left & right).
-
Difficult to Implement: More complex logic than linear data structures.
-
Complex Deletion: Deleting nodes while maintaining structure is tricky.
๐งพ Key Notes for Revision
-
Binary tree = At most 2 children per node.
-
Types: Full, Perfect, Complete, Balanced, Skewed.
-
Traversals: Inorder, Preorder, Postorder, Level Order.
-
Used in: Expression trees, Huffman coding, hierarchical data, file systems, BSTs, Heaps.
-
Balanced trees are optimal in performance.
-
Represented using linked structure, not arrays.
๐ Real-Life Analogy
Think of a company hierarchy:
-
CEO is the root
-
Managers are internal nodes
-
Interns or employees without subordinates are leaves
No comments:
Post a Comment