Bird
Raised Fist0
DSA Goprogramming

Create a Binary Tree Manually in DSA Go

Choose your learning style10 modes available
Mental Model
A binary tree is a structure where each item can have up to two children, left and right. We build it by linking nodes manually one by one.
Analogy: Think of a family tree where each person can have up to two children. You start with the parent and then add children on the left and right side.
    1
   / \
  2   3
 / \
4   5
Dry Run Walkthrough
Input: Create a binary tree with root 1, left child 2, right child 3, left child of 2 is 4, right child of 2 is 5
Goal: Build the tree manually by creating nodes and linking them correctly
Step 1: Create root node with value 1
1
null null
Why: Start with the root node as the base of the tree
Step 2: Create left child node with value 2 and link to root's left
    1
   / 
  2
null null
Why: Add left child to root to build left subtree
Step 3: Create right child node with value 3 and link to root's right
    1
   / \
  2   3
null null null null
Why: Add right child to root to complete root's children
Step 4: Create left child node with value 4 and link to node 2's left
    1
   / \
  2   3
 / 
4
null null
Why: Add left child to node 2 to build its subtree
Step 5: Create right child node with value 5 and link to node 2's right
    1
   / \
  2   3
 / \
4   5
null null null null
Why: Add right child to node 2 to complete its children
Result:
    1
   / \
  2   3
 / \
4   5
Annotated Code
DSA Go
package main

import "fmt"

type Node struct {
    Val   int
    Left  *Node
    Right *Node
}

func main() {
    // Step 1: Create root node
    root := &Node{Val: 1}

    // Step 2: Create left child of root
    root.Left = &Node{Val: 2}

    // Step 3: Create right child of root
    root.Right = &Node{Val: 3}

    // Step 4: Create left child of node 2
    root.Left.Left = &Node{Val: 4}

    // Step 5: Create right child of node 2
    root.Left.Right = &Node{Val: 5}

    printTree(root, "")
}

func printTree(node *Node, prefix string) {
    if node == nil {
        return
    }
    fmt.Println(prefix + fmt.Sprint(node.Val))
    printTree(node.Left, prefix+"  ")
    printTree(node.Right, prefix+"  ")
}
root := &Node{Val: 1}
Create root node with value 1
root.Left = &Node{Val: 2}
Link left child node with value 2 to root
root.Right = &Node{Val: 3}
Link right child node with value 3 to root
root.Left.Left = &Node{Val: 4}
Link left child node with value 4 to node 2
root.Left.Right = &Node{Val: 5}
Link right child node with value 5 to node 2
OutputSuccess
1 2 4 5 3
Complexity Analysis
Time: O(1) for each node creation because each node is created and linked manually without traversal
Space: O(n) where n is the number of nodes because each node occupies space in memory
vs Alternative: Compared to building a tree from traversal arrays, manual creation is simpler but less flexible and not scalable for large trees
Edge Cases
Empty tree (no nodes)
No nodes are created, root is nil, tree is empty
DSA Go
No explicit guard needed; root remains nil if no nodes created
Single node tree
Only root node exists with no children
DSA Go
root := &Node{Val: 1} // no children linked
When to Use This Pattern
When you need to build a tree from scratch or test tree algorithms with a fixed structure, manually creating nodes and linking them is the straightforward approach.
Common Mistakes
Mistake: Forgetting to link child nodes to their parents, resulting in disconnected nodes
Fix: Always assign child nodes to the parent's Left or Right pointer explicitly
Summary
Manually create each node and link them as left or right children to build a binary tree.
Use this when you want full control over the tree structure or for small fixed trees.
The key is to create nodes first, then connect them properly to form the tree.

Practice

(1/5)
1. What is the first step to manually create a binary tree in Go?
easy
A. Define a struct type for the tree node with data and pointers
B. Write a function to traverse the tree
C. Initialize an empty slice
D. Import the fmt package

Solution

  1. Step 1: Define the node structure

    To create a binary tree manually, you first need a struct that represents each node with fields for data and pointers to left and right children.
  2. Step 2: Use this struct to create nodes and link them

    After defining the struct, you can create nodes and connect them to form the tree.
  3. Final Answer:

    Define a struct type for the tree node with data and pointers -> Option A
  4. Quick Check:

    Node struct definition = A [OK]
