📖 Scenario: Imagine you are organizing a collection of books by their unique ID numbers. You want to store them so that you can quickly find any book by its ID. A Binary Search Tree (BST) helps you do this by keeping smaller IDs on the left and larger IDs on the right.
🎯 Goal: You will build a simple Binary Search Tree (BST) with three nodes and then print the tree structure to see how the BST property keeps the data organized.
📋 What You'll Learn
Create a BST node class with value, left, and right properties
Create three nodes with values 10, 5, and 15
Link the nodes to form a BST where 5 is left child of 10 and 15 is right child of 10
Print the tree nodes in order: root, left child, right child
💡 Why This Matters
🌍 Real World
BSTs are used in databases and file systems to quickly find, add, or remove data by keeping it organized.
💼 Career
Understanding BSTs helps in software engineering roles that involve data storage, search optimization, and algorithm design.
Progress0 / 4 steps
1
Create BST Node Class
Create a class called BSTNode with a constructor that takes a value of type number and initializes left and right properties to null.
DSA Typescript
Hint
Think of each node as a box holding a number and two empty spots for smaller and larger numbers.
2
Create Three BST Nodes
Create three variables called root, leftChild, and rightChild and assign them new BSTNode instances with values 10, 5, and 15 respectively.
DSA Typescript
Hint
Use the new keyword to create nodes with the given values.
3
Link Nodes to Form BST
Link the nodes by setting root.left to leftChild and root.right to rightChild to form a BST where smaller values go left and larger values go right.
DSA Typescript
Hint
Remember, smaller values go to the left child, larger values to the right child.
4
Print BST Node Values
Print the values of root, root.left, and root.right in order using three separate console.log statements.
DSA Typescript
Hint
Use console.log to print each node's value. Use optional chaining ?. for safety.
Practice
(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. Left child nodes have smaller values, right child nodes have larger values
B. All nodes have exactly two children
C. Nodes are arranged in a circular linked list
D. Each node stores the sum of its children
Solution
Step 1: Understand BST node arrangement
In a BST, every node's left child has a smaller value and the right child has a larger value.
Step 2: Compare with other options
Other options describe different data structures or incorrect properties.
Final Answer:
Left child nodes have smaller values, right child nodes have larger values -> Option A
Quick Check:
BST property = Left smaller, right larger [OK]
Hint: Remember BST orders left < node < right [OK]
Common Mistakes:
Thinking all nodes must have two children
Confusing BST with binary tree
Assuming nodes store sums
2. Which TypeScript interface correctly represents a BST node?
easy
A. interface Node { value: number; parent: Node; }