-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathTernarySearchTree.cpp
117 lines (103 loc) · 3.8 KB
/
TernarySearchTree.cpp
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
115
116
117
// Ternary Search Tree (TST) implementation in C++
#include <iostream>
using namespace std;
// The algorithm of the Ternary Search Tree (TST)
//1.) Create a TST node class with character data, left, middle, and right pointers, and an end-of-word flag.
//2.) Implement an insert() function that recursively inserts characters into the TST based on comparisons.
//3.) Implement a search() function that recursively searches for a string in the TST by comparing characters.
//4.) Initialize an empty TST and use the insert() function to add strings to the TST.
//5.) Use the search() function to check if a string exists in the TST.
// Node class represents a node in the TST
class Node {
public:
char data; // The character stored in the node
bool isEndOfWord; // Flag to indicate if the node represents the end of a word
Node* left, * middle, * right; // Pointers to the left, middle, and right child nodes
// Constructor to initialize the node
Node(char data) {
this->data = data;
this->isEndOfWord = false;
this->left = this->middle = this->right = nullptr;
}
};
// Ternary Search Tree class
class TernarySearchTree {
private:
Node* root; // Root node of the TST
public:
// Constructor to initialize the TST
TernarySearchTree() {
this->root = nullptr;
}
// Method to insert a string into the TST
void insert(string word) {
root = insert(root, word, 0);
}
// Recursive method to insert a character into the TST
Node* insert(Node* node, const string& word, int index) {
// If the current node is null, create a new node
if (node == nullptr) {
node = new Node(word[index]);
}
// Compare the current character with the character in the node
if (word[index] < node->data) {
node->left = insert(node->left, word, index);
}
else if (word[index] > node->data) {
node->right = insert(node->right, word, index);
}
else {
// If the characters match, move to the next character
if (index + 1 < word.length()) {
node->middle = insert(node->middle, word, index + 1);
}
else {
// Mark the node as the end of a word
node->isEndOfWord = true;
}
}
return node;
}
// Method to search for a string in the TST
bool search(string word) {
return search(root, word, 0);
}
// Recursive method to search for a character in the TST
bool search(Node* node, const string& word, int index) {
// If the current node is null, the word does not exist in the TST
if (node == nullptr) {
return false;
}
// Compare the current character with the character in the node
if (word[index] < node->data) {
return search(node->left, word, index);
}
else if (word[index] > node->data) {
return search(node->right, word, index);
}
else {
// If the characters match, move to the next character
if (index + 1 == word.length()) {
// Check if the node represents the end of a word
return node->isEndOfWord;
}
else {
return search(node->middle, word, index + 1);
}
}
}
};
// Main function to test the TernarySearchTree implementation
int main() {
TernarySearchTree tst;
// Insert some words into the TST
tst.insert("apple");
tst.insert("banana");
tst.insert("carrot");
cout<<"Enter String "<<endl;
string s;
cin>>s;
if(tst.search(s) == 1) cout<<s<<" is Present "<< endl;// Output: 1 (exists)
else cout<<s<<" is not Present "<< endl; // Output: 0 (does not exist)
return 0;
}