0% found this document useful (0 votes)
2 views7 pages

cprg

The document contains C code for implementing a Binary Search Tree (BST) and a Queue. It includes functions for creating nodes, inserting elements, and performing in-order, pre-order, and post-order traversals for the BST, as well as enqueueing and dequeueing operations for the Queue. The main function demonstrates the usage of both data structures with sample outputs.

Uploaded by

Top 3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

cprg

The document contains C code for implementing a Binary Search Tree (BST) and a Queue. It includes functions for creating nodes, inserting elements, and performing in-order, pre-order, and post-order traversals for the BST, as well as enqueueing and dequeueing operations for the Queue. The main function demonstrates the usage of both data structures with sample outputs.

Uploaded by

Top 3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <stdio.

h>

#include <stdlib.h>

// Define the structure for a node in the BST

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

// Function to insert a node into the BST

struct Node* insert(struct Node* root, int data) {

if (root == NULL) {

return createNode(data);

if (data < root->data) {

root->left = insert(root->left, data);

} else if (data > root->data) {

root->right = insert(root->right, data);

return root;

}
// Function for in-order traversal (Left -> Root -> Right)

void inorderTraversal(struct Node* root) {

if (root != NULL) {

inorderTraversal(root->left);

printf("%d ", root->data);

inorderTraversal(root->right);

// Function for pre-order traversal (Root -> Left -> Right)

void preorderTraversal(struct Node* root) {

if (root != NULL) {

printf("%d ", root->data);

preorderTraversal(root->left);

preorderTraversal(root->right);

// Function for post-order traversal (Left -> Right -> Root)

void postorderTraversal(struct Node* root) {

if (root != NULL) {

postorderTraversal(root->left);

postorderTraversal(root->right);

printf("%d ", root->data);

int main() {

struct Node* root = NULL;


// Insert nodes into the BST

root = insert(root, 50);

root = insert(root, 30);

root = insert(root, 70);

root = insert(root, 20);

root = insert(root, 40);

root = insert(root, 60);

root = insert(root, 80);

printf("BINARY SEARCH TREE TRAVERSAL:\n");

printf("In-order Traversal: ");

inorderTraversal(root);

printf("\n");

printf("Pre-order Traversal: ");

preorderTraversal(root);

printf("\n");

printf("Post-order Traversal: ");

postorderTraversal(root);

printf("\n");

return 0;

Output:

BINARY SEARCH TREE TRAVERSAL:

In-order Traversal: 20 30 40 50 60 70 80

Pre-order Traversal: 50 30 20 40 70 60 80

Post-order Traversal: 20 40 30 60 80 70 50
#include <stdio.h>

#include <stdlib.h>

#define MAX 5 // Maximum size of the queue

// Define the queue structure

struct Queue {

int items[MAX];

int front, rear;

};

// Function to initialize the queue

void initializeQueue(struct Queue *q) {

q->front = -1;

q->rear = -1;

// Function to check if the queue is empty

int isEmpty(struct Queue *q) {

return q->front == -1;

// Function to check if the queue is full

int isFull(struct Queue *q) {

return q->rear == MAX - 1;

// Function to add an element to the queue

void enqueue(struct Queue *q, int value) {

if (isFull(q)) {

printf("Queue is Full! Cannot enqueue %d.\n", value);


} else {

if (q->front == -1) {

q->front = 0; // Initialize front for the first element

q->rear++;

q->items[q->rear] = value;

printf("Enqueued: %d\n", value);

// Function to remove an element from the queue

int dequeue(struct Queue *q) {

if (isEmpty(q)) {

printf("Queue is Empty! Cannot dequeue.\n");

return -1;

} else {

int value = q->items[q->front];

if (q->front == q->rear) {

// Reset queue when the last element is dequeued

q->front = q->rear = -1;

} else {

q->front++;

printf("Dequeued: %d\n", value);

return value;

// Function to display the elements of the queue

void display(struct Queue *q) {

if (isEmpty(q)) {
printf("Queue is Empty!\n");

} else {

printf("Queue elements: ");

for (int i = q->front; i <= q->rear; i++) {

printf("%d ", q->items[i]);

printf("\n");

int main() {

struct Queue q;

initializeQueue(&q);

printf("QUEUE OPERATIONS\n");

enqueue(&q, 10);

enqueue(&q, 20);

enqueue(&q, 30);

enqueue(&q, 40);

enqueue(&q, 50);

display(&q);

dequeue(&q);

dequeue(&q);

display(&q);

enqueue(&q, 60); // Attempt to enqueue into the queue


enqueue(&q, 70);

display(&q);

return 0;

Output:

QUEUE OPERATIONS

Enqueued: 10

Enqueued: 20

Enqueued: 30

Enqueued: 40

Enqueued: 50

Queue elements: 10 20 30 40 50

Dequeued: 10

Dequeued: 20

Queue elements: 30 40 50

Queue is Full! Cannot enqueue 60.

Queue is Full! Cannot enqueue 70.

Queue elements: 30 40 50

You might also like