Bird
Raised Fist0
DSA Goprogramming~30 mins

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

Choose your learning style10 modes available
BST Property and Why It Matters
📖 Scenario: Imagine you are organizing a library's book collection. Each book has a unique ID number. You want to store these books so you can quickly find any book by its ID. A Binary Search Tree (BST) helps you do this by keeping books in order.
🎯 Goal: You will build a simple Binary Search Tree (BST) with a few book IDs. Then, you will check if the tree follows the BST property: for every node, all book IDs in the left subtree are smaller, and all in the right subtree are larger. Finally, you will print the tree's in-order traversal to see the sorted order of book IDs.
📋 What You'll Learn
Create a BST node structure with integer book ID
Insert given book IDs into the BST following BST rules
Write a function to check if the tree satisfies the BST property
Print the in-order traversal of the BST
💡 Why This Matters
🌍 Real World
BSTs are used in databases and file systems to organize data for fast searching, insertion, and deletion.
💼 Career
Understanding BSTs is essential for software engineers working with data storage, search algorithms, and system design.
Progress0 / 4 steps
1
Create BST Node Structure and Insert Books
Create a struct called Node with an integer field bookID and two pointers left and right to Node. Then create a function insert that takes a pointer to Node and an integer bookID, and inserts the book ID into the BST following the BST rules. Finally, create a variable root of type *Node and insert these book IDs in order: 50, 30, 70, 20, 40, 60, 80.
DSA Go
Hint

Define the Node struct with bookID, left, and right. The insert function should add nodes recursively following BST rules.

2
Add Function to Check BST Property
Create a function isBST that takes a pointer to Node and two integers min and max. It returns true if the subtree rooted at the node satisfies the BST property: all bookID values are strictly between min and max. Use isBST recursively to check left and right subtrees with updated ranges. Call isBST(root, -1, 101) and store the result in a variable validBST.
DSA Go
Hint

Use recursion to check if each node's bookID is between min and max. Update min and max when checking left and right children.

3
Write In-Order Traversal Function
Create a function inOrder that takes a pointer to Node and prints the bookID values in ascending order separated by spaces. Use recursion to first visit the left subtree, then print the current node's bookID, then visit the right subtree.
DSA Go
Hint

Use recursion to print left subtree, then current node's bookID, then right subtree.

4
Print BST Validity Result
Print "BST Valid: " followed by the value of validBST on a new line.
DSA Go
Hint

Use fmt.Printf("BST Valid: %t\n", validBST) to print the boolean result.

Practice

