0
0
DSA Typescriptprogramming~30 mins

BST Find Maximum Element in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
BST Find Maximum Element
📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. You want to find the largest number stored in the tree.Think of the BST like a family tree where each node has a number. The right child always has a bigger number than its parent. So, the biggest number is the rightmost node.
🎯 Goal: Build a simple BST with given numbers, then write code to find and print the maximum number in the BST.
📋 What You'll Learn
Create a BST node class called TreeNode with value, left, and right properties
Create a BST by manually linking nodes with these exact values: 15, 10, 20, 8, 12, 17, 25
Write a function called findMax that finds the maximum value in the BST by moving right
Print the maximum value found in the BST
💡 Why This Matters
🌍 Real World
BSTs are used in databases and search engines to quickly find the largest or smallest values.
💼 Career
Understanding BSTs and how to find max/min values is important for software engineers working with data structures and algorithms.
Progress0 / 4 steps
1
Create BST nodes with exact values
Create a class called TreeNode with a constructor that takes a value number and sets left and right to null. Then create these nodes exactly: root with value 15, node10 with value 10, node20 with value 20, node8 with value 8, node12 with value 12, node17 with value 17, and node25 with value 25.
DSA Typescript
Hint

Remember to set left and right to null in the constructor.

2
Link nodes to form the BST
Link the nodes to form the BST exactly as follows: root.left = node10, root.right = node20, node10.left = node8, node10.right = node12, node20.left = node17, and node20.right = node25.
DSA Typescript
Hint

Remember, left child is smaller, right child is bigger.

3
Write function to find maximum value in BST
Write a function called findMax that takes a TreeNode called node and returns the maximum value in the BST. Use a while loop to move to the right child until it is null. Return the value of the rightmost node.
DSA Typescript
Hint

Keep moving right until there is no right child.

4
Print the maximum value in the BST
Use console.log to print the maximum value found by calling findMax(root).
DSA Typescript
Hint

Call findMax(root) and print the result using console.log.