module-4 DS
module-4 DS
TREES
Binary Trees
A binary tree is a non-linear data structure consisting of nodes
connected by branches (pointers).
Each node has:
o llink: Address of the left subtree.
o info: Data stored in the node.
o rlink: Address of the right subtree.
A binary tree can be:
o Empty (no nodes).
o Non-empty, partitioned into:
1 NODE: First node of the tree.
2 NODE: Tree with 1 subtree.
3 NODE: Tree with 2 subtrees.
Key Properties
printf("Tree: ");
// Build this
tree: printTree(root); // Output:
// 10 10 5 20
// / \
// 5 20 return 0;
}
Binary Tree Insertion
(Based on Direction String - e.g., "LRL")
Insertion Logic
Goal: Insert a new node at a specific path (e.g., "LRL" = Left → Right
→ Left from root).
Steps:
1.Start at root.
2.Follow the direction string (e.g., 'L' = move left, 'R' = move right).
3.Insert the new node at the end of the path if the spot is empty
(NULL).
#include <stdio.h> // Traverse the path
#include <stdlib.h> int i;
for (i = 0; i < strlen(direction); i++) {
#include <string.h>
if (cur == NULL) break; // Path
#include <ctype.h> // For toupper() doesn't exist
struct node { prev = cur;
int data; if (toupper(direction[i]) == 'L')
struct node* left; cur = cur->left;
struct node* right; else
}; cur = cur->right;
// Function to insert a node based on directions (e.g., }
"LRL") // Check if insertion is possible
if (cur != NULL || i != strlen(direction))
struct node* insert(struct node* root, int item, char*
{
direction) { printf("Insertion failed: Invalid path
// Create new node or spot occupied.\n");
struct node* temp = (struct free(temp);
node*)malloc(sizeof(struct node)); return root;
temp->data = item; }
temp->left = temp->right = NULL; // Insert the node
// Case 1: Empty tree if (toupper(direction[i-1]) == 'L')
if (root == NULL) return temp; prev->left = temp;
else
// Case 2: Non-empty tree
prev->right = temp;
struct node* prev = NULL;
int main() {
// Create initial tree
struct node* root = NULL;
root = (struct
node*)malloc(sizeof(struct node));
root->data = 10;
root->left = root->right = NULL;
return 0;
Binary Tree Traversal
Binary Tree Traversal
• Traversal refers to visiting all nodes of a tree
systematically.
• There are three main types, differing in when we
process the root node:
The pre order traversal
void preOrder(struct node*
Definition: The pre order traversal of root) {
a binary tree can be recursively
defined as if (root == NULL) return;
follows: printf("%d ", root-
1. Process the root Node >data); // Process root first
2. Traverse the Left subtree in preOrder(root->left); //
preorder Then left subtree
3. Traverse the Right subtree in preOrder(root->right); //
preorder Finally right subtree
}
Output for our tree
(10,20,40,30):
The inorder traversal
The inorder traversalof a binary
tree can be recursively void inOrder(struct node* root)
defined as follows: {
1. Traverse the Left subtree in if (root == NULL) return;
inorder
2. Process the root Node
inOrder(root->left); //
First left subtree
3. Traverse the Right subtree in
inorder printf("%d ", root->data); //
Then root
inOrder(root->right); //
Finally right subtree
}
Output: 40 20 10 30
The postorder traversal
The postorder traversal of a
binary tree can be recursively void postOrder(struct node*
defined as follows: root) {
1. Traverse the Left subtree in if (root == NULL) return;
postorder [L] postOrder(root->left); //
2. Traverse the Right subtree in First left subtree
postorder [R]
postOrder(root->right); //
3. Process the root Node [N] Then right subtree
printf("%d ", root->data); //
Finally root
}
Searching for a Key in a
Binary Tree
• Binary Tree
Search involves looking struct Node* preorderSearch(struct Node*
for a specific value (key) in root, int key) {
the tree if (root == NULL) return NULL;
• The approach and program if (root->data == key) return root;
to search for an item using
preorder is shown below:
struct Node* left = preorderSearch(root-
Search Approach: >left, key);
• Check if current node if (left != NULL) return left;
matches the key
• If not, search left subtree return preorderSearch(root->right, key);
• Then search right subtree }
Inorder search (Left →
Root → Right)
struct Node* inorderSearch(struct
• Process: Visit left Node* root, int key) {
subtree first, then if (root == NULL) return NULL;
root, then right
subtree
• Search Approach: struct Node* left =
• Search left subtree inorderSearch(root->left, key);
first if (left != NULL) return left;
• Check current node
if (root->data == key) return
• Then search right
subtree root;
return inorderSearch(root-
>right, key);
}
struct Node* postorderSearch(struct
Node* root, int key) {
if (root == NULL) return NULL;
Postorder search
struct Node* left =
(Left → Right → Root) postorderSearch(root->left, key);
• Process: Visit left if (left != NULL) return left;
subtree, then right
subtree, then root struct Node* right =
• Search Approach: postorderSearch(root->right, key);
• Search left subtree if (right != NULL) return right;
first
• Then search right if (root->data == key) return root;
subtree
• Finally check current
node return NULL;
}
Copying a Binary Tree
Operation
Copying a Binary Tree Operation
• Copying a binary tree involves creating an exact
duplicate of the original tree while maintaining its
structure.
• This can be done using different traversal methods
(preorder, inorder, postorder), but postorder
traversal is commonly used for deletion,
while preorder traversal is natural for copying.
// Function to copy a binary tree
(recursive preorder)