Hint: Start by defining the node struct with left and right pointers [OK]
Common Mistakes:
  • Trying to create nodes without defining the struct first
  • Confusing slices with tree nodes
  • Skipping pointer fields in the struct
2. Which of the following is the correct syntax to declare a binary tree node struct in Go?
easy
A. class Node { int data; Node left; Node right }
B. struct Node { int data; Node* left; Node* right }
C. Node := struct { data int; left Node; right Node }
D. type Node struct { data int; left *Node; right *Node }

Solution

  1. Step 1: Understand Go struct syntax

    In Go, structs are declared with the keyword 'type' followed by the struct name and fields inside curly braces. Pointers use '*'.
  2. Step 2: Match the correct syntax

    type Node struct { data int; left *Node; right *Node } correctly uses 'type Node struct { data int; left *Node; right *Node }' which is valid Go syntax for a binary tree node.
  3. Final Answer:

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

    Go struct with pointers = C [OK]
Hint: Remember Go uses 'type' and '*' for pointers in structs [OK]
Common Mistakes:
  • Using C/C++ or Java syntax instead of Go
  • Missing the '*' for pointer fields
  • Using := inside type declaration
3. What will be the output of this Go code that manually creates a binary tree and prints the root node's data?
package main
import "fmt"
type Node struct {
  data int
  left *Node
  right *Node
}
func main() {
  root := &Node{data: 10}
  root.left = &Node{data: 5}
  root.right = &Node{data: 15}
  fmt.Println(root.data)
}
medium
A. 10
B. 5
C. 15
D. Compilation error

Solution

  1. Step 1: Understand node creation

    The root node is created with data 10, and its left and right children are assigned 5 and 15 respectively.
  2. Step 2: Identify printed value

    The code prints root.data, which is 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    root.data = 10 [OK]
Hint: Print root.data to get the root node's value [OK]
Common Mistakes:
  • Printing left or right data instead of root
  • Confusing pointer dereferencing syntax
  • Expecting traversal output instead of root data
4. Identify the error in this Go code that tries to create a binary tree node:
type Node struct {
  data int
  left *Node
  right *Node
}
func main() {
  root := Node{data: 10}
  root.left = Node{data: 5}
  root.right = Node{data: 15}
}
medium
A. Missing semicolon after struct definition
B. Data field should be string, not int
C. Cannot assign struct to pointer field without & operator
D. No error, code is correct

Solution

  1. Step 1: Check pointer assignments

    The left and right fields are pointers (*Node), but the code assigns Node structs directly without using & to get their address.
  2. Step 2: Correct pointer assignment

    To fix, use &Node{data: 5} and &Node{data: 15} to assign pointers.
  3. Final Answer:

    Cannot assign struct to pointer field without & operator -> Option C
  4. Quick Check:

    Pointer fields need & before struct [OK]
Hint: Use & to assign address to pointer fields [OK]
Common Mistakes:
  • Assigning struct value directly to pointer field
  • Ignoring pointer type in struct fields
  • Assuming Go auto-converts struct to pointer
5. Given the following Go code to create a binary tree manually, what is the correct way to add a new node with value 7 as the left child of the node with value 5?
type Node struct {
  data int
  left *Node
  right *Node
}
func main() {
  root := &Node{data: 10}
  root.left = &Node{data: 5}
  root.right = &Node{data: 15}
  // Add new node here
}
hard
A. root.right.right = &Node{data: 7}
B. root.left.left = &Node{data: 7}
C. root.right.left = &Node{data: 7}
D. root.left.right = &Node{data: 7}

Solution

  1. Step 1: Locate node with value 5

    The node with value 5 is root.left.
  2. Step 2: Add new node as left child of node 5

    To add a left child, assign root.left.left = &Node{data: 7}.
  3. Final Answer:

    root.left.left = &Node{data: 7} -> Option B
  4. Quick Check:

    Left child of node 5 = D [OK]
Hint: Use root.left.left to add left child to node 5 [OK]
Common Mistakes:
  • Adding new node to wrong child pointer
  • Confusing left and right pointers
  • Assigning to root.right instead of root.left
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