CAT-1 Assignment (3122247001066)
CAT-1 Assignment (3122247001066)
Assignment - 1
Name: K.S.Vijayaraaghavan Reg.no:3122247001062
Approach
Pseudocode
// Initialize empty queue
function createQueue():
queue.front ← null
queue.rear ← null
queue.count ← 0
return queue
Code Implementation
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// Queue structure
typedef struct {
Device *front;
Device *rear;
int count;
} DeviceQueue;
int main() {
DeviceQueue *q = createQueue();
printf("Adding devices...\n");
enqueue(q, "00:1A:2B:3C:4D:5E");
enqueue(q, "11:22:33:44:55:66");
enqueue(q, "AA:BB:CC:DD:EE:FF");
displayQueue(q);
freeQueue(q);
return 0;
}
Output
PS C:\Desktop\Folders\College\SEM-2\DSA\ass1> ./file
Adding devices...
1. MAC: 00:1A:2B:3C:4D:5E
2. MAC: 11:22:33:44:55:66
3. MAC: AA:BB:CC:DD:EE:FF
1. MAC: 11:22:33:44:55:66
2. MAC: AA:BB:CC:DD:EE:FF
Approach
• Use a Binary Search Tree (BST) keyed by full IP string for efficient insertion and lookup.
• Each node stores:
o address (string)
o isWired (bool)
o isStatic (bool)
o left/right child pointers
• Insertion or search is O(h). In-order traversal groups by category.
Pseudocode
function classifyIP(ipStr):
prefix, last ← split ipStr at last dot
isWired ← (prefix == "10.0.1")
lastOctet ← toInteger(last)
isStatic ← (lastOctet < 100)
return (isWired, isStatic)
Code Implementation
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// BST insert
IPNode* insertIP(IPNode *root, const char *ip) {
if (!root) return createNode(ip);
int cmp = strcmp(ip, root->address);
if (cmp < 0) root->left = insertIP(root->left, ip);
else if (cmp > 0) root->right = insertIP(root->right, ip);
return root;
}
// Free BST
void freeTree(IPNode *root) {
if (!root) return;
freeTree(root->left);
freeTree(root->right);
free(root);
}
int main() {
IPNode *root = NULL;
// Sample inserts
root = insertIP(root, "10.0.1.50");
root = insertIP(root, "10.0.1.150");
root = insertIP(root, "10.0.2.75");
root = insertIP(root, "10.0.2.200");
freeTree(root);
return 0;
}
Output
PS C:\Desktop\Folders\College\SEM-2\DSA\ass1> ./file
Approach
Pseudocode
function searchIP(root, ipStr):
if root is null or root.address == ipStr:
return root
if ipStr < root.address:
return searchIP(root.left, ipStr)
else:
return searchIP(root.right, ipStr)
function minValue(node):
while node.left ≠ null:
node = node.left
return node
Code Implementation
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// Insert IP
IPNode* insertIP(IPNode *root, const char *ip) {
if (!root) return createNode(ip);
int cmp = strcmp(ip, root->address);
if (cmp < 0) root->left = insertIP(root->left, ip);
else if (cmp > 0) root->right = insertIP(root->right, ip);
return root;
}
// Search IP
IPNode* searchIP(IPNode *root, const char *ip) {
if (!root || strcmp(root->address, ip) == 0) return root;
if (strcmp(ip, root->address) < 0) return searchIP(root->left, ip);
return searchIP(root->right, ip);
}
// Delete IP
IPNode* deleteIP(IPNode *root, const char *ip) {
if (!root) return NULL;
int cmp = strcmp(ip, root->address);
if (cmp < 0) root->left = deleteIP(root->left, ip);
else if (cmp > 0) root->right = deleteIP(root->right, ip);
else {
if (!root->left) {
IPNode *temp = root->right;
free(root);
return temp;
} else if (!root->right) {
IPNode *temp = root->left;
free(root);
return temp;
}
IPNode *temp = minValueNode(root->right);
strcpy(root->address, temp->address);
root->isWired = temp->isWired;
root->isStatic = temp->isStatic;
root->right = deleteIP(root->right, temp->address);
}
return root;
}
int main() {
IPNode *root = NULL;
// Insert IPs
root = insertIP(root, "10.0.1.50"); // Wired SIP
root = insertIP(root, "10.0.1.150"); // Wired DIP
root = insertIP(root, "10.0.2.75"); // Wireless SIP
root = insertIP(root, "10.0.2.200"); // Wireless DIP
// Display
printf("Wired Static IPs:\n");
displayIPsByType(root, true, true);
// Search
const char *targetIP = "10.0.1.50";
IPNode *found = searchIP(root, targetIP);
if (found) {
printf("\nFound IP: %s (%s, %s)\n", found->address,
found->isWired ? "Wired" : "Wireless",
found->isStatic ? "Static" : "Dynamic");
} else {
printf("\nIP not found.\n");
}
// Delete
printf("\nDeleting IP: %s\n", targetIP);
root = deleteIP(root, targetIP);
// Clean up
freeIPTree(root);
return 0;
}
Output
Why?
• When a new device joins the queue (enqueue), it is added at the rear.
• We simply update the rear pointer and link the new node.
• No traversal or shifting is required, regardless of queue size.
Why?
3. Add IP to BST
Time Complexity:
Why?
• To insert an IP into a binary search tree (BST), the program compares values from the root down to a
leaf.
• In a balanced BST, this takes about log₂(n) steps.
• If the tree is unbalanced (e.g., every node only has one child), the path may be n nodes long.
4. Search IP in BST
Time Complexity:
Why?
Time Complexity:
Why?
• First, the program must find the IP to delete (same as search → O(h)).
• Then:
o If the node has no children or one child: deletion is straightforward.
o If it has two children: you must also find the in-order successor (minimum in right subtree),
which can take O(h) time.
• So the total cost is based on the height h of the tree.
Summary Table