-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouting.c
67 lines (61 loc) · 2.14 KB
/
Routing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct binaryTreeStruct{
char *routeName;
char *routePath;
struct binaryTreeStruct *left, *right;
};
struct binaryTreeStruct * createBinaryTree(char* routeName, char* routePath){
struct binaryTreeStruct *node = (struct binaryTreeStruct * )malloc(sizeof(struct binaryTreeStruct *));
node->routeName = (char *)malloc(sizeof(routeName));
node->routePath = (char *)malloc(sizeof(routePath));
if(node == NULL || node->routeName == NULL || node->routePath == NULL){
printf("Memory allocation patladi");
exit(1);
}
strcpy(node->routeName, routeName);
strcpy(node->routePath, routePath);
node->left = node->right = NULL;
return node;
}
struct binaryTreeStruct * searchRoute(struct binaryTreeStruct *root, char *routeName){
if(root == NULL || strcmp(root->routeName,routeName) == 0) {
return root;
}
if(strcmp(root->routeName, routeName) > 0) {
return searchRoute(root->right, routeName);
}
return searchRoute(root->left, routeName);
};
struct binaryTreeStruct * addRoute(struct binaryTreeStruct *node, char *routeName, char* routePath) {
if(node == NULL) {
return createBinaryTree(routeName, routePath);
}
if(strcmp(node->routeName, routeName) == 0){
printf("----------BRUH NOO-----------\n");
printf("There is already route assigned to this place");
} else if(strcmp(node->routeName, routeName) > 0) {
node->right = addRoute(node->right, routeName, routePath);
} else if(strcmp(node->routeName, routeName) < 0){
node->left = addRoute(node->left, routeName, routePath);
}
return node;
};
void postOrder(struct binaryTreeStruct *node) {
if(node != NULL) {
postOrder(node->left);
postOrder(node->right);
printf("Here is the routes: %s and their paths: %s\n", node->routeName, node->routePath);
}
}
void freeBinaryTree(struct binaryTreeStruct *node) {
if (node == NULL) {
return;
}
freeBinaryTree(node->left);
freeBinaryTree(node->right);
free(node->routeName);
free(node->routePath);
free(node);
}