Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 80

Binary Search Tree (BST)

BST Model और Invariant

Binary search tree में हर node की left subtree की सभी keys छोटी और right subtree की सभी keys बड़ी होती हैं। Rule हर subtree पर recursively लागू होता है। इस program में duplicate key ignore होती है।

        50
      /    \
    30      70
   /  \    /  \
 20   40  60   80

Insertion order tree shape बदलता है; हर BST balanced नहीं होता।

Node Ownership

struct Node {
    int key;
    std::unique_ptr<Node> left, right;
    explicit Node(int value) : key(value) {}
};

Node अपने children को exclusively own करता है। Root destroy होने पर पूरा tree automatic release होता है; manual delete की जरूरत नहीं। Smart pointers पढ़ें।

Insert और Search

Key छोटी हो तो left, बड़ी हो तो right path लें। Null link पर new node बनता है। Search भी एक root-to-leaf path follow करता है और null मिलने पर false देता है। Owning pointer reference insert को empty child replace करने देता है।

Tree Traversals

TraversalOrderUse
InorderLeft, node, rightBST की ascending keys
PreorderNode, left, rightRoot first
PostorderLeft, right, nodeChildren first
Level orderDepth-wiseTree shape

Full traversal Θ(n) time और recursive O(h) stack लेता है।

Deletion के तीन Cases

  1. Leaf: owning link null करें।
  2. One child: child ownership target link में move करें।
  3. Two children: right subtree की minimum key (successor) copy कर उसके original node को delete करें।

unique_ptr move ownership सुरक्षित transfer करता है।

Height और Complexity

OperationCost
Search/Insert/DeleteO(h)
Balanced heightΘ(log n)
Skewed heightΘ(n)
TraversalΘ(n)
StorageΘ(n)

Sorted insertion chain बना सकता है। Height guarantee के लिए AVL/red-black concept और practical ordered data के लिए std::set/std::map देखें।

Complete Modern C++ Lab

std::unique_ptr<Node> root;
for (int key : {50,30,70,20,40,60,80}) insert(root,key);
std::cout << "Inorder:"; inorder(root);
std::cout << std::boolalpha << contains(root,60);
erase(root,30);
Inorder: 20 30 40 50 60 70 80 Contains 60: true After deleting 30: 20 40 50 60 70 80

English section का full compile-ready program insert, search, inorder और three-case deletion implement करता है। 30 के successor 40 से replacement होता है।

Mistakes, Tests और Practice

  • Ordering केवल direct child पर नहीं, whole subtree पर लागू है।
  • Duplicate policy document करें।
  • Delete में child ownership न खोएं।
  • Actual height analyze करें; BST को automatic balanced न कहें।
  • Null base case पहले रखें।

Empty, leaf, one-child, two-child, root, absent और skewed cases test करें। Height और BST validator practice करें।

Authoritative संदर्भ

Definitions और ordering invariant authoritative sources से verify हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

BST और binary tree में क्या अंतर है?
Binary tree केवल हर node को maximum two children तक सीमित करता है। BST इसके साथ left/right subtrees पर ordering rule भी लगाता है।
Duplicate keys का क्या करें?
Policy साफ रखें: reject करें, count रखें, या consistently एक side रखें। इस tutorial का program duplicates ignore करता है।
Inorder traversal sorted keys क्यों देता है?
यह left subtree, node, फिर right subtree visit करता है; BST invariant के कारण यही ascending order बनता है।
BST operations की complexity क्या है?
Search, insert और delete O(h) हैं। Balanced tree में O(log n), skewed tree में O(n) हो सकते हैं।
BST deletion के तीन cases कौन से हैं?
Leaf, one child और two children। Two-child case में inorder successor/predecessor key लेकर उस replacement node को हटाते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicPascals Triangle आपकी प्रगति: 80 / 81 (99%) · Lesson 81 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें