0
0
DSA Typescriptprogramming~10 mins

BST Insert Operation in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new node with the given value.

DSA Typescript
class Node {
  value: number;
  left: Node | null = null;
  right: Node | null = null;

  constructor(value: number) {
    this.value = [1];
  }
}
Drag options to blanks, or click blank then click option'
AnewValue
Bnode
Cvalue
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the constructor.
Assigning 'val' or 'node' which are not parameters.
2fill in blank
medium

Complete the code to insert a new value into the BST recursively.

DSA Typescript
insert(node: Node | null, value: number): Node {
  if (node === null) {
    return new Node([1]);
  }
  if (value < node.value) {
    node.left = this.insert(node.left, value);
  } else {
    node.right = this.insert(node.right, value);
  }
  return node;
}
Drag options to blanks, or click blank then click option'
Athis
Bnode.value
Cnull
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node.value' which is undefined when node is null.
Returning null instead of a new node.
3fill in blank
hard

Fix the error in the comparison to decide where to insert the new value.

DSA Typescript
if (value [1] node.value) {
  node.left = this.insert(node.left, value);
} else {
  node.right = this.insert(node.right, value);
}
Drag options to blanks, or click blank then click option'
A===
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which reverses the BST property.
Using '===' or '!=' which are equality checks, not ordering.
4fill in blank
hard

Fill both blanks to complete the insert method that updates the tree correctly.

DSA Typescript
insert(node: Node | null, value: number): Node {
  if (node === null) {
    return new Node([1]);
  }
  if (value [2] node.value) {
    node.left = this.insert(node.left, value);
  } else {
    node.right = this.insert(node.right, value);
  }
  return node;
}
Drag options to blanks, or click blank then click option'
Avalue
B<
C>
Dnode.value
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node.value' in place of 'value' when creating new node.
Using '>' instead of '<' for left subtree insertion.
5fill in blank
hard

Fill all three blanks to complete the insert method with correct recursive calls and return.

DSA Typescript
insert(node: Node | null, value: number): Node {
  if (node === null) {
    return new Node([1]);
  }
  if (value [2] node.value) {
    node.left = this.insert(node.left, [3]);
  } else {
    node.right = this.insert(node.right, value);
  }
  return node;
}
Drag options to blanks, or click blank then click option'
Avalue
B<
Dnode.value
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'node.value' instead of 'value' in recursive call.
Using wrong comparison operator.
Not returning the updated node.