Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the constructor.
Assigning 'val' or 'node' which are not parameters.
✗ Incorrect
The constructor parameter is named 'value', so we assign it to this.value.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node.value' which is undefined when node is null.
Returning null instead of a new node.
✗ Incorrect
When node is null, we create a new node with the value to insert.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which reverses the BST property.
Using '===' or '!=' which are equality checks, not ordering.
✗ Incorrect
In BST, if value is less than node.value, insert to the left subtree.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node.value' in place of 'value' when creating new node.
Using '>' instead of '<' for left subtree insertion.
✗ Incorrect
Create a new node with 'value' when node is null, and use '<' to decide left subtree insertion.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'node.value' instead of 'value' in recursive call.
Using wrong comparison operator.
Not returning the updated node.
✗ Incorrect
Create new node with 'value', use '<' for comparison, and pass 'value' recursively for left insertion.