Binary Tree Foundation Visualizer
Data Structures Binary Tree Foundation

Binary Tree Foundation

Build and explore the structure of a binary tree with simple insertions and a visual node layout.

Theory

A binary tree is a hierarchical data structure where each node has at most two children, called the left and right child. This elegant structure enables efficient searching, sorting, and hierarchical representation of data. Binary trees form the foundation for more advanced structures like binary search trees and heaps. They model many real-world scenarios, from organizational hierarchies to decision-making processes, and are crucial for understanding advanced algorithms.

  • Structure: Each node has at most two children (left and right), forming a hierarchical tree
  • Operations: Insert (add nodes), Traverse (inorder, preorder, postorder), Search (find values)
  • Real-world Applications:
    • File system directory structures
    • Decision trees in machine learning
    • Expression parsing in compilers
    • Database indexing (B-trees)

Problem Statement

A binary tree organizes values in nodes with at most two children. Use the visualizer to insert new values into a tree and see how the structure grows level by level.
Next Topic: Tree Traversal →

Step 1: Build Node + Children

Understand the Binary Tree Rule First

Each node can connect to at most two children: one left and one right. Start by creating one parent node, then attach children manually to understand the structure before automatic insert logic.

binary_rule.svg

Root Left Right One parent node, max two child pointers

node_structure.c / node.py

1// C Node
2struct Node {
3int data;
4struct Node* left;
5struct Node* right;
6};
7
8# Python Node
9class Node:
10def __init__(self, data):
11self.data = data
12self.left = None
13self.right = None

Step 2: Manual Connection Approach

manual_connect.c

1struct Node* root = newNode(10);
2root->left = newNode(5);
3root->right = newNode(15);
4root->left->left = newNode(3);
5root->left->right = newNode(7);
6// We attach every pointer manually

manual_connect.py

1root = Node(10)
2root.left = Node(5)
3root.right = Node(15)
4root.left.left = Node(3)
5root.left.right = Node(7)
6# Manual links help beginners see the tree shape

Manual Connect vs Insert Logic

Manual connection: best for learning node pointers and tree shape. Insert function: best for building trees from many values automatically (like your existing visualizer below).

Use this sequence while teaching: create node → attach left/right children → connect deeper nodes → then switch to insert-based construction for scalability.

Step 3: Interactive Manual Node Linking

manual_tree_builder.svg

Current Node: None
Create nodes, choose current, then connect left/right child.

node_creation_pseudocode.txt

1function createNode(value):
2node = new Node()
3node.value = value
4node.left = null
5node.right = null
6return node

node_connection_pseudocode.txt

1function connectChild(parent, child, side):
2if side == "left":
3parent.left = child
4else:
5parent.right = child
6return parent

Existing Interactive Visualizer

node_structure.txt

1struct TreeNode {
2int value;
3TreeNode* left;
4TreeNode* right;
5}

tree_anatomy.svg

5 3 7 Parent → Left, Right

tree.svg — Node Layout

Pseudocode & Complexity

pseudocode.txt

1// Binary search tree insert
2function insert(node, value):
3if node == null:
4return newNode(value)
5else if value < node.value:
6node.left = insert(node.left, value)
7else:
8node.right = insert(node.right, value)
9return node

complexity.md

OperationTime
InsertO(h)
SearchO(h)
SpaceO(n)

C Implementation

binary_tree.c

1#include <stdio.h>
2#include <stdlib.h>
3
4struct node {
5int data;
6struct node* left;
7struct node* right;
8};
9
10struct node* newNode(int data) {
11struct node* node = (struct node*)malloc(sizeof(struct node));
12node->data = data;
13node->left = NULL;
14node->right = NULL;
15return node;
16}
17
18struct node* insert(struct node* node, int data) {
19if (node == NULL)
20return newNode(data);
21if (data < node->data)
22node->left = insert(node->left, data);
23else if (data > node->data)
24node->right = insert(node->right, data);
25return node;
26}
27
28void inorder(struct node* root) {
29if (root != NULL) {
30inorder(root->left);
31printf("%d ", root->data);
32inorder(root->right);
33}
34}
35
36int main() {
37struct node* root = NULL;
38root = insert(root, 50);
39insert(root, 30);
40insert(root, 20);
41insert(root, 40);
42insert(root, 70);
43insert(root, 60);
44insert(root, 80);
45printf("Inorder traversal: ");
46inorder(root);
47return 0;
48}