(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. Nodes are arranged in a complete binary tree shape
B. Left child nodes have smaller values, right child nodes have larger values
C. All nodes have exactly two children
D. Nodes store only unique values without any order

Solution

  1. Step 1: Understand BST node arrangement

    In a BST, each node's left child contains values smaller than the node, and the right child contains values larger than the node.
  2. Step 2: Compare with other tree types

    Other options describe different tree properties, but only Left child nodes have smaller values, right child nodes have larger values matches the BST property.
  3. Final Answer:

    Left child nodes have smaller values, right child nodes have larger values -> Option B
  4. Quick Check:

    BST property = Left smaller, right larger [OK]
Hint: Remember BST orders left < node < right [OK]
Common Mistakes:
  • Confusing BST with complete binary tree
  • Thinking all nodes must have two children
  • Ignoring the order property of BST
2. Which of the following Go struct definitions correctly represents a node in a BST?
easy
A. type Node struct { value int; left Node; right Node }
B. type Node struct { value int; left *int; right *int }
C. type Node struct { value int; left *Node; right *Node }
D. type Node struct { value int; left interface{}; right interface{} }

Solution

  1. Step 1: Check pointer usage for child nodes

    In Go, child nodes must be pointers to allow nil and recursive structure. type Node struct { value int; left *Node; right *Node } uses *Node pointers correctly.
  2. Step 2: Identify incorrect struct fields

    type Node struct { value int; left Node; right Node } uses Node directly, causing infinite size. type Node struct { value int; left *int; right *int } uses *int which is wrong type. type Node struct { value int; left interface{}; right interface{} } uses interface{}, losing type safety.
  3. Final Answer:

    type Node struct { value int; left *Node; right *Node } -> Option C
  4. Quick Check:

    Use pointers for recursive structs in Go [OK]
Hint: Use *Node for left and right child pointers [OK]
Common Mistakes:
  • Using struct fields instead of pointers for children
  • Using wrong pointer types like *int
  • Using interface{} losing type safety
3. What will be the in-order traversal output of this BST after inserting values in order: 10, 5, 15, 3, 7?
package main
import "fmt"
type Node struct {
  value int
  left, right *Node
}
func inOrder(root *Node) {
  if root == nil {
    return
  }
  inOrder(root.left)
  fmt.Print(root.value, " ")
  inOrder(root.right)
}
func insert(root *Node, val int) *Node {
  if root == nil {
    return &Node{value: val}
  }
  if val < root.value {
    root.left = insert(root.left, val)
  } else {
    root.right = insert(root.right, val)
  }
  return root
}
func main() {
  var root *Node
  vals := []int{10, 5, 15, 3, 7}
  for _, v := range vals {
    root = insert(root, v)
  }
  inOrder(root)
}
medium
A. 3 5 7 10 15
B. 10 5 15 3 7
C. 15 10 7 5 3
D. 5 3 7 10 15

Solution

  1. Step 1: Insert values maintaining BST property

    Insert 10 as root, 5 to left, 15 to right, 3 left of 5, 7 right of 5.
  2. Step 2: Perform in-order traversal

    In-order visits left subtree, node, right subtree. So output is 3 5 7 10 15.
  3. Final Answer:

    3 5 7 10 15 -> Option A
  4. Quick Check:

    In-order traversal of BST = sorted values [OK]
Hint: In-order traversal of BST gives sorted values [OK]
Common Mistakes:
  • Confusing pre-order or post-order with in-order
  • Not inserting nodes correctly in BST
  • Printing values in insertion order
4. The following Go code tries to insert a value into a BST but fails to correctly insert the value. What is the bug?
func insert(root *Node, val int) *Node {
  if root == nil {
    return &Node{value: val}
  }
  if val <= root.value {
    insert(root.left, val)
  } else {
    insert(root.right, val)
  }
  return root
}
medium
A. Missing assignment to root.left or root.right after recursive insert
B. Using <= instead of < causes infinite recursion
C. Returning root instead of new node causes error
D. Function should not return *Node

Solution

  1. Step 1: Analyze recursive insert calls

    The recursive calls insert(root.left, val) and insert(root.right, val) do not assign the returned node back to root.left or root.right.
  2. Step 2: Understand why assignment is needed

    Without assignment, new nodes are not linked to the tree, causing incorrect structure.
  3. Final Answer:

    Missing assignment to root.left or root.right after recursive insert -> Option A
  4. Quick Check:

    Assign recursive insert result to child pointers [OK]
Hint: Always assign recursive insert result to child pointers [OK]
Common Mistakes:
  • Forgetting to assign recursive call results
  • Confusing comparison operators causing infinite loops
  • Ignoring return values in recursive functions
5. You want to check if a binary tree is a valid BST. Which approach correctly verifies the BST property for all nodes?
hard
A. Check only immediate children values against node
B. Check in-order traversal for duplicates only
C. Check tree balance and uniqueness of values
D. Recursively verify all subtree values against node limits

Solution

  1. Step 1: Understand BST validation requirements

    BST property requires all nodes in left subtree to be less than current node and all in right subtree to be greater, not just immediate children.
  2. Step 2: Evaluate each option

    Recursively verify all subtree values against node limits correctly checks recursively with value limits. Check only immediate children values against node only checks immediate children, which can miss violations deeper in the tree.
  3. Final Answer:

    Recursively verify all subtree values against node limits -> Option D
  4. Quick Check:

    BST validation = recursive range checks [OK]
Hint: Check all subtree values recursively, not just children [OK]
Common Mistakes:
  • Checking only immediate children values
  • Confusing balance with BST property
  • Assuming no duplicates means valid BST
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard