• Project Of DataStructure Tree node All Code
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
// Define the node structure.
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Create a new node with given data.
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation error!\n");
exit(1);
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
// Iterative BST insertion.
struct Node* insert(struct Node* root, int data) {
struct Node* newNode = createNode(data);
if (root == NULL)
return newNode;
struct Node* current = root;
struct Node* parent = NULL;
while (current) {
parent = current;
if (data < current->data)
current = current->left;
else
current = current->right;
if (data < parent->data)
parent->left = newNode;
else
parent->right = newNode;
return root;
// Iterative search in BST. Returns pointer to the node if found; otherwise NULL.
struct Node* searchNode(struct Node* root, int key) {
while (root) {
if (root->data == key)
return root;
if (key < root->data)
root = root->left;
else
root = root->right;
}
return NULL;
// Find the minimum value in BST.
int findMin(struct Node* root) {
if (root == NULL) return INT_MIN;
while (root->left)
root = root->left;
return root->data;
// Find the maximum value in BST.
int findMax(struct Node* root) {
if (root == NULL) return INT_MAX;
while (root->right)
root = root->right;
return root->data;
// Iterative Level-order traversal.
void levelOrderTraversal(struct Node* root) {
if (root == NULL) return;
struct Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
struct Node* current = queue[front++];
printf("%d ", current->data);
if (current->left)
queue[rear++] = current->left;
if (current->right)
queue[rear++] = current->right;
printf("\n");
// Iterative In-order traversal using a stack.
void inOrderTraversal(struct Node* root) {
struct Node* stack[100];
int top = -1;
struct Node* current = root;
while (current || top >= 0) {
while (current) {
stack[++top] = current;
current = current->left;
current = stack[top--];
printf("%d ", current->data);
current = current->right;
printf("\n");
// Iterative Pre-order traversal using a stack.
void preOrderTraversal(struct Node* root) {
if (root == NULL)
return;
struct Node* stack[100];
int top = -1;
stack[++top] = root;
while (top >= 0) {
struct Node* current = stack[top--];
printf("%d ", current->data);
// Push right first so that left is processed first.
if (current->right)
stack[++top] = current->right;
if (current->left)
stack[++top] = current->left;
printf("\n");
// Iterative Post-order traversal using two stacks.
void postOrderTraversal(struct Node* root) {
if (root == NULL)
return;
struct Node* stack1[100];
struct Node* stack2[100];
int top1 = -1, top2 = -1;
stack1[++top1] = root;
while (top1 >= 0) {
struct Node* current = stack1[top1--];
stack2[++top2] = current;
if (current->left)
stack1[++top1] = current->left;
if (current->right)
stack1[++top1] = current->right;
while (top2 >= 0) {
printf("%d ", stack2[top2--]->data);
printf("\n");
// Iterative deletion from BST.
// This function deletes the node with key "key" and returns the new root.
struct Node* deleteNode(struct Node* root, int key) {
struct Node* parent = NULL;
struct Node* current = root;
// Search for the node.
while (current && current->data != key) {
parent = current;
if (key < current->data)
current = current->left;
else
current = current->right;
if (current == NULL) {
printf("Node with key %d not found.\n", key);
return root; // key not found.
}
// Case 1: Node has no children.
if (current->left == NULL && current->right == NULL) {
if (parent == NULL) // Node to delete is root.
root = NULL;
else if (parent->left == current)
parent->left = NULL;
else
parent->right = NULL;
free(current);
// Case 2: Node has two children.
else if (current->left && current->right) {
// Find inorder successor (smallest in right subtree).
struct Node* successor = current->right;
struct Node* successorParent = current;
while (successor->left) {
successorParent = successor;
successor = successor->left;
// Copy successor's data to current node.
current->data = successor->data;
// Delete the successor now.
if (successorParent->left == successor)
successorParent->left = successor->right;
else
successorParent->right = successor->right;
free(successor);
// Case 3: Node has one child.
else {
struct Node* child = (current->left) ? current->left : current->right;
if (parent == NULL)
root = child;
else if (parent->left == current)
parent->left = child;
else
parent->right = child;
free(current);
return root;
// Display the full tree by printing nodes level by level.
// This iterative level-order display also prints the level number.
void displayFullTree(struct Node* root) {
if (root == NULL) {
printf("Tree is empty.\n");
return;
struct Node* queue[100];
int levels[100];
int front = 0, rear = 0;
queue[rear] = root;
levels[rear] = 0;
rear++;
int currentLevel = 0;
printf("Level %d: ", currentLevel);
while (front < rear) {
struct Node* curr = queue[front];
int lev = levels[front];
front++;
if (lev > currentLevel) {
currentLevel = lev;
printf("\nLevel %d: ", currentLevel);
printf("%d ", curr->data);
if (curr->left) {
queue[rear] = curr->left;
levels[rear] = lev + 1;
rear++;
if (curr->right) {
queue[rear] = curr->right;
levels[rear] = lev + 1;
rear++;
printf("\n");
int main() {
struct Node* root = NULL;
int choice, value;
// The menu follows the order:
// 1. Display Tree
// 2. Search
// 3. Insert new node(s) line by line (stop on alphabet)
// 4. Delete a Node
// 5. Find Minimum
// 6. Find Maximum
// 7. Pre-order Traversal
// 8. In-order Traversal
// 9. Post-order Traversal
// 10. Level-order Traversal
// 11. Exit
do {
printf("\n======================================\n");
printf("1. Display full tree (level by level)\n");
printf("2. Search for a value\n");
printf("3. Insert new node(s) (line by line; non-numeric input stops insertion)\n");
printf("4. Delete a node\n");
printf("5. Find minimum value\n");
printf("6. Find maximum value\n");
printf("7. Pre-order traversal\n");
printf("8. In-order traversal\n");
printf("9. Post-order traversal\n");
printf("10. Level-order traversal\n");
printf("11. Exit\n");
printf("Enter your choice: ");
if(scanf("%d", &choice) != 1) {
// In case of non-numeric choice.
printf("Invalid input. Exiting.\n");
break;
}
switch (choice) {
case 1:
printf("Displaying full tree:\n");
displayFullTree(root);
break;
case 2:
printf("Enter value to search: ");
scanf("%d", &value);
if (searchNode(root, value))
printf("Value %d found in the tree.\n", value);
else
printf("Value %d not found in the tree.\n", value);
break;
case 3: {
// Clear any lingering newline from previous input.
while(getchar() != '\n');
char input[20];
printf("Insert nodes line by line. Enter a non-numeric input to stop.\n");
while (1) {
printf("Enter value: ");
if (fgets(input, sizeof(input), stdin) == NULL)
break;
// Trim newline if present.
input[strcspn(input, "\n")] = 0;
// Attempt conversion; if it fails, break.
if (sscanf(input, "%d", &value) != 1)
break;
root = insert(root, value);
break;
}
case 4:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;
case 5:
if (root)
printf("Minimum value in the tree: %d\n", findMin(root));
else
printf("Tree is empty.\n");
break;
case 6:
if (root)
printf("Maximum value in the tree: %d\n", findMax(root));
else
printf("Tree is empty.\n");
break;
case 7:
printf("Pre-order traversal: ");
preOrderTraversal(root);
break;
case 8:
printf("In-order traversal: ");
inOrderTraversal(root);
break;
case 9:
printf("Post-order traversal: ");
postOrderTraversal(root);
break;
case 10:
printf("Level-order traversal: ");
levelOrderTraversal(root);
break;
case 11:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
} while (choice != 11);
return 0;
}
*/ Running Project