📖 Scenario: You are building a simple binary tree to store numbers. Each spot in the tree can hold one number and can have up to two smaller spots connected to it: one on the left and one on the right.
🎯 Goal: Create a basic binary tree node structure in TypeScript. You will first define the node with its value and connections, then create a simple tree with three nodes.
📋 What You'll Learn
Define a class called TreeNode with three properties: value, left, and right.
The value should be a number.
The left and right should be either another TreeNode or null.
Create three nodes with values 10, 5, and 15.
Connect the nodes so that 10 is the root, 5 is the left child, and 15 is the right child.
💡 Why This Matters
🌍 Real World
Binary trees are used in many applications like searching, sorting, and organizing data efficiently.
💼 Career
Understanding binary tree structures is fundamental for software developers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Define the TreeNode class
Create a class called TreeNode with a constructor that takes a value of type number. Inside the constructor, set this.value to the given value. Also, add two properties left and right initialized to null.
DSA Typescript
Hint
Remember to set this.value to the input value. Initialize left and right to null.
2
Create three TreeNode instances
Create three variables called root, leftChild, and rightChild. Assign them new TreeNode objects with values 10, 5, and 15 respectively.
DSA Typescript
Hint
Use new TreeNode(value) to create each node and assign to the correct variable.
3
Connect the nodes to form the tree
Set the left property of root to leftChild and the right property of root to rightChild.
DSA Typescript
Hint
Use dot notation to set root.left and root.right.
4
Print the tree node values
Print the values of root, root.left, and root.right using console.log. Print them in this order, separated by spaces.
DSA Typescript
Hint
Use console.log(root.value, root.left?.value, root.right?.value) to print the values separated by spaces.
Practice
(1/5)
1. What does a typical binary tree node contain in its structure?
easy
A. Only a value and a pointer to the next node
B. A value, a left child node, and a right child node
C. A value and multiple child nodes in a list
D. Only a value without any child nodes
Solution
Step 1: Understand binary tree node components
A binary tree node typically stores a value and references to two children: left and right.
Step 2: Compare options with definition
A value, a left child node, and a right child node correctly lists value, left child, and right child. Others describe linked list or general tree nodes.
Final Answer:
A value, a left child node, and a right child node -> Option B
Quick Check:
Binary tree node = value + left + right [OK]
Hint: Binary tree nodes always have two child pointers [OK]
Common Mistakes:
Confusing binary tree node with linked list node
Thinking nodes have multiple children like general trees
Ignoring child pointers in node structure
2. Which of the following is the correct TypeScript syntax to define a binary tree node class?
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } correctly declares left and right as Node or null and initializes them to null in constructor.
Step 2: Verify constructor parameters and assignments
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } constructor takes value and sets left and right to null, matching typical binary tree node setup.
Final Answer:
Correct TypeScript class with nullable children and proper constructor -> Option C
Quick Check:
Nullable children + constructor with value = class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } [OK]
Hint: Child nodes must allow null to represent empty branches [OK]
Common Mistakes:
Not initializing left and right to null
Using undefined instead of null for children
Missing constructor parameter for value
3. Given the following TypeScript code, what will be the output of console.log(root.left?.value)?