#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define size 26
struct Node {
bool isLeaf;
Node *next[size];
Node *parent;
int wordCount, id;
Node() {
isLeaf = false;
parent = NULL;
wordCount = 0;
id = -1;
for (int i = 0; i < size; i++)
next[i] = NULL;
}
} *root;
struct Lookup {
bool isAvailable;
Node *lookedupNode;
Lookup(bool available, Node *ins) {
isAvailable = available;
lookedupNode = ins;
}
};
Lookup search(char *str) {
Node *curr = root;
for (int i = 0; str[i]; i++) {
int id = str[i] - 'a';
if (curr->next[id] == NULL) {
return Lookup(false, NULL);
}
curr = curr->next[id];
}
if (curr->isLeaf) return Lookup(true, curr);
else return Lookup(false, NULL);
}
void insert(char *str) {
Lookup var = search(str);
if (var.isAvailable) {
printf("Already Exist\n");
return;
}
Node *curr = root;
for (int i = 0; i < str[i]; i++) {
Node *temp = curr;
int id = str[i] - 'a';
if (curr->next[id] == NULL)
curr->next[id] = new Node;
curr->wordCount++;
curr = curr->next[id];
curr->parent = temp;
curr->id = id;
}
curr->isLeaf = true;
curr->wordCount++;
printf("New Entry\n");
}
void remove(Node *&curr) {
if (curr->parent) {
Node *temp = curr->parent;
if (curr->wordCount > 1) {
curr->wordCount--;
}
else {
temp->next[curr->id] = NULL;
delete curr;
curr = NULL;
}
remove(temp);
}
else {
curr->wordCount--;
if (curr->wordCount == 0) {
delete curr;
curr = NULL;
}
}
}
void findRemoveItem(char *str) {
Lookup var = search(str);
if (var.isAvailable) {
var.lookedupNode->isLeaf = false;
remove(var.lookedupNode);
}
else {
printf("Not a word\n");
}
}
void traceParent(Node *curr) {
if (curr->parent) {
traceParent(curr->parent);
printf("%c", curr->id + 'a');
}
}
void print(Node *curr) {
if (!curr) return;
//printf("wordcount = %d %c\n", curr->wordCount, curr->id + 'a');
for (int i = 0; i < size; i++) {
if (curr->next[i]) {
if (curr->next[i]->isLeaf) {
traceParent(curr->next[i]);
printf("\n");
}
print(curr->next[i]);
}
}
}
void del(Node *&cur) {
if (!cur) {
return;
}
for (int i = 0; i < size; i++)
if (cur->next[i])
del(cur->next[i]);
delete cur;
cur = NULL;
}
int main() {
root = new Node;
char arr[][10] = { "single", "sing", "singleton", "singer", "tree", "try", "trie", "sign", "sing" };
for (int i = 0; i < 9; i++)
insert(arr[i]);
print(root);
char str[10] = "sign";
search(str).isAvailable ? printf("Found\n") : printf("Not Found\n");
strcpy(str, "trye");
search(str).isAvailable ? printf("Found\n") : printf("Not Found\n");
strcpy(str, "tree");
search(str).isAvailable ? printf("Found\n") : printf("Not Found\n");
findRemoveItem(arr[0]);
print(root);
printf("\n----------------after removing %s----------------\n", arr[0]);
findRemoveItem(arr[2]);
print(root);
printf("\n----------------after removing %s----------------\n", arr[2]);
findRemoveItem(arr[3]);
print(root);
printf("\n----------------after removing %s----------------\n", arr[3]);
findRemoveItem(arr[6]);
print(root);
printf("\n----------------after removing %s----------------\n", arr[6]);
findRemoveItem(arr[4]);
print(root);
printf("\n----------------after removing %s----------------\n", arr[4]);
findRemoveItem(arr[1]);
print(root);
printf("\n----------------after removing %s----------------\n", arr[1]);
findRemoveItem(arr[5]);
print(root);
printf("\n----------------after removing %s----------------\n", arr[5]);
findRemoveItem(arr[7]);
root == NULL ? printf("root = NULL\n") : printf("root = instance %p\n", root);
print(root);
printf("\n----------------after removing %s----------------\n", arr[7]);
del(root);
root == NULL ? printf("root = NULL\n") : printf("root = instance %p\n", root);
return 0;
}