0
0
DSA Goprogramming

Create a Binary Tree Manually in DSA Go

Choose your learning style9 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.