• Root node is at index 0.
• For any node at index i:
• Left child = 2 * i + 1
• Right child = 2 * i + 2
• Parent = (i - 1) / 2
C Code: Binary Tree Using Array (with User Input)
#include <stdio.h>
#define SIZE 100
int tree[SIZE];
// Initialize tree with -1 (empty marker)
void initTree() {
for (int i = 0; i < SIZE; i++) {
tree[i] = -1;
}
}
// Insert node at a specific index
void insertNode() {
int data, index;
printf("Enter node value: ");
scanf("%d", &data);
printf("Enter index to insert at: ");
scanf("%d", &index);
if (index >= SIZE) {
printf("Error: Index out of bounds.\n");
return;
}
if (tree[index] != -1) {
printf("Error: Node already exists at index %d.\n", index);
} else {
tree[index] = data;
printf("Node inserted at index %d.\n", index);
}
}
// Display tree in array format
void displayTree() {
printf("\nBinary Tree (Array Representation):\n");
for (int i = 0; i < SIZE; i++) {
if (tree[i] != -1) {
printf("Index %d: %d\n", i, tree[i]);
}
}
}
// Main menu
int main() {
int choice;
By Mr. Rahamatulla Assistant Professor,CSE,BWU.
initTree();
while (1) {
printf("\n--- Binary Tree Menu ---\n");
printf("1. Insert Node\n");
printf("2. Display Tree\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertNode();
break;
case 2:
displayTree();
break;
case 3:
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
Sample Run:
mathematica
CopyEdit
--- Binary Tree Menu ---
1. Insert Node
2. Display Tree
3. Exit
Enter your choice: 1
Enter node value: 10
Enter index to insert at: 0
Node inserted at index 0.
--- Binary Tree Menu ---
1. Insert Node
2. Display Tree
3. Exit
Enter your choice: 2
Binary Tree (Array Representation):
Index 0: 10
By Mr. Rahamatulla Assistant Professor,CSE,BWU.
C Code: Binary Tree Using Linked List (with User Input)
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
// Create a new node
Node* createNode(int data) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// Insert node recursively (simple binary tree, not BST)
Node* insert(Node* root) {
int data;
char choice;
printf("Enter data: ");
scanf("%d", &data);
Node* newNode = createNode(data);
if (root == NULL) {
printf("Node %d inserted as root.\n", data);
return newNode;
}
printf("Insert %d to the left or right of node %d? (l/r): ", data, root->data);
scanf(" %c", &choice);
if (choice == 'l') {
root->left = insert(root->left);
} else if (choice == 'r') {
root->right = insert(root->right);
} else {
printf("Invalid choice.\n");
}
return root;
}
// Inorder traversal
void inorder(Node* root) {
if (root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
By Mr. Rahamatulla Assistant Professor,CSE,BWU.
int main() {
Node* root = NULL;
int choice;
while (1) {
printf("\n--- Binary Tree Menu ---\n");
printf("1. Insert Node\n");
printf("2. Display Inorder Traversal\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
root = insert(root);
break;
case 2:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
}
Sample Interaction:
mathematica
CopyEdit
--- Binary Tree Menu ---
1. Insert Node
2. Display Inorder Traversal
3. Exit
Enter your choice: 1
Enter data: 10
Node 10 inserted as root.
--- Binary Tree Menu ---
1. Insert Node
Enter data: 5
Insert 5 to the left or right of node 10? (l/r): l
Node 5 inserted.
--- Binary Tree Menu ---
2. Display Inorder Traversal
Inorder Traversal: 5 10
By Mr. Rahamatulla Assistant Professor,CSE,BWU.
✅ Binary Search Tree in C with User Input
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a BST node
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// Insert a value into the BST
Node* insert(Node* root, int data) {
if (root == NULL) {
return createNode(data); // Empty spot found, insert here
}
if (data < root->data) {
root->left = insert(root->left, data); // Insert in left subtree
} else if (data > root->data) {
root->right = insert(root->right, data); // Insert in right subtree
} else {
printf("Duplicate value %d not allowed in BST.\n", data);
}
return root;
}
// Inorder traversal (Left, Root, Right)
void inorder(Node* root) {
if (root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
int main() {
Node* root = NULL;
int choice, value;
while (1) {
printf("\n--- Binary Search Tree Menu ---\n");
printf("1. Insert Value\n");
printf("2. Display Inorder Traversal\n");
printf("3. Exit\n");
printf("Enter your choice: ");
By Mr. Rahamatulla Assistant Professor,CSE,BWU.
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
Sample Output
mathematica
CopyEdit
--- Binary Search Tree Menu ---
1. Insert Value
2. Display Inorder Traversal
3. Exit
Enter your choice: 1
Enter value to insert: 50
--- Binary Search Tree Menu ---
1. Insert Value
Enter value to insert: 30
--- Binary Search Tree Menu ---
2. Display Inorder Traversal
Inorder Traversal: 30 50
By Mr. Rahamatulla Assistant Professor,CSE,BWU.