Tree and Types of Tree
A tree is a non-linear hierarchical data structure of nodes connected by edges with a single root node and no cycles. Each node holds data and links to child nodes, supporting efficient searching, sorting, and hierarchical modeling like file systems.
Theory & Explanation
Trees, Terminology, Types of Trees & Binary Trees
1. Definition of a Tree
A Tree is a non-linear, hierarchical data structure consisting of a finite set of elements called nodes connected by directed or undirected links called edges such that there is a designated starting node called the root, every non-root node has exactly one parent, and there are no cycles (a connected acyclic graph, or a collection of disjoint trees called a forest).
Unlike linear structures (arrays, stacks, queues, linked lists), trees model hierarchical relationships such as folder hierarchies, organization charts, expression trees, and decision trees. A tree with N nodes always has N - 1 edges.
2. Basic Terminology Used with Trees
- Root: The topmost node of a tree that has no parent.
- Node: Each element in the tree storing data and pointers/references to related nodes.
- Edge: The connection/link between a parent node and a child node.
- Parent & Child: If node
Ahas a directed link to nodeB,Ais the parent ofBandBis a child ofA. - Siblings: Nodes that share the same parent.
- Leaf (External) Node: A node with no children (degree
0). - Internal (Non-Leaf) Node: A node that has at least one child.
- Degree of a Node: The number of children a node has. Degree of a Tree: The maximum degree among all nodes in the tree.
- Path & Path Length: A sequence of nodes
v1, v2, ..., vkwhere each consecutive pair is connected by an edge. Path length is the number of edges (k - 1) along the path. - Level of a Node: Distance from the root measured in edges (root is typically at Level 0; its children are at Level 1, and so on).
- Depth of a Node: The number of edges on the path from the root to that node (equals its level when root is at level
0). - Height of a Node: The number of edges on the longest downward path from that node to a leaf node. A leaf has height
0; an empty tree is often given height-1(or0by node-count convention—state the convention in exam answers). - Height of a Tree: The height of the root node (maximum depth of any leaf node).
- Subtree: Any node along with all of its descendants forms a subtree.
- Forest: A disjoint collection of zero or more trees; removing the root of a tree yields a forest of subtrees.
Graphical Representation of a Tree & Terminology (HTML/CSS Diagram)
A |
Children of A: B, C (Siblings) |
Internal nodes: A, B, C |
Leaf nodes: D, E, F |
Edges: 5 (for 6 nodes).
3. Types of Trees & Their Definitions
- General Tree: A hierarchical tree where a node can have any number (
0or more) of child nodes. - Binary Tree: A tree in which every node has at most two children, referred to as the left child and the right child.
- Full (Strict / Proper) Binary Tree: A binary tree in which every node has either
0or2children (no node has only one child). A full binary tree withLleaves has2L - 1total nodes andL - 1internal nodes. - Complete Binary Tree: A binary tree in which all levels except possibly the last are completely filled, and all nodes in the last level are filled from left to right without gaps. Ideal for array-based heap representations.
- Perfect Binary Tree: A binary tree in which all internal nodes have
2children and all leaf nodes are at the same level. A perfect binary tree of heighth(root at heighth, leaves at0) has2h+1 - 1nodes. - Skewed Binary Tree: A binary tree where all nodes have only one child—either Left-Skewed (only left children) or Right-Skewed (only right children), resembling a linked list with height
n - 1. - Binary Search Tree (BST): A binary tree where for every node
X, all keys inX's left subtree are< X(or≤ Xby convention) and all keys inX's right subtree are> X. Supports averageO(log n)search, insertion, and deletion. - AVL Tree (Self-Balancing BST): A height-balanced BST where the balance factor (
height(left) - height(right)) of every node is in{-1, 0, +1}, restored via single or double rotations after insertions/deletions. GuaranteesO(log n)worst-case operations. - Red-Black Tree: A self-balancing BST where nodes are colored red or black with color/black-height rules ensuring the longest path from root to leaf is at most twice the shortest path, keeping height
O(log n). - Heap (Max-Heap / Min-Heap): A complete binary tree satisfying the heap property: in a Max-Heap, every parent is
≥its children; in a Min-Heap, every parent is≤its children. - B-Tree & B+ Tree: Multi-way self-balancing search trees (nodes can have multiple keys and children) widely used in databases and file systems to minimize disk block reads.
- Trie (Prefix Tree): A tree structure used to store strings/prefixes over an alphabet, where edges represent characters and paths from the root spell keys/prefixes.
4. Binary Trees: Properties & Traversals
A Binary Tree restricts each node to at most two children (left and right). Key exam properties for a binary tree (with root at level 0 and height h):
- Maximum number of nodes at level
lis2l(forl ≥ 0). - Maximum number of nodes in a binary tree of height
h(where a single root has height0) is2h+1 - 1. - Minimum number of nodes in a binary tree of height
hish + 1(skewed tree). - In any non-empty binary tree with
n0leaf nodes andn2internal nodes of degree 2:n0 = n2 + 1.
Standard Binary Tree Traversals:
- Inorder (Left → Root → Right): Visits left subtree, then root, then right subtree (gives sorted order for a BST).
- Preorder (Root → Left → Right): Visits root first, then left subtree, then right subtree (useful for copying/prefix expressions).
- Postorder (Left → Right → Root): Visits left subtree, then right subtree, then root (useful for deleting a tree/postfix expressions).
Graphical Representation: Types of Binary Trees
Example: Root A → B, C; B → D, E.
Example: Root A → B, C; B → D.
5. C Program: Binary Tree Creation & Traversals
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node left;
struct Node right;
};
struct Node createNode(int val) {
struct Node node = (struct Node)malloc(sizeof(struct Node));
node->data = val;
node->left = NULL;
node->right = NULL;
return node;
}
void inorder(struct Node root) {
if (root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
void preorder(struct Node root) {
if (root == NULL) return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct Node root) {
if (root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
int main() {
/
1
/ \
2 3
/ \
4 5
/
struct Node *root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
return 0;
}
Expected Output (C):
Inorder Traversal: 4 2 5 1 3
Preorder Traversal: 1 2 4 5 3
Postorder Traversal: 4 5 2 3 1
6. Python Program: Binary Tree Creation & Traversals
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def inorder(root):
if root is None:
return []
return inorder(root.left) + [root.data] + inorder(root.right)
def preorder(root):
if root is None:
return []
return [root.data] + preorder(root.left) + preorder(root.right)
def postorder(root):
if root is None:
return []
return postorder(root.left) + postorder(root.right) + [root.data]
if __name__ == "__main__":
# 1
# / \
# 2 3
# / \
# 4 5
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Inorder Traversal: ", inorder(root))
print("Preorder Traversal: ", preorder(root))
print("Postorder Traversal:", postorder(root))
Expected Output (Python):
Inorder Traversal: [4, 2, 5, 1, 3]
Preorder Traversal: [1, 2, 4, 5, 3]
Postorder Traversal: [4, 5, 2, 3, 1]