Back to Dashboard
Binary Search Tree (BST)
A node-based binary tree data structure that allows for efficient search, insertion, and deletion.
Active
Target
Pivot
idle
Initializing...
Time Complexity
Best
Average
Worst
Space Complexity
StabilityFALSE
In-placeFALSE
Explanation
What is a Binary Search Tree (BST)?
A Binary Search Tree (BST) is a node-based binary tree data structure which has the following properties:
- The left child of a node contains only nodes with keys lesser than the node’s key.
- The right child of a node contains only nodes with keys greater than or equal to the node’s key.
These properties allow for efficient searching, insertion, and deletion of data in average time.
How it Works
1. Insertion
- Start from the root.
- If the value to be inserted is less than the current node, move to the left; otherwise, move to the right.
- Once an empty spot is found, insert the new node.
2. Binary Search
- Compare the target value with the current node.
- If they match, the search is successful.
- If the target is smaller, search the left subtree; otherwise, search the right subtree.
Complexity
- Time Complexity (Average):
- Time Complexity (Worst): (When the tree is unbalanced)
- Space Complexity:
Pseudo Code
1procedure insert(node, value)2 if node is null3 return new Node(value)4 if value < node.value5 node.left = insert(node.left, value)6 else7 node.right = insert(node.right, value)8 return node9end procedure
Sponsor Ad