In a Binary Tree, insertion is generally done level-wise (BFS) — meaning we insert a new node at the first available position from top to bottom and left to right.
✅ Steps for Insertion
-
If the tree is empty, the new node becomes the root.
-
Otherwise, do level-order traversal using a queue.
-
Insert the new node where a left or right child is missing.
🧠 Python Code for Insertion
⚡ Example Output:
🟢 Advantages:
-
Simple insertion using BFS.
-
Maintains balance (in terms of completeness) if inserting into a Complete Binary Tree.
🔴 Disadvantages:
-
Not suitable for ordered data (for that, use Binary Search Tree (BST) instead).
No comments:
Post a Comment