Binary Search Tree (BST)
📘 Binary Search Tree (BST)
A Binary Search Tree (BST) is a binary tree with the following properties:
-
Each node contains a key (value).
-
The left subtree of a node contains only nodes with keys less than the node’s key.
-
The right subtree of a node contains only nodes with keys greater than the node’s key.
-
Both left and right subtrees must also be binary search trees.
-
No duplicate keys are allowed (in standard BSTs).
👉 Because of these properties, searching, insertion, and deletion can be done efficiently, usually in O(log n) time for balanced trees.
Algorithm: Insert a Node in BST
👉 At each step:
-
Compare the new value with the current node.
-
If smaller, go left.
-
If greater, go right.
-
If empty spot is found, insert the new node there.
Comments
Post a Comment