0
0
DSA Javascriptprogramming~5 mins

BST Search Operation in DSA Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Binary Search Tree (BST)?
A Binary Search Tree is a tree data structure where each node has at most two children. The left child's value is less than the parent's value, and the right child's value is greater than the parent's value.
Click to reveal answer
beginner
How does the search operation work in a BST?
Start at the root. If the value equals the node's value, return it. If the value is less, search the left subtree. If greater, search the right subtree. Repeat until found or no nodes left.
Click to reveal answer
intermediate
What is the time complexity of searching in a balanced BST?
The time complexity is O(log n) because each step eliminates half of the remaining nodes to search.
Click to reveal answer
beginner
What happens if the value is not found in the BST during search?
The search reaches a null child (no node), meaning the value is not in the tree. The operation returns null or indicates not found.
Click to reveal answer
intermediate
Show the JavaScript code snippet for searching a value in a BST node.
function searchBST(node, val) {
  if (node === null) return null;
  if (node.val === val) return node;
  if (val < node.val) return searchBST(node.left, val);
  else return searchBST(node.right, val);
}
Click to reveal answer
In a BST, where do you search if the target value is less than the current node's value?
ALeft subtree
BRight subtree
CBoth subtrees
DRoot node only
What is the time complexity of searching in a balanced BST?
AO(n)
BO(1)
CO(n log n)
DO(log n)
What does the search function return if the value is not found in the BST?
AThe last visited node
BNull or equivalent
CThe root node
DAn error
Which property must a BST satisfy for the search operation to work correctly?
ALeft child values are less than parent, right child values are greater
BRight child values are less than parent
CAll nodes have two children
DLeft child values are greater than parent
What is the first step in the BST search operation?
ACheck the left child
BCheck the right child
CCompare the target value with the root node's value
DReturn null
Explain how the search operation works in a Binary Search Tree.
Think about how you decide which side of the tree to explore.
You got /5 concepts.
    Write a simple recursive function in JavaScript to search for a value in a BST.
    Use if-else conditions and recursion.
    You got /5 concepts.