-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymboltable.c
114 lines (92 loc) · 2.61 KB
/
symboltable.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "symboltable.h"
struct symbol* createTable()
{
struct symbol* root = (struct symbol*)malloc(sizeof(struct symbol));
root->symbol_name = NULL;
return root;
}
struct symbol* addSymbol(struct symbol* node, char* symbol_name)
{
/* We've reached a node that's unallocated, it will be our new leaf */
if (node == NULL)
{
printf("win?\n");
struct symbol* root = (struct symbol*)malloc(sizeof(struct symbol));
root->symbol_name = symbol_name;
return root;
}
/* Compare strings so we know where to put our new node */
enum ComparisonValue comparison = compareStrings(symbol_name, node->symbol_name);
if (comparison == LESS_THAN)
{
/* if left is open, put it there, else, let's kill going down the tree */
if (node->left == NULL)
{
node->left = (struct symbol*)malloc(sizeof(struct symbol));
node->left->symbol_name = symbol_name;
return node->left;
}
else
{
return addSymbol(node->left, symbol_name);
}
}
else if (comparison == GREATER_THAN)
{
/* if right is open, put it there, else, let's kill going down the tree */
if (node->right == NULL)
{
node->right = (struct symbol*)malloc(sizeof(struct symbol));
node->right->symbol_name = symbol_name;
return node->right;
}
else
{
return addSymbol(node->right, symbol_name);
}
}
else /* comparison is equal */
{
return NULL;
}
}
uint16_t calculateHeight(struct symbol* node)
{
/* we've reached the end, return 0 */
if (node == NULL)
{
return 0;
}
uint16_t left_subtree_height = calculateHeight(node->left);
uint16_t right_subtree_height = calculateHeight(node->right);
if (left_subtree_height < right_subtree_height)
return right_subtree_height + 1;
else
return left_subtree_height + 1;
}
/* Return a value from the ComparisonValue enum relative between first term and second term */
enum ComparisonValue compareStrings(const char* first_term, const char* second_term)
{
uint32_t first_term_length = strlen(first_term);
uint32_t second_term_length = strlen(second_term);
uint32_t comparison_length;
/* Find the smaller length */
if (first_term_length < second_term_length)
comparison_length = first_term_length;
else
comparison_length = second_term_length;
/* We need to compare up to the smaller length number of characters */
for (uint32_t index = 0; index < comparison_length; index++)
{
if (first_term[index] > second_term[index])
return GREATER_THAN;
else if (first_term[index] < second_term[index])
return LESS_THAN;
}
if (first_term_length == second_term_length)
return EQUAL_TO;
else if (first_term_length < second_term_length)
return LESS_THAN;
else
return GREATER_THAN;
}