forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AdarshAddee#57 from Shriansh2002/main
Added xor Linked List and trie
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Operation Time-Complexity | ||
// Insertion O(n) | ||
// Searching O(n) | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
const int ALPHABET_SIZE = 26; | ||
|
||
struct TrieNode | ||
{ | ||
struct TrieNode *children[ALPHABET_SIZE]; | ||
|
||
bool isEndOfWord; | ||
}; | ||
|
||
struct TrieNode *getNode(void) | ||
{ | ||
struct TrieNode *pNode = new TrieNode; | ||
|
||
pNode->isEndOfWord = false; | ||
|
||
for (int i = 0; i < ALPHABET_SIZE; i++) | ||
pNode->children[i] = NULL; | ||
|
||
return pNode; | ||
} | ||
|
||
void insert(struct TrieNode *root, string key) | ||
{ | ||
struct TrieNode *pCrawl = root; | ||
|
||
for (int i = 0; i < key.length(); i++) | ||
{ | ||
int index = key[i] - 'a'; | ||
if (!pCrawl->children[index]) | ||
pCrawl->children[index] = getNode(); | ||
|
||
pCrawl = pCrawl->children[index]; | ||
} | ||
|
||
pCrawl->isEndOfWord = true; | ||
} | ||
|
||
bool search(struct TrieNode *root, string key) | ||
{ | ||
struct TrieNode *pCrawl = root; | ||
|
||
for (int i = 0; i < key.length(); i++) | ||
{ | ||
int index = key[i] - 'a'; | ||
if (!pCrawl->children[index]) | ||
return false; | ||
|
||
pCrawl = pCrawl->children[index]; | ||
} | ||
|
||
return (pCrawl->isEndOfWord); | ||
} | ||
|
||
int main() | ||
{ | ||
|
||
string keys[] = {"the", "a", "there", | ||
"answer", "any", "by", | ||
"bye", "their"}; | ||
int n = sizeof(keys) / sizeof(keys[0]); | ||
|
||
struct TrieNode *root = getNode(); | ||
|
||
for (int i = 0; i < n; i++) | ||
insert(root, keys[i]); | ||
|
||
char output[][32] = {"Not present in trie", "Present in trie"}; | ||
|
||
cout << "the" | ||
<< " --- " << output[search(root, "the")] << endl; | ||
cout << "these" | ||
<< " --- " << output[search(root, "these")] << endl; | ||
cout << "their" | ||
<< " --- " << output[search(root, "their")] << endl; | ||
cout << "thaw" | ||
<< " --- " << output[search(root, "thaw")] << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <bits/stdc++.h> | ||
#include <cinttypes> | ||
|
||
using namespace std; | ||
|
||
class Node | ||
{ | ||
public: | ||
int data; | ||
Node *xnode; | ||
}; | ||
|
||
Node *Xor(Node *x, Node *y) | ||
{ | ||
return reinterpret_cast<Node *>( | ||
reinterpret_cast<uintptr_t>(x) ^ reinterpret_cast<uintptr_t>(y)); | ||
} | ||
|
||
void insert(Node **head_ref, int data) | ||
{ | ||
|
||
Node *new_node = new Node(); | ||
new_node->data = data; | ||
|
||
new_node->xnode = *head_ref; | ||
|
||
if (*head_ref != NULL) | ||
{ | ||
|
||
(*head_ref) | ||
->xnode = Xor(new_node, (*head_ref)->xnode); | ||
} | ||
|
||
*head_ref = new_node; | ||
} | ||
|
||
void printList(Node *head) | ||
{ | ||
Node *curr = head; | ||
Node *prev = NULL; | ||
Node *next; | ||
|
||
cout << "The nodes of Linked List are: \n"; | ||
|
||
while (curr != NULL) | ||
{ | ||
|
||
cout << curr->data << " "; | ||
|
||
next = Xor(prev, curr->xnode); | ||
|
||
prev = curr; | ||
curr = next; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
Node *head = NULL; | ||
insert(&head, 10); | ||
insert(&head, 100); | ||
insert(&head, 1000); | ||
insert(&head, 10000); | ||
|
||
printList(head); | ||
|
||
return (0); | ||
} |