Module 4: Trees
1. Introduction to Trees
A tree is a non-linear hierarchical data structure consisting of nodes connected by edges. It begins at a
root node and branches out to child nodes.
1.1 Basic Terminology
• Root: The topmost node.
• Child: A node directly connected to another node moving away from the root.
• Parent: The converse of a child.
• Leaf Node: A node with no children.
• Internal Node: A node with at least one child.
• Subtree: A tree formed by a node and its descendants.
• Degree: Number of children of a node.
• Height: Longest path from root to leaf.
• Depth: Distance from root to the node.
2. Binary Trees
A binary tree is a tree in which each node has at most two children: left and right.
2.1 Representation of Binary Trees
Binary trees are represented using linked structures:
typedef struct Node {
int data;
struct Node *left, *right;
} Node;
3. Binary Tree Operations
3.1 Insertion
Insertion depends on context — full binary tree, BST, etc. In a basic tree, insert left or right manually.
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
3.2 Traversal
Inorder (Left, Root, Right):
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
Preorder (Root, Left, Right):
void preorder(Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
Postorder (Left, Right, Root):
void postorder(Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
3.3 Searching
Node* search(Node* root, int key) {
if (root == NULL || root->data == key) return root;
if (key < root->data)
return search(root->left, key);
return search(root->right, key);
3.4 Copying a Tree
Node* copyTree(Node* root) {
if (root == NULL) return NULL;
Node* newNode = createNode(root->data);
newNode->left = copyTree(root->left);
newNode->right = copyTree(root->right);
return newNode;
4. Binary Search Trees (BST)
A BST is a binary tree where the left child is less than the root and the right child is greater.
4.1 Insertion in BST
Node* insert(Node* root, int key) {
if (root == NULL) return createNode(key);
if (key < root->data)
root->left = insert(root->left, key);
else
root->right = insert(root->right, key);
return root;
4.2 Searching in BST
Same as tree search using the BST property (shown above).
4.3 Find Maximum and Minimum
int findMin(Node* root) {
while (root->left != NULL)
root = root->left;
return root->data;
int findMax(Node* root) {
while (root->right != NULL)
root = root->right;
return root->data;
4.4 Count Total Nodes
int countNodes(Node* root) {
if (root == NULL) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
5. Expression Trees
An expression tree is a binary tree representing arithmetic expressions. Leaves are operands, internal
nodes are operators.
Example: Expression (a + b) * (c - d)
/\
+ -
/\/\
a bc d
Construction and evaluation of such trees follow postorder traversal for evaluation.