Skip to content

Commit

Permalink
Merge pull request AdarshAddee#57 from Shriansh2002/main
Browse files Browse the repository at this point in the history
Added xor Linked List and trie
  • Loading branch information
AdarshAddee committed Oct 1, 2022
2 parents 84c6528 + 22bd0ff commit af151d7
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
85 changes: 85 additions & 0 deletions C++/Trie.cpp
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;
}
68 changes: 68 additions & 0 deletions C++/XOR LL.cpp
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);
}

0 comments on commit af151d7

Please sign in to comment.