C Program to Implement Create and Display Operation for a Binary Tree
A Binary Tree is a hierarchical data structure where each node has at most two children: a left child and a right child. In this program, we will implement Create and Display operations in a simple, student-friendly way.
📝 Problem Statement
B) Write C programs to implement create and display operation for a binary tree.
💡 Logic (Simple Steps)
🔹 Create Operation
- Define a structure for a node with:
dataleftpointerrightpointer
- Create a function to insert nodes:
- Allocate memory for a new node.
- Assign data to the node.
- Set
leftandrightpointers toNULL. - Insert nodes manually or using recursion (for simplicity, we insert manually in this program).
🔹 Display Operation
- Use Preorder Traversal (Root → Left → Right) to display nodes.
- Recursive function to traverse and print each node.
💻 Source Code (Student-Friendly Version)
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct node
{
int data;
struct node *left;
struct node *right;
};
// Function to create a new node
struct node* createNode(int value)
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
// Function to display tree (Preorder Traversal)
void displayTree(struct node* root)
{
if(root == NULL)
return;
printf("%d ", root->data); // Visit root
displayTree(root->left); // Traverse left subtree
displayTree(root->right); // Traverse right subtree
}
int main()
{
struct node *root = NULL;
int n, value, i;
printf("Enter number of nodes: ");
scanf("%d", &n);
// For simplicity, creating tree manually
// First node will be root
if(n > 0)
{
printf("Enter value for root node: ");
scanf("%d", &value);
root = createNode(value);
}
// Insert remaining nodes
for(i = 1; i < n; i++)
{
printf("Enter value for node %d: ", i+1);
scanf("%d", &value);
// Simple insertion: left if vacant else right
struct node* newnode = createNode(value);
struct node* temp = root;
while(1)
{
if(temp->left == NULL)
{
temp->left = newnode;
break;
}
else if(temp->right == NULL)
{
temp->right = newnode;
break;
}
else
{
// Move to left child for next insertion
temp = temp->left;
}
}
}
printf("Binary Tree nodes (Preorder Traversal):\n");
displayTree(root);
return 0;
}
🖥️ Sample Output
Enter number of nodes: 5
Enter value for root node: 10
Enter value for node 2: 20
Enter value for node 3: 30
Enter value for node 4: 40
Enter value for node 5: 50
Binary Tree nodes (Preorder Traversal):
10 20 40 50 30
🎓 Viva Questions with Answers
1. What is a binary tree?
A tree where each node has at most two children: left and right.
2. What are the types of binary trees?
Full binary tree, Complete binary tree, Perfect binary tree, etc.
3. What is the difference between binary tree and linked list?
A linked list is linear, a binary tree is hierarchical.
4. Which traversal method is used in this program?
Preorder Traversal (Root → Left → Right).
5. How do you insert a node in a binary tree?
By creating a new node and linking it to an empty left or right child.
