0
0
DSA Typescriptprogramming~30 mins

BST Property and Why It Matters in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
BST Property and Why It Matters
📖 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.