Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Create & Insert Duplicate Node
For a given a Binary Tree of type integer, duplicate every node of the tree and attach it to the left of itself.
The root will remain the same. So you just need to insert nodes in the given Binary Tree.
Example:
alt txt

After making the changes to the above-depicted tree, the updated tree will look like this.
alt txt

You can see that every node in the input tree has been duplicated and inserted to the left of itself.
Input format :
The first and the only line of input will contain the node data, all separated by a single space. Since -1 is used as an indication whether the left or right node data exist for root, it will not be a part of the node data.
Output Format :
The updated tree will be printed in a level order fashion where each level will be printed on a new line.
Elements on every level will be printed in a linear fashion. A single space will separate them.
Note:
You are not required to print anything explicitly. It has already been taken care of. Just implement the function to achieve the desired structure of the tree.
Hint:
First, store the left node. Next, insert a duplicate node to the left of the current node. Then, call the function for the stored left node, which will return a modified node. Attach this modified node to the left of the duplicate node. Finally, proceed to call the function for the right node of the root
Constraints :
1 <= N <= 10^5
Where N is the total number of nodes in the binary tree.

Time Limit: 1 sec
Sample Input 1:
10 20 30 40 50 -1 60 -1 -1 -1 -1 -1 -1
Sample Output 1:
10
10 30
20 30 60
20 50 60
40 50
40
Sample Input 2:
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
Sample Output 2:
8
8 10
5 10
5 6
2 6 7
2 7
*/
#include <iostream>
#include <queue>

template <typename T>
class BinaryTreeNode {
public:
T data;
BinaryTreeNode<T>* left;
BinaryTreeNode<T>* right;

BinaryTreeNode(T data) {
this->data = data;
left = NULL;
right = NULL;
}
};

using namespace std;
void insertDuplicateNode(BinaryTreeNode<int> *root) {
// Write your code here
if ( root == NULL) {
return;
}
BinaryTreeNode<int> * newnode =new BinaryTreeNode<int>(root->data);
newnode->left = root->left;
root->left = newnode;
insertDuplicateNode(newnode->left);
insertDuplicateNode(root->right);
return;
}

BinaryTreeNode<int>* takeInput() {
int rootData;
cin >> rootData;
if (rootData == -1) {
return NULL;
}
BinaryTreeNode<int>* root = new BinaryTreeNode<int>(rootData);
queue<BinaryTreeNode<int>*> q;
q.push(root);
while (!q.empty()) {
BinaryTreeNode<int>* currentNode = q.front();
q.pop();
int leftChild, rightChild;
cin >> leftChild;
if (leftChild != -1) {
BinaryTreeNode<int>* leftNode = new BinaryTreeNode<int>(leftChild);
currentNode->left = leftNode;
q.push(leftNode);
}
cin >> rightChild;
if (rightChild != -1) {
BinaryTreeNode<int>* rightNode =
new BinaryTreeNode<int>(rightChild);
currentNode->right = rightNode;
q.push(rightNode);
}
}
return root;
}

void printLevelATNewLine(BinaryTreeNode<int>* root) {
queue<BinaryTreeNode<int>*> q;
q.push(root);
q.push(NULL);
while (!q.empty()) {
BinaryTreeNode<int>* first = q.front();
q.pop();
if (first == NULL) {
if (q.empty()) {
break;
}
cout << endl;
q.push(NULL);
continue;
}
cout << first->data << " ";
if (first->left != NULL) {
q.push(first->left);
}
if (first->right != NULL) {
q.push(first->right);
}
}
}

int main() {
BinaryTreeNode<int>* root = takeInput();
insertDuplicateNode(root);
printLevelATNewLine(root);
delete root;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Code : Search in Tries

Implement the function Search for the Trie class.
For a Trie, write the function for searching a word. Return true if the word is found successfully, otherwise return false.
Note : main function is given for your reference which we are using internally to test the code.
*/

#include <iostream>
#include <string>
using namespace std;

class TrieNode {
public:
char data;
TrieNode **children;
bool isTerminal;

TrieNode(char data) {
this->data = data;
children = new TrieNode *[26];
for (int i = 0; i < 26; i++) {
children[i] = NULL;
}
isTerminal = false;
}
};

class Trie {
TrieNode *root;

public:
Trie() {
root = new TrieNode('\0');
}

void insertWord(TrieNode *root, string word) {
// Base case
if (word.size() == 0) {
root->isTerminal = true;
return;
}

// Small Calculation
int index = word[0] - 'a';
TrieNode *child;
if (root->children[index] != NULL) {
child = root->children[index];
} else {
child = new TrieNode(word[0]);
root->children[index] = child;
}

// Recursive call
insertWord(child, word.substr(1));
}

void insertWord(string word) {
insertWord(root, word);
}

bool search(TrieNode *root,string word){

if(word.size()==0){
return root->isTerminal==true;
}

int index=word[0] -'a';
TrieNode *child;
if(root->children[index]!=NULL){
child=root->children[index];
bool small=search(child,word.substr(1));
return small;
}
else
return false;
}

bool search(string word) {
// Write your code here
return search(root,word);

}
};

int main() {
int choice;
cin >> choice;
Trie t;

while (choice != -1) {
string word;
bool ans;
switch (choice) {
case 1: // insert
cin >> word;
t.insertWord(word);
break;
case 2: // search
cin >> word;
cout << (t.search(word) ? "true\n" : "false\n");
break;
default:
return 0;
}
cin >> choice;
}

return 0;
}