⚔๐Ÿ›  Binary Tree ๐Ÿ› ⚔



๐ŸŒณ 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

class Node: def __init__(self, data): self.data = data self.left = None # Left child self.right = None # Right child



๐Ÿง  Terminology in Binary Tree

TermExplanation
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

TypeDescription
Full Binary TreeEvery node has 0 or 2 children
Perfect Binary TreeAll internal nodes have 2 children & all leaves are at the same level
Complete Binary TreeAll levels are completely filled except possibly the last, which is filled from left to right
Balanced Binary TreeFor every node, the difference in height of left and right subtree is at most 1
Degenerate / Skewed TreeEach parent has only one child, either left or right (like a linked list)


⚙️ Basic Operations on Binary Tree

OperationDescription
Insertion
    Add new nodes at the appropriate position  ๐Ÿ‘ˆ Click Here
Traversal
    Visit each node in a specific order ๐Ÿ‘ˆ Click Here
Search
    Find whether a node exists in the tree ๐Ÿ‘ˆ Click Here
Deletion
    Remove a node and rearrange the tree ๐Ÿ‘ˆ Click Here


๐ŸŒฑ Insertion

  • 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)

๐Ÿ” Searching

  • 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

OperationKey PointTime 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

  1. Hierarchical Structure: Represents data with a natural hierarchy (like file systems).

  2. Efficient Searching: Especially in Binary Search Tree (BST) – average time is O(log n).

  3. Faster Insertion/Deletion: Compared to arrays or linked lists (especially in BSTs).

  4. Memory Efficient: Doesn’t require contiguous memory like arrays.

  5. Scalable: Easily expanded or reduced without affecting the structure.


Disadvantages of Binary Tree

  1. Unbalanced Trees Perform Poorly: If not balanced, worst-case time complexity becomes O(n).

  2. Overhead of Pointers: Each node stores two additional pointers (left & right).

  3. Difficult to Implement: More complex logic than linear data structures.

  4. 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

DSA using Python: Assignment - 2: Classes and Objects

Follow Me “In this assignment, we will learn how to use Python classes and objects to build reusable and structured code. The tasks cover re...