Skip to content

Commit e52a336

Browse files
committed
add
1 parent ba31621 commit e52a336

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

008 - Trees/Practice 01/TreeUses.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,92 @@ void postOrderTraversal(TreeNode* head){
147147
cout<<head->data<<" ";
148148
}
149149

150+
//Contains x in a tree or not
151+
bool containsX(TreeNode* root,int x){
152+
bool ans = false;
153+
for(int i=0;i<root->children.size();i++){
154+
bool temp = containsX(root->children[i],x);
155+
if(temp==true){
156+
ans=true;
157+
}
158+
}
159+
if(root->data==x){
160+
return true;
161+
}
162+
return ans;
163+
}
164+
165+
//Count Nodes Greater than X
166+
int nodesGreaterThanX(TreeNode* root,int k){
167+
int count=0;
168+
for(int i=0;i<root->children.size();i++){
169+
count+=nodesGreaterThanX(root->children[i],k);
170+
}
171+
if(root->data>k){
172+
count++;
173+
}
174+
return count;
175+
}
176+
177+
//Node with Maximum Child Sum
178+
TreeNode* nodewithMaxChild(TreeNode* root){
179+
int sum = root->data;
180+
TreeNode* yash=root;
181+
for(int i=0;i<root->children.size();i++){
182+
sum+=root->children[i]->data;
183+
}
184+
for(int i=0;i<root->children.size();i++){
185+
TreeNode* temp = nodewithMaxChild(root->children[i]);
186+
int xSum=temp->data;
187+
for(int j=0;j<temp->children.size();j++){
188+
xSum+=temp->children[j]->data;
189+
}
190+
if(xSum>sum){
191+
yash=temp;
192+
}
193+
}
194+
return yash;
195+
}
196+
197+
//Structure Identical
198+
bool structureIdentical(TreeNode* tree1,TreeNode* tree2){
199+
if(tree1->data!=tree2->data){
200+
return false;
201+
}
202+
if(tree1->children.size()!=tree2->children.size()){
203+
return false;
204+
}
205+
for(int i=0;i<tree1->children.size();i++){
206+
return structureIdentical(tree1->children[i],tree2->children[i]);
207+
}
208+
return true;
209+
}
210+
211+
//Next Larger
212+
TreeNode* nextLarger(TreeNode* root,int k){
213+
if(root==NULL){
214+
return root;
215+
}
216+
TreeNode* temp = NULL;
217+
if(k<root->data){
218+
temp=root;
219+
}
220+
for(int i=0;i<root->children.size();i++){
221+
TreeNode* shortAns = nextLarger(root->children[i],k);
222+
if(shortAns==NULL){
223+
continue;
224+
}else{
225+
if(temp==NULL){
226+
temp = shortAns;
227+
}else if(temp->data > shortAns->data && shortAns->data>k){
228+
temp = shortAns;
229+
}
230+
}
231+
}
232+
return temp;
233+
}
234+
235+
150236
int main(){
151237

152238
// 1 3 2 3 4 2 5 6 2 8 12 3 10 11 9 0 0 0 0 0 0 0
@@ -166,7 +252,16 @@ int main(){
166252
postOrderTraversal(root);
167253
cout<<endl;
168254
depthofNode(root,1);
255+
cout<<endl;
256+
cout<<"Contains X or Not: "<<containsX(root,3)<<endl;
257+
cout<<"Greater Than X: "<<nodesGreaterThanX(root,35)<<endl;
258+
TreeNode* temp = nodewithMaxChild(root);
259+
cout<<"Max Sum root data: "<<temp->data<<endl;
169260

261+
//TreeNode* root1 = takeInput();
262+
//cout<<"\nSame structure: "<<structureIdentical(root,root1)<<endl;
263+
cout<<"\nNext Larger: "<<endl;
264+
printTree(nextLarger(root,21));
170265

171266
//TreeNode* root = new TreeNode(10);
172267
//TreeNode* ch1 = new TreeNode(20);
1.9 MB
Binary file not shown.

009 - Binary Trees/Practice/practice.cpp

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class TrieNode{
2+
public:
3+
char data;
4+
TrieNode **children;
5+
bool isTerminal;
6+
TrieNode(char data){
7+
thos->data = data;
8+
children = new TrieNode*[26];
9+
isTerminal = false;
10+
}
11+
};

0 commit comments

Comments
 (0)