0
0
DSA Typescriptprogramming~20 mins

BST Insert Operation in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after inserting nodes in BST
What is the printed state of the BST after inserting the values 10, 5, 15, 3, 7 in that order? The BST is printed using in-order traversal (left -> root -> right).
DSA Typescript
class Node {
  value: number;
  left: Node | null = null;
  right: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class BST {
  root: Node | null = null;

  insert(value: number) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (value < current.value) {
        if (!current.left) {
          current.left = newNode;
          return;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          return;
        }
        current = current.right;
      }
    }
  }

  inOrder(node: Node | null, result: number[] = []) {
    if (!node) return result;
    this.inOrder(node.left, result);
    result.push(node.value);
    this.inOrder(node.right, result);
    return result;
  }
}

const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(3);
bst.insert(7);
console.log(bst.inOrder(bst.root));
A[10, 5, 15, 3, 7]
B[3, 5, 7, 10, 15]
C[15, 10, 7, 5, 3]
D[3, 7, 5, 10, 15]
Attempts:
2 left
💡 Hint
Remember that in-order traversal of a BST prints values in sorted order.
Predict Output
intermediate
2:00remaining
BST structure after inserting duplicate value
What is the in-order traversal output after inserting values 20, 10, 30, 20 into an empty BST? Assume duplicates are inserted to the right subtree.
DSA Typescript
class Node {
  value: number;
  left: Node | null = null;
  right: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class BST {
  root: Node | null = null;

  insert(value: number) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (value < current.value) {
        if (!current.left) {
          current.left = newNode;
          return;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          return;
        }
        current = current.right;
      }
    }
  }

  inOrder(node: Node | null, result: number[] = []) {
    if (!node) return result;
    this.inOrder(node.left, result);
    result.push(node.value);
    this.inOrder(node.right, result);
    return result;
  }
}

const bst = new BST();
bst.insert(20);
bst.insert(10);
bst.insert(30);
bst.insert(20);
console.log(bst.inOrder(bst.root));
A[10, 20, 20, 30]
B[10, 20, 30]
C[20, 10, 20, 30]
D[10, 30, 20, 20]
Attempts:
2 left
💡 Hint
Duplicates go to the right subtree, so the second 20 is placed as right child of the first 20.
🔧 Debug
advanced
2:00remaining
Identify the bug in BST insert method
What error will occur when running this BST insert code if we try to insert 5 into the BST with root 10 and left child 5?
DSA Typescript
class Node {
  value: number;
  left: Node | null = null;
  right: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class BST {
  root: Node | null = null;

  insert(value: number) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (value < current.value) {
        if (!current.left) {
          current.left = newNode;
          return;
        }
        current = current.left;
      } else if (value > current.value) {
        if (!current.right) {
          current.right = newNode;
          return;
        }
        current = current.right;
      }
    }
  }
}

const bst = new BST();
bst.root = new Node(10);
bst.root.left = new Node(5);
bst.insert(5);
ANo error, insertion succeeds
BTypeError: Cannot read property 'left' of null
CSyntaxError: Unexpected token 'else if'
DInfinite loop causing program to hang
Attempts:
2 left
💡 Hint
Check what happens when value equals current.value in the loop.
Predict Output
advanced
2:00remaining
BST structure after multiple inserts and in-order traversal
After inserting the values 50, 30, 70, 20, 40, 60, 80 into an empty BST, what is the in-order traversal output?
DSA Typescript
class Node {
  value: number;
  left: Node | null = null;
  right: Node | null = null;
  constructor(value: number) {
    this.value = value;
  }
}

class BST {
  root: Node | null = null;

  insert(value: number) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (value < current.value) {
        if (!current.left) {
          current.left = newNode;
          return;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          return;
        }
        current = current.right;
      }
    }
  }

  inOrder(node: Node | null, result: number[] = []) {
    if (!node) return result;
    this.inOrder(node.left, result);
    result.push(node.value);
    this.inOrder(node.right, result);
    return result;
  }
}

const bst = new BST();
[50, 30, 70, 20, 40, 60, 80].forEach(v => bst.insert(v));
console.log(bst.inOrder(bst.root));
A[80, 70, 60, 50, 40, 30, 20]
B[50, 30, 20, 40, 70, 60, 80]
C[20, 30, 40, 50, 60, 70, 80]
D[20, 40, 30, 60, 80, 70, 50]
Attempts:
2 left
💡 Hint
In-order traversal prints BST values in ascending order.
🧠 Conceptual
expert
2:00remaining
Number of nodes in BST after insertions
If you insert the values [15, 10, 20, 10, 25, 15] into an empty BST where duplicates are inserted to the right subtree, how many nodes will the BST contain?
A6
B4
C3
D5
Attempts:
2 left
💡 Hint
Count each inserted value as a node, including duplicates inserted to the right.