mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-14 19:00:15 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -5,15 +5,13 @@
|
||||
#include <string>
|
||||
|
||||
// structure definition
|
||||
typedef struct trie
|
||||
{
|
||||
typedef struct trie {
|
||||
struct trie* arr[26];
|
||||
bool isEndofWord;
|
||||
} trie;
|
||||
|
||||
// create a new node for trie
|
||||
trie* createNode()
|
||||
{
|
||||
trie* createNode() {
|
||||
trie* nn = new trie();
|
||||
for (int i = 0; i < 26; i++) nn->arr[i] = NULL;
|
||||
nn->isEndofWord = false;
|
||||
@@ -21,17 +19,12 @@ trie* createNode()
|
||||
}
|
||||
|
||||
// insert string into the trie
|
||||
void insert(trie* root, std::string str)
|
||||
{
|
||||
for (int i = 0; i < str.length(); i++)
|
||||
{
|
||||
void insert(trie* root, std::string str) {
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
int j = str[i] - 'a';
|
||||
if (root->arr[j])
|
||||
{
|
||||
if (root->arr[j]) {
|
||||
root = root->arr[j];
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
root->arr[j] = createNode();
|
||||
root = root->arr[j];
|
||||
}
|
||||
@@ -40,10 +33,8 @@ void insert(trie* root, std::string str)
|
||||
}
|
||||
|
||||
// search a string exists inside the trie
|
||||
bool search(trie* root, std::string str, int index)
|
||||
{
|
||||
if (index == str.length())
|
||||
{
|
||||
bool search(trie* root, std::string str, int index) {
|
||||
if (index == str.length()) {
|
||||
if (!root->isEndofWord)
|
||||
return false;
|
||||
return true;
|
||||
@@ -59,10 +50,8 @@ removes the string if it is not a prefix of any other
|
||||
string, if it is then just sets the endofword to false, else
|
||||
removes the given string
|
||||
*/
|
||||
bool deleteString(trie* root, std::string str, int index)
|
||||
{
|
||||
if (index == str.length())
|
||||
{
|
||||
bool deleteString(trie* root, std::string str, int index) {
|
||||
if (index == str.length()) {
|
||||
if (!root->isEndofWord)
|
||||
return false;
|
||||
root->isEndofWord = false;
|
||||
@@ -73,15 +62,11 @@ bool deleteString(trie* root, std::string str, int index)
|
||||
if (!root->arr[j])
|
||||
return false;
|
||||
bool var = deleteString(root, str, index + 1);
|
||||
if (var)
|
||||
{
|
||||
if (var) {
|
||||
root->arr[j] = NULL;
|
||||
if (root->isEndofWord)
|
||||
{
|
||||
if (root->isEndofWord) {
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
int i;
|
||||
for (i = 0; i < 26; i++)
|
||||
if (root->arr[i])
|
||||
@@ -91,8 +76,7 @@ bool deleteString(trie* root, std::string str, int index)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
trie* root = createNode();
|
||||
insert(root, "hello");
|
||||
insert(root, "world");
|
||||
|
||||
Reference in New Issue
Block a user