0
0
DSA Typescriptprogramming~20 mins

Why BST Over Plain Binary Tree in DSA Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is a Binary Search Tree (BST) preferred over a plain Binary Tree for searching?

Consider you have two trees: a plain binary tree and a binary search tree (BST). Which reason best explains why BST is better for searching?

ABST keeps elements sorted, so searching can skip half the tree at each step.
BBST nodes have more children than a plain binary tree.
CBST allows storing duplicate values easily.
DBST uses less memory than a plain binary tree.
Attempts:
2 left
💡 Hint

Think about how the order of elements helps in finding a value quickly.

Predict Output
intermediate
2:00remaining
Output of Searching in BST vs Plain Binary Tree

Given the following TypeScript code snippets for searching a value in a plain binary tree and a BST, what is the output when searching for value 7?

DSA Typescript
class Node {
  constructor(public val: number, public left: Node | null = null, public right: Node | null = null) {}
}

// Plain binary tree search (no order)
function searchPlain(root: Node | null, target: number): boolean {
  if (!root) return false;
  if (root.val === target) return true;
  return searchPlain(root.left, target) || searchPlain(root.right, target);
}

// BST search (ordered)
function searchBST(root: Node | null, target: number): boolean {
  if (!root) return false;
  if (root.val === target) return true;
  if (target < root.val) return searchBST(root.left, target);
  else return searchBST(root.right, target);
}

// Tree setup
const rootPlain = new Node(5, new Node(3, new Node(7)), new Node(8));
const rootBST = new Node(5, new Node(3), new Node(8, new Node(7)));

console.log(searchPlain(rootPlain, 7));
console.log(searchBST(rootBST, 7));
Afalse false
Bfalse true
Ctrue false
Dtrue true
Attempts:
2 left
💡 Hint

Check if both search functions find the value 7 in their respective trees.

🔧 Debug
advanced
2:00remaining
Why does this plain binary tree search take longer than BST search?

Look at the two search functions below. Why does the plain binary tree search take longer time than the BST search?

DSA Typescript
function searchPlain(root: Node | null, target: number): boolean {
  if (!root) return false;
  if (root.val === target) return true;
  return searchPlain(root.left, target) || searchPlain(root.right, target);
}

function searchBST(root: Node | null, target: number): boolean {
  if (!root) return false;
  if (root.val === target) return true;
  if (target < root.val) return searchBST(root.left, target);
  else return searchBST(root.right, target);
}
APlain tree search uses more memory; BST search uses less memory.
BPlain tree search checks all nodes; BST search skips half nodes by ordering.
CPlain tree search uses loops; BST search uses recursion.
DPlain tree search only works on balanced trees; BST search works on all trees.
Attempts:
2 left
💡 Hint

Think about how many nodes each search visits in worst case.

📝 Syntax
advanced
2:00remaining
Identify the error in this BST insertion code

Which option correctly fixes the syntax error in this TypeScript BST insertion function?

DSA Typescript
function insertBST(root: Node | null, val: number): Node {
  if (!root) return new Node(val);
  if (val < root.val) {
    root.left = insertBST(root.left, val)
  } else {
    root.right = insertBST(root.right, val);
  }
  return root;
}
ARemove the return statement at the end
BChange 'if' to 'while' in the first condition
CAdd missing semicolon after root.left = insertBST(root.left, val)
DReplace 'else' with 'else if (val > root.val)'
Attempts:
2 left
💡 Hint

Look carefully at the line endings and TypeScript syntax rules.

🚀 Application
expert
2:00remaining
How many nodes are visited when searching for 15 in this BST?

Given the BST below, how many nodes are visited when searching for the value 15?

BST structure:

  • Root: 10
  • Left child of 10: 5
  • Right child of 10: 20
  • Left child of 20: 15
  • Right child of 20: 25
A3
B2
C4
D5
Attempts:
2 left
💡 Hint

Count nodes visited from root to the target following BST search rules.