A binary tree is a hierarchical data structure where each node has at most two children, referred to as the left child and right child. This constraint makes binary trees particularly efficient for certain operations and forms the foundation for more specialized structures like Binary Search Trees and heaps.
Binary trees provide efficient O(log n) performance for search, insertion, and deletion operations when balanced. They are widely used in compilers, expression parsing, and database indexing systems.
Step 1: Create a root node by entering a value and clicking "Create Root".
Step 2: Click on any node to select it (it will turn blue).
Step 3: Enter a value and click "Add to Selected Node" under Left or Right Child to add children.
Step 4: Each node can have at most one left child and one right child.
Note: The tree automatically arranges nodes to maintain proper binary tree structure.
Full Binary Tree: Every node has either 0 or 2 children.
Complete Binary Tree: All levels are filled except possibly the last, filled left to right.
Perfect Binary Tree: All internal nodes have 2 children and all leaves are at the same level.
Balanced Binary Tree: Height difference between left and right subtrees is at most 1.
Degenerate Tree: Each parent has only one child (essentially a linked list).
In-Order (Left-Root-Right): Visit left subtree, root, then right subtree.
Pre-Order (Root-Left-Right): Visit root, left subtree, then right subtree.
Post-Order (Left-Right-Root): Visit left subtree, right subtree, then root.
Level-Order: Visit nodes level by level from top to bottom, left to right.
Insert: Add a node as left or right child of a selected parent.
Delete: Remove a node; handle cases for leaf, one child, or two children.
Search: Find a node by traversing the tree (O(n) without ordering).
Traversal: Visit all nodes in preorder, inorder, postorder, or level-order.
Height: Calculate the longest path from root to a leaf node.
Count Nodes: Count total, leaf, or internal nodes recursively.
Recursive DFS: Solve problems by combining solutions from left and right subtrees.
BFS with Queue: Process level by level; track level size for level-specific operations.
Tree Diameter: Longest path between any two nodes; max of (leftHeight + rightHeight) at each node.
Symmetric Tree: Check if tree is mirror of itself by comparing left and right subtrees.
Path Sum Problems: Track current sum while traversing; backtrack when returning.
Construct from Traversals: Build tree from inorder + preorder/postorder using recursion.
Vertical/Diagonal Order: Use column index mapping with BFS or DFS for ordered output.
/* Binary Tree implementation in C */
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int value;
struct Node* left;
struct Node* right;
} Node;
Node* createNode(int value) {
Node* node = (Node*)malloc(sizeof(Node));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
/* Tree traversals */
void inOrder(Node* node) {
if (node == NULL) return;
inOrder(node->left);
printf("%d ", node->value);
inOrder(node->right);
}
void preOrder(Node* node) {
if (node == NULL) return;
printf("%d ", node->value);
preOrder(node->left);
preOrder(node->right);
}
void postOrder(Node* node) {
if (node == NULL) return;
postOrder(node->left);
postOrder(node->right);
printf("%d ", node->value);
}
int main() {
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
printf("In-order: "); inOrder(root); printf("\n");
printf("Pre-order: "); preOrder(root); printf("\n");
printf("Post-order: "); postOrder(root); printf("\n");
return 0;
}