├── BinarySearchTrees ├── BSTClass.h ├── BSTClass_Use.cpp ├── BSTClass_Use.exe ├── BSTClass_Use.o ├── BTNodeClass.cpp ├── BinaryTreeClassUse_Basic.cpp ├── BinaryTreeClassUse_Basic.exe ├── BinaryTreeClassUse_Basic.o ├── BinaryTreeNodeClass_Use.cpp ├── InsertAndDelete_BST_Class.cpp ├── PathFinderInBinaryTree.h └── Practice │ ├── BTNode.h │ ├── BTNodeClassUse_basic.cpp │ ├── BTNodeClassUse_basic.exe │ └── BTNodeClassUse_basic.o ├── BinaryTrees ├── BTNodeClass.cpp ├── BinaryTreeNodeClass_Use.cpp ├── BinaryTreeNodeClass_Use.exe └── BinaryTreeNodeClass_Use.o ├── Dynamic Programming 2 ├── EditDistance_DP.h ├── LongestCommonSubsequence.cpp ├── LongestCommonSubsequence.exe └── LongestCommonSubsequence.o ├── DynamicProgramming1 ├── BalancedBT.cpp ├── BalancedBT.exe ├── BalancedBT.o ├── CountSteps(squareSum).cpp ├── CountSteps(squareSum).exe ├── CountSteps(squareSum).o ├── FibonacciDP.cpp ├── FibonacciDP.exe ├── FibonacciDP.o ├── MinStepsTo1UsingDP.cpp └── Revise │ ├── Fibonacci_DP.cpp │ ├── Fibonacci_DP.exe │ ├── Fibonacci_DP.o │ ├── minStepsto1.cpp │ ├── minStepsto1.exe │ └── minStepsto1.o ├── Extra Codes ├── DynamicClassUse.cpp ├── DynamicClassUse.exe ├── DynamicClassUse.o ├── Dynamic_array_class.cpp ├── Fraction_class.cpp ├── GCD.cpp ├── GCD.exe ├── GCD.o ├── Iterators.cpp ├── Iterators.exe ├── Iterators.o ├── Pattern_practice.cpp ├── Pattern_practice.exe ├── Pattern_practice.o ├── Read Me.txt ├── StudentClass.cpp ├── Using FractionClass.cpp ├── Using FractionClass.exe ├── Using FractionClass.o ├── Using_Student_class.cpp ├── Using_Student_class.exe ├── Using_Student_class.o ├── compress_the_string.cpp ├── compress_the_string.exe ├── compress_the_string.o ├── redundant_brackets_using_stacks.cpp ├── redundant_brackets_using_stacks.exe ├── redundant_brackets_using_stacks.o └── using_Static_Members.cpp ├── Graphs1 ├── BFS Graph Traversal Complete.cpp ├── DFS Graph Traversal.cpp ├── DijkstrasAlgorithm.cpp ├── Get Path DFS.cpp ├── Get Path DFS.exe ├── Get Path DFS.o ├── GetPath BFS.cpp ├── GetPath BFS.exe ├── GetPath BFS.o ├── Graphs_Adj_Matrix.cpp ├── Graphs_Adj_Matrix.exe ├── Graphs_Adj_Matrix.o ├── HasPath Problem.cpp ├── HasPath Problem.exe ├── HasPath Problem.o ├── KruskalsAlgorithm.cpp ├── MyGraph.c++ ├── MyGraph.cpp ├── MyGraph1.cpp └── PrimsAlgorithm.cpp ├── HashMaps Implementation ├── MapClass.h ├── MapClassUse.cpp ├── MapClassUse.exe └── MapClassUse.o ├── HashMaps ├── BSTClass.h ├── BTNodeClass.cpp ├── BinaryTreeClassUse_Basic.cpp ├── BinaryTreeClassUse_Basic.exe ├── BinaryTreeClassUse_Basic.o ├── BinaryTreeNodeClass_Use.cpp ├── HashMapClassUse_Trial.cpp ├── HashMapClassUse_Trial.exe ├── HashMapClassUse_Trial.o ├── HashMapsClass_TrialAndError.cpp ├── Temp_BinaryTreeRevision.cpp ├── Unordered_mapSTL.cpp ├── Unordered_mapSTL.exe └── Unordered_mapSTL.o ├── Linklists ├── Class_DoublyLL.cpp ├── Class_LL.cpp ├── DoublyList_Use.cpp ├── DoublyList_Use.exe ├── DoublyList_Use.o ├── LL101.cpp ├── LL101.exe ├── LL101.o ├── LL101.pp.c ├── LinkedList_revise1.cpp ├── LinkedList_revise1.exe ├── LinkedList_revise1.o ├── Node.cpp ├── Practice │ ├── LL_Node.cpp │ ├── LL_class_use.cpp │ ├── LL_class_use.exe │ ├── LL_class_use.o │ ├── temp.c │ ├── temp.exe │ └── temp.o ├── ReverseLL.cpp └── ReverseLL_rec.cpp ├── New Text Document.txt ├── Priority Queues ├── InBuilt_PriorityQueues.cpp ├── InBuilt_PriorityQueues.exe ├── InBuilt_PriorityQueues.o ├── PriorityQueueClass.cpp ├── PriorityQueueClassUse.cpp ├── PriorityQueueClassUse.exe └── PriorityQueueClassUse.o ├── Queues ├── Queue_Class_T.cpp ├── Queue_use.cpp ├── Queue_use.exe └── Queue_use.o ├── README.md ├── Stacks ├── 10PrefixEvaluationAndConversion.cpp ├── 11CelebrityProblem.cpp ├── 1DuplicateBrackets.cpp ├── 2BalancedBrackets.cpp ├── 3NextGreaterElementOnRight.cpp ├── 3aNextGreaterElementOnRight.cpp ├── 3bNextSmallestElementOnRight.cpp ├── 3cNextSmallestElementOnRight.cpp ├── 3dNextGreatestElementOnLeft.cpp ├── 3eNextGreaterElementOnLeft.cpp ├── 4StockSpan.cpp ├── 5AreaUnderHistogram.cpp ├── 6slidingWindowMaximum.cpp ├── 7InfixEvaluation.cpp ├── 8InfixConversion.cpp ├── 9PostfixConversionAndEvaluation.cpp ├── Class_Stacks_Arrays.cpp ├── Dynamic_Stack_Class.cpp ├── Dynamic_Stack_Class.o ├── StackClassOfArraysUse.cpp ├── StackClassOfArraysUse.exe ├── StackClassOfArraysUse.o ├── Stack_LL.h ├── Stack_LL_use.cpp ├── Stack_LL_use.exe └── Stack_LL_use.o ├── Templates ├── Class_Templates.cpp ├── Template_Class_Use.cpp ├── Template_Class_Use.exe └── Template_Class_Use.o ├── Trees ├── TreeNode.h ├── TreeNode1.cpp ├── TreeNode_Use.cpp ├── TreeNode_Use.exe └── TreeNode_Use.o └── Tries ├── Trie.h ├── TrieNodeClass.cpp └── TrieNodeClass.h /BinarySearchTrees/BSTClass.h: -------------------------------------------------------------------------------- 1 | #include"BTNodeClass.cpp" 2 | 3 | class BST{ 4 | public: 5 | BinaryTreeNode* root; 6 | int numberOfNodes; 7 | public: 8 | BST() 9 | { 10 | root=NULL; 11 | numberOfNodes=0; 12 | } 13 | private: 14 | 15 | bool hasDataHelper(int data,BinaryTreeNode*node){ 16 | if(node==NULL) return false; 17 | if(node->data==data) return true; 18 | else{ 19 | 20 | ///since this is a BST 21 | if(datadata) 22 | { 23 | return hasDataHelper(data,node->left); 24 | } 25 | else return hasDataHelper(data,node->right); 26 | } 27 | 28 | 29 | } 30 | public: 31 | 32 | bool hasData(int data){ 33 | return hasDataHelper(data,root); 34 | } 35 | 36 | private: 37 | void insertHelper(int data,BinaryTreeNode*&node){ 38 | ///cout<<"In insertHelper"<(data); 43 | 44 | return; 45 | } 46 | if(datadata&&node->left==NULL){ 47 | ///cout<<"I am in root->left"<Data== "<data<*n=new BinaryTreeNode(data); 49 | node->left=n; 50 | return; 51 | } 52 | else if(data>=node->data&&node->right==NULL){ 53 | 54 | BinaryTreeNode*n=new BinaryTreeNode(data); 55 | node->right=n; 56 | return; 57 | } 58 | else{ 59 | if(datadata) 60 | { 61 | insertHelper(data,node->left); return; 62 | } 63 | else{ 64 | insertHelper(data,node->right); return; 65 | } 66 | } 67 | 68 | } 69 | public: 70 | 71 | void Insert(int data){ 72 | ///if(!root) cout<<"Root is NULL"<data<<" : is roots data after insertion "<* root) 79 | { 80 | 81 | if(root==NULL) return; 82 | cout<data<<":"; 83 | if(root->left) 84 | cout<<"L"<left->data<<","; 85 | if(root->right) 86 | { 87 | cout<<"R"<right->data; 88 | 89 | } 90 | cout<left); 92 | printTreeProper(root->right); 93 | return; 94 | 95 | 96 | } 97 | public: 98 | 99 | void print() 100 | { 101 | printTreeProper(root); 102 | 103 | } 104 | private: 105 | BinaryTreeNode* minimum(BinaryTreeNode*root) 106 | { 107 | if(!root) return root; 108 | 109 | BinaryTreeNode*Lmin=minimum(root->left); 110 | BinaryTreeNode*Rmin=minimum(root->right); 111 | if(Lmin==NULL&&Rmin==NULL) return root; 112 | else if(Lmin==NULL){ 113 | if(root->datadata) return root; 114 | else return Rmin; 115 | } 116 | else if(Rmin==NULL){ 117 | if(root->datadata) return root; 118 | else return Lmin; 119 | } 120 | if(root->datadata&&root->datadata) return root; 121 | else if(Lmin->datadata){return Lmin;} 122 | else return Rmin; 123 | 124 | 125 | } 126 | /* BinaryTreeNode* deleteDataHelper(int data,BinaryTreeNode*root){ 127 | if(root==NULL) return root; 128 | if(root->data==data) 129 | { 130 | if(!(root->left||root->right)) 131 | { 132 | return NULL; 133 | 134 | } 135 | else if(!root->left&&root->right) 136 | { 137 | return root->right; 138 | 139 | } 140 | else if(!root->right&&root->left) 141 | { 142 | return root->left; 143 | } 144 | else{ 145 | BinaryTreeNode*node=minimum(root->right); 146 | root->data=node->data; 147 | node=deleteDataHelper(node->data,node); 148 | 149 | } 150 | return root; 151 | } 152 | else{ 153 | if(datadata) 154 | { 155 | root->left= deleteDataHelper(data,root->left); 156 | } 157 | else{ 158 | root->right=deleteDataHelper(data,root->right); 159 | } 160 | return root; 161 | } 162 | 163 | }*/ 164 | public: 165 | void deleteData(int data) 166 | { 167 | root=deleteDataHelper(root,data); 168 | 169 | } 170 | BinaryTreeNode*deleteDataHelper(BinaryTreeNode*root,int value){ 171 | if(root==NULL) return root; 172 | if(root->dataleft,value); 175 | } 176 | else if(root->data>value) 177 | { 178 | deleteDataHelper(root->right,value); 179 | } 180 | else{ 181 | if(root->left==NULL&&root->right==NULL) 182 | { 183 | delete root; 184 | return NULL; 185 | 186 | } 187 | else if(root->left==NULL) 188 | { 189 | BinaryTreeNode*temp=root->right; 190 | root->right=NULL; 191 | delete root; 192 | return temp; 193 | 194 | } 195 | else if(root->right==NULL) 196 | { 197 | BinaryTreeNode*temp=root->left; 198 | root->left=NULL; 199 | delete root; 200 | return temp; 201 | 202 | } 203 | else { 204 | BinaryTreeNode*temp=root->right; 205 | while(temp!=NULL&&temp->left!=NULL) 206 | { 207 | temp=temp->left; 208 | } 209 | root->data=temp->data; 210 | root->right=deleteDataHelper(root->right,value); 211 | return root; 212 | 213 | } 214 | 215 | 216 | } 217 | 218 | 219 | } 220 | }; 221 | -------------------------------------------------------------------------------- /BinarySearchTrees/BSTClass_Use.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include"BSTClass.h" 4 | #include 5 | class Triple 6 | { 7 | public: 8 | bool isBST; 9 | int Maximum; 10 | int Minimum; 11 | Triple() 12 | { 13 | isBST=true; 14 | Maximum=INT_MIN; 15 | Minimum=INT_MAX; 16 | 17 | } 18 | 19 | }; 20 | Triple isBSTmaxMin(BinaryTreeNode*root) 21 | { 22 | if(root==NULL){ 23 | Triple t; 24 | return t; 25 | } 26 | Triple t1,t2; 27 | t1=isBSTmaxMin(root->left); 28 | t2=isBSTmaxMin(root->right); 29 | if(!(t1.isBST&&t2.isBST)) 30 | { 31 | Triple t; 32 | t.isBST=false; 33 | t.Maximum=max(root->data,max(t1.Maximum,t2.Maximum)); 34 | t.Minimum=min(root->data,min(t1.Minimum,t2.Minimum)); 35 | return t; 36 | } 37 | else{ 38 | Triple t; 39 | t.Maximum=max(root->data,max(t1.Maximum,t2.Maximum)); 40 | t.Minimum=min(root->data,min(t1.Minimum,t2.Minimum)); 41 | if(root->data>t1.Maximum&&root->data<=t2.Minimum) 42 | { 43 | t.isBST=true; 44 | } 45 | else t.isBST=false; 46 | return t; 47 | } 48 | 49 | } 50 | bool isBST(BinaryTreeNode*root) 51 | { 52 | return isBSTmaxMin(root).isBST; 53 | 54 | } 55 | 56 | void makeBSThelper( BST &b, int *a,int s,int e) 57 | { 58 | if(s>e) 59 | { 60 | return; 61 | } 62 | 63 | int mid=(s+e)/2; 64 | b.Insert(a[mid]); 65 | makeBSThelper(b,a,s,mid-1); 66 | makeBSThelper(b,a,mid+1,e); 67 | return ; 68 | 69 | } 70 | 71 | BST makeBST_sortedArray(int *a,int n){ 72 | BST b; 73 | makeBSThelper(b,a,0,n-1); 74 | return b; 75 | } 76 | 77 | int main() 78 | { 79 | 80 | BST b; 81 | b.Insert(10); 82 | b.Insert(5); 83 | b.Insert(15); 84 | b.Insert(3); 85 | b.Insert(20); 86 | cout<<"Check if tree has 10 "< 2 | class BinaryTreeNode{ 3 | public: 4 | T data; 5 | BinaryTreeNode*left; 6 | BinaryTreeNode*right; 7 | 8 | BinaryTreeNode(T data) 9 | { 10 | this->data=data; 11 | left=NULL; 12 | right=NULL; 13 | } 14 | 15 | void printTree() 16 | { 17 | if(this==NULL) return; 18 | cout<printTree(); 20 | right->printTree(); 21 | return; 22 | 23 | } 24 | 25 | ~BinaryTreeNode() 26 | { 27 | delete left; 28 | delete right; 29 | } 30 | 31 | }; 32 | -------------------------------------------------------------------------------- /BinarySearchTrees/BinaryTreeClassUse_Basic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #include"BTNodeClass.cpp" 5 | #include 6 | 7 | void printTreeProper(BinaryTreeNode* root) 8 | { 9 | 10 | if(root==NULL) return; 11 | cout<data<<":"; 12 | if(root->left) 13 | cout<<"L"<left->data<<" "; 14 | if(root->right) 15 | { 16 | cout<<"R"<right->data; 17 | 18 | } 19 | cout<left); 21 | printTreeProper(root->right); 22 | return; 23 | 24 | } 25 | BinaryTreeNode* takeInputLevelWise() 26 | { 27 | BinaryTreeNode* root; 28 | BinaryTreeNode* leftChildNode; 29 | BinaryTreeNode* rightChildNode; 30 | int data; 31 | cout<<"Enter Root Data: "<>data; 32 | if(data==-1) return NULL; 33 | 34 | root=new BinaryTreeNode (data); 35 | 36 | queue *> q; 37 | q.push(root); 38 | while(q.size()!=0) 39 | { 40 | int child_left=-1,child_right=-1; 41 | BinaryTreeNode*temp=q.front(); 42 | q.pop(); 43 | cout<<"Enter left child of "<data<>child_left; 45 | cout<<"Enter right child of "<data<>child_right; 47 | if(child_left!=-1) 48 | { 49 | leftChildNode=new BinaryTreeNode (child_left); 50 | temp->left=leftChildNode; 51 | q.push(leftChildNode); 52 | 53 | } 54 | if(child_right!=-1) 55 | { 56 | rightChildNode=new BinaryTreeNode (child_right); 57 | temp->right=rightChildNode; 58 | q.push(rightChildNode); 59 | 60 | } 61 | } 62 | return root; 63 | 64 | } 65 | 66 | void printAtLevelK(BinaryTreeNode*root,int k) 67 | { 68 | if(root==NULL) return; 69 | if(k==0) {cout<data<left,k-1); 73 | printAtLevelK(root->right,k-1); 74 | return; 75 | } 76 | 77 | } 78 | class Pair{ 79 | public: 80 | 81 | bool isThere; 82 | int depth; 83 | 84 | Pair() 85 | { 86 | isThere=false; 87 | depth=0; 88 | } 89 | Pair(bool a,int d) 90 | { 91 | isThere=a; 92 | depth=d; 93 | } 94 | 95 | }; 96 | Pair isNodeThereAndDepth(BinaryTreeNode*root,int data) 97 | { 98 | if(root==NULL){ 99 | Pair p; 100 | return p; 101 | } 102 | 103 | if(root->data==data) 104 | { 105 | Pair p(true,1);return p; 106 | 107 | } 108 | Pair p1,p2; 109 | p1=isNodeThereAndDepth(root->left,data); 110 | p2=isNodeThereAndDepth(root->right,data); 111 | 112 | if(p1.isThere){ 113 | p1.depth+=1; 114 | return p1; 115 | } 116 | else if(p2.isThere) { 117 | p2.depth+=1; 118 | return p2;} 119 | else{ 120 | Pair p; 121 | return p; 122 | } 123 | 124 | } 125 | 126 | void nodesAtDistanceK(BinaryTreeNode *root, int node, int k) { 127 | // Write your code here 128 | cout<<" i come here"<data<<" value of k is : "<data==node) 132 | { 133 | printAtLevelK(root,k); 134 | } 135 | Pair p1,p2; 136 | 137 | p1=isNodeThereAndDepth(root->left,node); 138 | if(p1.isThere) 139 | { 140 | cout<<" p1 depth is : "<data<right,k-p1.depth-1);///check if it will really print at depth - k or any other higher or lower level 145 | 146 | } 147 | nodesAtDistanceK(root->left,node,k); 148 | } 149 | p2=isNodeThereAndDepth(root->right,node); 150 | if(p2.isThere) 151 | { 152 | if(p2.depth==k) cout<data<left,p2.depth-k);/class BST: 156 | def __init__(self): 157 | self.root=None 158 | 159 | def Insert(self,data): 160 | newNode=treeNode(data) 161 | if(self.root==None): 162 | self.root=newNode 163 | return 164 | 165 | elif self.root.data>data: 166 | self.Insert(data) 167 | //check if it will really print ar depth - k or any other higher or lower level 168 | 169 | } 170 | nodesAtDistanceK(root->right,node,k); 171 | } 172 | return; 173 | } 174 | ///1 2 3 4 5 -1 -1 6 7 -1 -1 -1 -1 -1 -1class BST: 175 | def __init__(self): 176 | self.root=None 177 | 178 | def Insert(self,data): 179 | newNode=treeNode(data) 180 | if(self.root==None): 181 | self.root=newNode 182 | return 183 | 184 | elif self.root.data>data: 185 | self.Insert(data) 186 | 187 | ///5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1 188 | int main() 189 | { 190 | BinaryTreeNode*root; 191 | root=takeInputLevelWise(); 192 | cout< 2 | #include 3 | using namespace std; 4 | #include"BTNodeClass.cpp" 5 | #include 6 | void print(int *a,int s,int e) 7 | { 8 | for(int i=s;i<=e;i++) 9 | { 10 | cout<* root) 15 | { 16 | if(root==NULL) return ; 17 | cout<data<<" "; 18 | printTree(root->left); 19 | printTree(root->right); 20 | 21 | } 22 | void printTreeProper(BinaryTreeNode* root) 23 | { 24 | 25 | if(root==NULL) return; 26 | cout<data<<":"; 27 | if(root->left) 28 | cout<<"L"<left->data<<" "; 29 | if(root->right) 30 | { 31 | cout<<"R"<right->data; 32 | 33 | } 34 | cout<left); 36 | printTreeProper(root->right); 37 | return; 38 | 39 | 40 | } 41 | BinaryTreeNode* takeInput() 42 | { 43 | int data; 44 | cout<<"Enter Data "<>data; 46 | if(data==-1) return NULL; 47 | 48 | BinaryTreeNode*root=new BinaryTreeNode(data); 49 | 50 | root->left=takeInput(); 51 | root->right=takeInput(); 52 | return root; 53 | 54 | 55 | }/// simple input 1 2 -1 4 -1 -1 3 5 -1 -1 6 -1 -1 56 | BinaryTreeNode* takeInputLevelWise() 57 | { 58 | BinaryTreeNode* root; 59 | BinaryTreeNode* leftChildNode; 60 | BinaryTreeNode* rightChildNode; 61 | int data; 62 | cout<<"Enter Root Data: "<>data; 63 | if(data==-1) return NULL; 64 | 65 | root=new BinaryTreeNode (data); 66 | 67 | queue *> q; 68 | q.push(root); 69 | while(q.size()!=0) 70 | { 71 | int child_left=-1,child_right=-1; 72 | BinaryTreeNode*temp=q.front(); 73 | q.pop(); 74 | cout<<"Enter left child of "<data<>child_left; 76 | cout<<"Enter right child of "<data<>child_right; 78 | if(child_left!=-1) 79 | { 80 | leftChildNode=new BinaryTreeNode (child_left); 81 | temp->left=leftChildNode; 82 | q.push(leftChildNode); 83 | 84 | } 85 | if(child_right!=-1) 86 | { 87 | rightChildNode=new BinaryTreeNode (child_right); 88 | temp->right=rightChildNode; 89 | q.push(rightChildNode); 90 | 91 | } 92 | } 93 | return root; 94 | 95 | } 96 | 97 | void InOrder(BinaryTreeNode*root) 98 | { 99 | if(root==NULL) return; 100 | InOrder(root->left); 101 | cout<data<<" "; 102 | InOrder(root->right); 103 | return; 104 | } 105 | void postOrder(BinaryTreeNode*root) 106 | { 107 | if(root==NULL) return; 108 | postOrder(root->left); 109 | postOrder(root->right); 110 | cout<data<<" "; 111 | return; 112 | } 113 | void preOrder(BinaryTreeNode*root) 114 | { 115 | if(root==NULL) return; 116 | cout<data<<" "; 117 | preOrder(root->left); 118 | preOrder(root->right); 119 | return; 120 | } 121 | 122 | /// get Tree works fine:) 123 | BinaryTreeNode* getTree(int *pre,int *in,int preS,int preE,int inS,int inE) 124 | { 125 | ///int size=ei_pre-si_pre+1; 126 | if(inS>inE) return NULL; 127 | 128 | int rootIndex=-1; 129 | for(int i=inS;i<=inE;i++) 130 | { 131 | if(in[i]==pre[preS]) 132 | { 133 | rootIndex=i; 134 | break; 135 | } 136 | 137 | } 138 | int rootData=pre[preS]; 139 | int lPreS=preS+1; 140 | int lInS=inS; 141 | int lInE=rootIndex-1; 142 | int lPreE=lInE-lInS+lPreS; 143 | int rPreS=lPreE+1; 144 | int rPreE=preE; 145 | int rInS=rootIndex+1; 146 | int rInE=inE; 147 | 148 | BinaryTreeNode* root=new BinaryTreeNode (rootData); 149 | root->left=getTree(pre,in,lPreS,lPreE,lInS,lInE); 150 | 151 | root->right=getTree(pre,in,rPreS,rPreE,rInS,rInE); 152 | 153 | return root; 154 | 155 | 156 | } 157 | 158 | BinaryTreeNode* buildTree(int *preorder, int preLength, int *inorder, int inLength) { 159 | // Write your code here 160 | int s1,e1,s2,e2; 161 | s1=0;s2=0;e1=preLength-1;e2=inLength-1; 162 | /// cout<<"I am in First function: "<* getTree1(int *post,int *in,int postS,int postE,int inS,int inE) 168 | { 169 | if(inS>inE) return NULL; 170 | 171 | int rootIndex=-1; 172 | for(int i=inS;i<=inE;i++) 173 | { 174 | if(in[i]==post[postE]) 175 | { 176 | rootIndex=i; 177 | break; 178 | } 179 | 180 | } 181 | int rootData=post[postE]; 182 | 183 | int lInS=inS; 184 | int lInE=rootIndex-1; 185 | int lPostS=postS; 186 | int lPostE=lInE-lInS+lPostS; 187 | int rInS=rootIndex+1; 188 | int rInE=inE; 189 | int rPostS=lPostE+1; 190 | int rPostE=postE-1; 191 | BinaryTreeNode* root=new BinaryTreeNode (rootData); 192 | root->left=getTree(post,in,lPostS,lPostE,lInS,lInE); 193 | 194 | root->right=getTree(post,in,rPostS,rPostE,rInS,rInE); 195 | 196 | return root; 197 | 198 | 199 | } 200 | 201 | BinaryTreeNode* getTreeFromPostorderAndInorder(int *postorder, int postLength, int *inorder, int inLength) { 202 | // Write your code here 203 | int len=inLength-1; 204 | return getTree1(postorder,inorder,0,len,0,len); 205 | 206 | } 207 | 208 | int height(BinaryTreeNode*root) 209 | { 210 | if(root==NULL) return 0; 211 | return 1+max(height(root->left),height(root->right)); 212 | 213 | } 214 | 215 | int diameter(BinaryTreeNode* root) 216 | { 217 | if(root==NULL) return 0; 218 | int h1=height(root->left); 219 | int h2=height(root->right); 220 | /// cout<<"H1 and H2 are : "<left); 222 | int d2=diameter(root->right); 223 | return max(max(h1+h2,d1),d2); 224 | 225 | } 226 | 227 | ///better approach for diameter 13/8/17 228 | 229 | class Pair{ 230 | public: 231 | int height; 232 | int diameter; 233 | Pair() 234 | { 235 | height=diameter=0; 236 | 237 | } 238 | Pair(int h,int d) 239 | { 240 | height=h; 241 | diameter=d; 242 | 243 | } 244 | }; 245 | 246 | Pair heightAndDiameter(BinaryTreeNode*root) 247 | { 248 | Pair HandD; 249 | if(root==NULL) return HandD; 250 | Pair p1=heightAndDiameter(root->left); 251 | Pair p2=heightAndDiameter(root->right); 252 | 253 | int height_leftTree=p1.height; 254 | int height_rightTree=p2.height; 255 | int diameter_leftTree=p1.diameter; 256 | int diameter_rightTree=p2.diameter; 257 | 258 | int height=1+max(height_leftTree,height_rightTree); 259 | int diameter=max(max(height_leftTree+height_rightTree,diameter_leftTree),diameter_rightTree); 260 | 261 | Pair P(height,diameter); 262 | return P; 263 | 264 | } 265 | int diameterAdvanced(BinaryTreeNode*root) 266 | { 267 | return heightAndDiameter(root).diameter; 268 | 269 | } 270 | 271 | 272 | vector findPath(BinaryTreeNode* root,int x) 273 | { 274 | if(! root) { 275 | vector v; 276 | return v; 277 | } 278 | if(root->data==x) 279 | { 280 | 281 | vector v; 282 | v.push_back(x); 283 | return v; 284 | 285 | } 286 | vector vLeft=findPath(root->left,x); 287 | if(vLeft.size()) 288 | { 289 | vLeft.push_back(root->data); 290 | return vLeft; 291 | 292 | } 293 | vector vRight=findPath(root->right,x); 294 | if(vRight.size()) 295 | { 296 | 297 | vRight.push_back(root->data); 298 | return vRight; 299 | } 300 | else return vRight; 301 | } 302 | 303 | int main() 304 | { 305 | BinaryTreeNode*root=new BinaryTreeNode(10); 306 | BinaryTreeNode*node1=new BinaryTreeNode(20); 307 | BinaryTreeNode*node2=new BinaryTreeNode(30); 308 | root->left=node1; 309 | root->right=node2; 310 | node2=new BinaryTreeNode(40); 311 | root->left->right=node2; 312 | /* cout<<"The class print function: "<printTree(); 314 | cout<>n; 331 | size1=size2=n; 332 | for(int i=0;i>a1[i]; 336 | } 337 | for(int i=0;i>a2[i]; 340 | } 341 | 342 | root=buildTree(a1,size1,a2,size2); 343 | printTreeProper(root); 344 | */ 345 | root=takeInputLevelWise(); 346 | printTreeProper(root); 347 | /// postOrder(root);cout< v; 353 | cout<<"I am now to find path "<>n; 357 | v=findPath(root,n); 358 | ///1 2 3 4 5 -1 7 -1 -1 6 -1 -1 -1 -1 -1 359 | for(int i=0;i*root; 8 | public: 9 | BST() 10 | { 11 | root=NULL; 12 | 13 | } 14 | ~BST() 15 | { 16 | delete root; 17 | } 18 | 19 | private: 20 | bool hasDataHelper(int data,BinaryTreeNode*node){ 21 | if(node==NULL) return false; 22 | if(node->data==data) return true; 23 | else{ 24 | 25 | ///since this is a BST 26 | if(datadata) 27 | { 28 | return hasDataHelper(data,node->left); 29 | } 30 | else return hasDataHelper(data,node->right); 31 | } 32 | 33 | 34 | } 35 | public: 36 | bool hasData(int data){ 37 | return hasDataHelper(data,root); 38 | } 39 | 40 | private: 41 | void insertHelper(int data,BinaryTreeNode*&node){ 42 | ///cout<<"In insertHelper"<(data); 47 | 48 | return; 49 | } 50 | if(datadata&&node->left==NULL){ 51 | ///cout<<"I am in root->left"<Data== "<data<*n=new BinaryTreeNode(data); 53 | node->left=n; 54 | return; 55 | } 56 | else if(data>=node->data&&node->right==NULL){ 57 | 58 | BinaryTreeNode*n=new BinaryTreeNode(data); 59 | node->right=n; 60 | return; 61 | } 62 | else{ 63 | if(datadata) 64 | { 65 | insertHelper(data,node->left); return; 66 | } 67 | else{ 68 | insertHelper(data,node->right); return; 69 | } 70 | } 71 | 72 | } 73 | public: 74 | void insert(int data){ 75 | ///if(!root) cout<<"Root is NULL"<data<<" : is roots data after insertion "<* root) 82 | { 83 | if(root==NULL) return; 84 | cout<data<<":"; 85 | if(root->left) 86 | cout<<"L:"<left->data<<","; 87 | if(root->right) 88 | { 89 | cout<<"R:"<right->data; 90 | 91 | } 92 | cout<left); 94 | printTreeProper(root->right); 95 | return; 96 | } 97 | public: 98 | void printTree(){ 99 | printTreeProper(root); 100 | 101 | } 102 | 103 | /*BinaryTreeNode* minimum(BinaryTreeNode*root) 104 | { 105 | if(!root) return root; 106 | 107 | BinaryTreeNode*Lmin=minimum(root->left); 108 | BinaryTreeNode*Rmin=minimum(root->right); 109 | if(Lmin==NULL&&Rmin==NULL) return root; 110 | else if(Lmin==NULL){ 111 | if(root->datadata) return root; 112 | else return Rmin; 113 | } 114 | else if(Rmin==NULL){ 115 | if(root->datadata) return root; 116 | else return Lmin; 117 | } 118 | if(root->datadata&&root->datadata) return root; 119 | else if(Lmin->datadata){return Lmin;} 120 | else return Rmin; 121 | 122 | 123 | } 124 | BinaryTreeNode* deleteDataHelper(int data,BinaryTreeNode*node){ 125 | if(node==NULL) return node; 126 | if(node->data==data) 127 | { 128 | if(!(node->left||node->right)) 129 | { 130 | delete node; 131 | return NULL; 132 | 133 | } 134 | else if(!node->left&&node->right) 135 | { 136 | 137 | return node->right; 138 | 139 | } 140 | else if(!node->right&&node->left) 141 | { 142 | return node->left; 143 | } 144 | else{ 145 | BinaryTreeNode*nodeR=minimum(node->right); 146 | node->data=nodeR->data; 147 | nodeR->right=deleteDataHelper(nodeR->data,nodeR->right); 148 | return node; 149 | } 150 | 151 | } 152 | else{ 153 | if(datadata) 154 | { 155 | node->left= deleteDataHelper(data,node->left); 156 | } 157 | else{ 158 | node->right=deleteDataHelper(data,node->right); 159 | } 160 | return node; 161 | } 162 | 163 | } 164 | */ 165 | 166 | BinaryTreeNode*deleteData(int data,BinaryTreeNode*node) 167 | { 168 | if(node==NULL) return NULL; 169 | if(datadata) 170 | { 171 | /// cout<<" i also reach here "<left=deleteData(data,node->left); 174 | return node; 175 | } 176 | else if(data>node->data) 177 | { 178 | node->right=deleteData(data,node->right); 179 | return node; 180 | } 181 | else{ 182 | /// cout<<"I come at this point, here temps data is data to be deleted : "<*temp=NULL; 184 | if(root->left==NULL&&root->right==NULL) 185 | { 186 | delete node; 187 | return NULL; 188 | } 189 | else if(node->left&&!node->right) 190 | { 191 | temp=node->left; 192 | node->left=NULL; 193 | delete node; 194 | return temp; 195 | } 196 | else if(!node->left&&node->right){ 197 | temp=node->right; 198 | node->right=NULL; 199 | delete node; 200 | return temp; 201 | } 202 | else { 203 | BinaryTreeNode*temp=NULL; 204 | ///cout<<"I come here "<right; 206 | while(temp->left!=NULL) 207 | { 208 | temp=temp->left; 209 | } 210 | int rightMin=temp->data; 211 | ///temp now points at minimum of these nodes 212 | node->data=rightMin; 213 | 214 | node->right=deleteData(rightMin,node->right); 215 | return node; 216 | } 217 | 218 | } 219 | } 220 | 221 | public: 222 | void deleteData(int data) 223 | { 224 | root=deleteData(data,root); 225 | 226 | } 227 | 228 | }; 229 | 230 | 231 | -------------------------------------------------------------------------------- /BinarySearchTrees/PathFinderInBinaryTree.h: -------------------------------------------------------------------------------- 1 | #define FOR(a,b,c) for (int (a)=(b); (a)<(c); (a)++) 2 | #define FORN(a,b,c) for (int (a)=(b); (a)<=(c); (a)++) 3 | #define FORD(a,b,c) for (int (a)=(b); (a)>=(c); (a)--) 4 | #define REP(i,n) FOR(i,0,n) 5 | #define REPN(i,n) FORN(i,1,n) 6 | #define REPD(i,n) FORD(i,n,1) 7 | 8 | void print(vector *a,int Size) 9 | { 10 | FOR(i,0,Size) cout< 14 | bool hasPath(BinaryTreeNode *root, vector& arr, int x) 15 | { 16 | // if root is NULL 17 | // there is no path 18 | if (!root) 19 | return false; 20 | 21 | // push the node's value in 'arr' 22 | arr.push_back(root->data); 23 | 24 | // if it is the required node 25 | // return true 26 | if (root->data == x) 27 | return true; 28 | 29 | // else check whether the required node lies 30 | // in the left subtree or right subtree of 31 | // the current node 32 | if (hasPath(root->left, arr, x) || hasPath(root->right, arr, x)) 33 | return true; 34 | 35 | // required node does not lie either in the 36 | // left or right subtree of the current node 37 | // Thus, remove current node's value from 38 | // 'arr'and then return false 39 | arr.pop_back(); 40 | return false; 41 | } 42 | 43 | int lcaBinaryTree(BinaryTreeNode*root,int val1,int val2) 44 | { 45 | vector v1,v2; 46 | bool isPathThere1=hasPath(root,v1,val1); 47 | bool isPathThere2=hasPath(root,v2,val2); 48 | 49 | if(isPathThere1==false&&isPathThere2==false) return -1; 50 | else if(isPathThere1==false) return val2; 51 | else if(isPathThere2==false) return val1; 52 | else{ 53 | int flag=0; 54 | int returnVal=-1; 55 | for(int i=0;i 3 | class BinaryTreeNode{ 4 | public: 5 | T data; 6 | BinaryTreeNode*left; 7 | BinaryTreeNode*right; 8 | 9 | BinaryTreeNode(T data) 10 | { 11 | this->data=data; 12 | left=NULL; 13 | right=NULL; 14 | } 15 | 16 | /* void printTree() 17 | { 18 | if(this==NULL) return; 19 | cout<printTree(); 21 | right->printTree(); 22 | return; 23 | 24 | } 25 | */ 26 | ~BinaryTreeNode() 27 | { 28 | delete left; 29 | delete right; 30 | } 31 | 32 | }; 33 | -------------------------------------------------------------------------------- /BinarySearchTrees/Practice/BTNodeClassUse_basic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"BTNode.h" 3 | using namespace std; 4 | #include 5 | #include 6 | class Triple{ 7 | public: 8 | 9 | int Max; 10 | int Min; 11 | bool isBST; 12 | Triple() 13 | { 14 | Max=INT_MIN; 15 | Min=INT_MAX; 16 | isBST=true; 17 | } 18 | 19 | }; 20 | Triple isBSTMaxMin(BinaryTreeNode*); 21 | 22 | int main() 23 | { 24 | 25 | return 0; 26 | } 27 | 28 | Triple isBSTMaxMin(BinaryTreeNode*root) 29 | { 30 | if(root==NULL) 31 | { 32 | Triple p; 33 | return p; 34 | } 35 | Triple tLeft,tRight; 36 | tLeft=isBSTMaxMin(root->left); 37 | tRight=isBSTMaxMin(root->right); 38 | bool isTrue; 39 | if(tLeft.Maxdata&&tRight.Min>=root->data) 40 | { 41 | isTrue=true; 42 | 43 | } 44 | else isTrue=false; 45 | 46 | if(isTrue&&tLeft.isBST&&tRight.isBST) 47 | { 48 | 49 | Triple t; 50 | t.isBST=true; 51 | t.Min=min(root->data,min(tLeft.Min,tRight.Min)); 52 | t.Max=max(root->data,max(tLeft.Max,tRight.Max)); 53 | return t; 54 | 55 | } 56 | else 57 | { 58 | Triple t; 59 | t.isBST=false; 60 | t.Min=min(root->data,min(tLeft.Min,tRight.Min)); 61 | t.Max=max(root->data,max(tLeft.Max,tRight.Max)); 62 | return t; 63 | 64 | } 65 | 66 | 67 | 68 | } 69 | bool isBST(BinaryTreeNode*root) 70 | { 71 | return isBSTMaxMin(root).isBST; 72 | } 73 | 74 | bool Search(BinaryTreeNode*root,int x) 75 | { 76 | if(root==NULL) return false; 77 | if(root->data==x) return true; 78 | if(xdata) 79 | { 80 | return Search(root->left,x); 81 | 82 | } 83 | else 84 | { 85 | return Search(root->right,x); 86 | 87 | } 88 | } 89 | 90 | vector findPath(BinaryTreeNode*root,int x){ 91 | if(Search(root,x)==false||root==NULL) 92 | { 93 | vector v; 94 | return v; 95 | } 96 | if(root->data==x) 97 | { 98 | vector v; 99 | v.push_back(root->data); 100 | return v; 101 | 102 | } 103 | vector v1,v2; 104 | if(xdata) 105 | { 106 | v1=findPath(root->left,x); 107 | if(v1.size()) 108 | { 109 | v1.push_back(root->data); 110 | 111 | 112 | } 113 | return v1; 114 | } 115 | else{ 116 | v2=findPath(root->right,x); 117 | if(v2.size()) 118 | { 119 | v2.push_back(root->data); 120 | 121 | 122 | } 123 | return v2; 124 | 125 | 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /BinarySearchTrees/Practice/BTNodeClassUse_basic.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/BinarySearchTrees/Practice/BTNodeClassUse_basic.exe -------------------------------------------------------------------------------- /BinarySearchTrees/Practice/BTNodeClassUse_basic.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/BinarySearchTrees/Practice/BTNodeClassUse_basic.o -------------------------------------------------------------------------------- /BinaryTrees/BTNodeClass.cpp: -------------------------------------------------------------------------------- 1 | template 2 | class BinaryTreeNode{ 3 | public: 4 | T data; 5 | BinaryTreeNode*left; 6 | BinaryTreeNode*right; 7 | 8 | BinaryTreeNode(T data) 9 | { 10 | this->data=data; 11 | left=NULL; 12 | right=NULL; 13 | } 14 | 15 | void printTree() 16 | { 17 | if(this==NULL) return; 18 | cout<printTree(); 20 | right->printTree(); 21 | return; 22 | 23 | } 24 | 25 | ~BinaryTreeNode() 26 | { 27 | 28 | delete left; 29 | delete right; 30 | } 31 | 32 | }; 33 | -------------------------------------------------------------------------------- /BinaryTrees/BinaryTreeNodeClass_Use.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #include"BTNodeClass.cpp" 5 | #include 6 | void print(int *a,int s,int e) 7 | { 8 | for(int i=s;i<=e;i++) 9 | { 10 | cout<* root) 15 | { 16 | if(root==NULL) return ; 17 | cout<data<<" "; 18 | printTree(root->left); 19 | printTree(root->right); 20 | 21 | } 22 | void printTreeProper(BinaryTreeNode* root) 23 | { 24 | 25 | if(root==NULL) return; 26 | cout<data<<":"; 27 | if(root->left) 28 | cout<<"L"<left->data<<" "; 29 | if(root->right) 30 | { 31 | cout<<"R"<right->data; 32 | 33 | } 34 | cout<left); 36 | printTreeProper(root->right); 37 | return; 38 | 39 | 40 | } 41 | BinaryTreeNode* takeInput() 42 | { 43 | int data; 44 | cout<<"Enter Data "<>data; 46 | if(data==-1) return NULL; 47 | 48 | BinaryTreeNode*root=new BinaryTreeNode(data); 49 | 50 | root->left=takeInput(); 51 | root->right=takeInput(); 52 | return root; 53 | 54 | 55 | }/// simple input 1 2 -1 4 -1 -1 3 5 -1 -1 6 -1 -1 56 | BinaryTreeNode* takeInputLevelWise() 57 | { 58 | BinaryTreeNode* root; 59 | BinaryTreeNode* leftChildNode; 60 | BinaryTreeNode* rightChildNode; 61 | int data; 62 | cout<<"Enter Root Data: "<>data; 63 | if(data==-1) return NULL; 64 | 65 | root=new BinaryTreeNode (data); 66 | 67 | queue *> q; 68 | q.push(root); 69 | while(q.size()!=0) 70 | { 71 | int child_left=-1,child_right=-1; 72 | BinaryTreeNode*temp=q.front(); 73 | q.pop(); 74 | cout<<"Enter left child of "<data<>child_left; 76 | cout<<"Enter right child of "<data<>child_right; 78 | if(child_left!=-1) 79 | { 80 | leftChildNode=new BinaryTreeNode (child_left); 81 | temp->left=leftChildNode; 82 | q.push(leftChildNode); 83 | 84 | } 85 | if(child_right!=-1) 86 | { 87 | rightChildNode=new BinaryTreeNode (child_right); 88 | temp->right=rightChildNode; 89 | q.push(rightChildNode); 90 | 91 | } 92 | } 93 | return root; 94 | 95 | } 96 | 97 | void InOrder(BinaryTreeNode*root) 98 | { 99 | if(root==NULL) return; 100 | InOrder(root->left); 101 | cout<data<<" "; 102 | InOrder(root->right); 103 | return; 104 | } 105 | void postOrder(BinaryTreeNode*root) 106 | { 107 | if(root==NULL) return; 108 | postOrder(root->left); 109 | postOrder(root->right); 110 | cout<data<<" "; 111 | return; 112 | } 113 | void preOrder(BinaryTreeNode*root) 114 | { 115 | if(root==NULL) return; 116 | cout<data<<" "; 117 | preOrder(root->left); 118 | preOrder(root->right); 119 | return; 120 | } 121 | 122 | /// get Tree works fine:) 123 | BinaryTreeNode* getTree(int *pre,int *in,int preS,int preE,int inS,int inE) 124 | { 125 | ///int size=ei_pre-si_pre+1; 126 | if(inS>inE) return NULL; 127 | 128 | int rootIndex=-1; 129 | for(int i=inS;i<=inE;i++) 130 | { 131 | if(in[i]==pre[preS]) 132 | { 133 | rootIndex=i; 134 | break; 135 | } 136 | 137 | } 138 | int rootData=pre[preS]; 139 | int lPreS=preS+1; 140 | int lInS=inS; 141 | int lInE=rootIndex-1; 142 | int lPreE=lInE-lInS+lPreS; 143 | int rPreS=lPreE+1; 144 | int rPreE=preE; 145 | int rInS=rootIndex+1; 146 | int rInE=inE; 147 | 148 | BinaryTreeNode* root=new BinaryTreeNode (rootData); 149 | root->left=getTree(pre,in,lPreS,lPreE,lInS,lInE); 150 | 151 | root->right=getTree(pre,in,rPreS,rPreE,rInS,rInE); 152 | 153 | return root; 154 | 155 | 156 | } 157 | 158 | BinaryTreeNode* buildTree(int *preorder, int preLength, int *inorder, int inLength) { 159 | // Write your code here 160 | int s1,e1,s2,e2; 161 | s1=0;s2=0;e1=preLength-1;e2=inLength-1; 162 | /// cout<<"I am in First function: "<* getTree1(int *post,int *in,int postS,int postE,int inS,int inE) 168 | { 169 | if(inS>inE) return NULL; 170 | 171 | int rootIndex=-1; 172 | for(int i=inS;i<=inE;i++) 173 | { 174 | if(in[i]==post[postE]) 175 | { 176 | rootIndex=i; 177 | break; 178 | } 179 | 180 | } 181 | int rootData=post[postE]; 182 | 183 | int lInS=inS; 184 | int lInE=rootIndex-1; 185 | int lPostS=postS; 186 | int lPostE=lInE-lInS+lPostS; 187 | int rInS=rootIndex+1; 188 | int rInE=inE; 189 | int rPostS=lPostE+1; 190 | int rPostE=postE-1; 191 | BinaryTreeNode* root=new BinaryTreeNode (rootData); 192 | root->left=getTree(post,in,lPostS,lPostE,lInS,lInE); 193 | 194 | root->right=getTree(post,in,rPostS,rPostE,rInS,rInE); 195 | 196 | return root; 197 | 198 | 199 | } 200 | 201 | BinaryTreeNode* getTreeFromPostorderAndInorder(int *postorder, int postLength, int *inorder, int inLength) { 202 | // Write your code here 203 | int len=inLength-1; 204 | return getTree1(postorder,inorder,0,len,0,len); 205 | 206 | } 207 | 208 | int height(BinaryTreeNode*root) 209 | { 210 | if(root==NULL) return 0; 211 | return 1+max(height(root->left),height(root->right)); 212 | 213 | } 214 | 215 | int diameter(BinaryTreeNode* root) 216 | { 217 | if(root==NULL) return 0; 218 | int h1=height(root->left); 219 | int h2=height(root->right); 220 | /// cout<<"H1 and H2 are : "<left); 222 | int d2=diameter(root->right); 223 | return max(max(h1+h2,d1),d2); 224 | 225 | } 226 | 227 | ///better approach for diameter 13/8/17 228 | 229 | class Pair{ 230 | public: 231 | int height; 232 | int diameter; 233 | Pair() 234 | { 235 | height=diameter=0; 236 | 237 | } 238 | Pair(int h,int d) 239 | { 240 | height=h; 241 | diameter=d; 242 | 243 | } 244 | }; 245 | 246 | Pair heightAndDiameter(BinaryTreeNode*root) 247 | { 248 | Pair HandD; 249 | if(root==NULL) return HandD; 250 | Pair p1=heightAndDiameter(root->left); 251 | Pair p2=heightAndDiameter(root->right); 252 | 253 | int height_leftTree=p1.height; 254 | int height_rightTree=p2.height; 255 | int diameter_leftTree=p1.diameter; 256 | int diameter_rightTree=p2.diameter; 257 | 258 | int height=1+max(height_leftTree,height_rightTree); 259 | int diameter=max(max(height_leftTree+height_rightTree,diameter_leftTree),diameter_rightTree); 260 | 261 | Pair P(height,diameter); 262 | return P; 263 | 264 | } 265 | int diameterAdvanced(BinaryTreeNode*root) 266 | { 267 | return heightAndDiameter(root).diameter; 268 | 269 | } 270 | 271 | 272 | vector findPath(BinaryTreeNode* root,int x) 273 | { 274 | if(! root) { 275 | vector v; 276 | return v; 277 | } 278 | if(root->data==x) 279 | { 280 | 281 | vector v; 282 | v.push_back(x); 283 | return v; 284 | 285 | } 286 | vector vLeft=findPath(root->left,x); 287 | if(vLeft.size()) 288 | { 289 | vLeft.push_back(root->data); 290 | return vLeft; 291 | 292 | } 293 | vector vRight=findPath(root->right,x); 294 | if(vRight.size()) 295 | { 296 | 297 | vRight.push_back(root->data); 298 | return vRight; 299 | } 300 | else return vRight; 301 | } 302 | 303 | int main() 304 | { 305 | BinaryTreeNode*root=new BinaryTreeNode(10); 306 | BinaryTreeNode*node1=new BinaryTreeNode(20); 307 | BinaryTreeNode*node2=new BinaryTreeNode(30); 308 | root->left=node1; 309 | root->right=node2; 310 | node2=new BinaryTreeNode(40); 311 | root->left->right=node2; 312 | /* cout<<"The class print function: "<printTree(); 314 | cout<>n; 331 | size1=size2=n; 332 | for(int i=0;i>a1[i]; 336 | } 337 | for(int i=0;i>a2[i]; 340 | } 341 | 342 | root=buildTree(a1,size1,a2,size2); 343 | printTreeProper(root); 344 | */ 345 | root=takeInputLevelWise(); 346 | printTreeProper(root); 347 | /// postOrder(root);cout< v; 353 | cout<<"I am now to find path "<>n; 357 | v=findPath(root,n); 358 | ///1 2 3 4 5 -1 7 -1 -1 6 -1 -1 -1 -1 -1 359 | for(int i=0;i 2 | using namespace std; 3 | #include 4 | 5 | int editDistance(string s,string t) 6 | { 7 | ///create a dynamic array 8 | int sizeY=s.size()+1; 9 | int sizeX=t.size()+1; 10 | int m=sizeY; 11 | int n=sizeX; 12 | /// initializing a 2D array 13 | int **ans = new int*[sizeY]; 14 | for(int i = 0; i < sizeY; ++i) { 15 | ans[i] = new int[sizeX]; 16 | } 17 | 18 | for(int i=0;i 2 | using namespace std; 3 | #include 4 | #include 5 | int lcs_Better(string ,string, int *memo[1000]); 6 | 7 | /*int lcs(string string1,string string2) 8 | { 9 | if(string1.size()==0||string2.size()==0) 10 | { 11 | return 0; 12 | } 13 | if(string1[0]==string2[0]) 14 | { 15 | return 1+lcs(string1.substr(1),string2.substr(1)); 16 | } 17 | int a=lcs(string1,string2.substr(1)); 18 | int b=lcs(string1.substr(1),string2); 19 | return max(a,b); 20 | 21 | 22 | }*/ 23 | 24 | int lcs_memo(string str1,string str2) 25 | { 26 | 27 | int memo[1000][1000]; 28 | for(int i=0;i<=str1.size();i++) 29 | { 30 | for(int j=0;j<=str2.size();j++) 31 | { 32 | cout<<"here"< myMap; 85 | string string1,string2; 86 | ///cin>>string1>>string2; 87 | 88 | int ans[100][100]; 89 | for(int i=0;i<100;i++) 90 | {for(int j=0;j<100;j++) 91 | { 92 | ans[i][j]=-1; 93 | 94 | 95 | } 96 | } 97 | cout< 2 | using namespace std; 3 | #define LL long long int 4 | #include 5 | #include 6 | 7 | LL balancedBT(int n) 8 | { 9 | if(n==1) return 1; 10 | if(n==2) return 3; 11 | 12 | LL ans =(balancedBT(n-1)*(balancedBT(n-2)+balancedBT(n-1)) )%10000000+ (balancedBT(n-2)*balancedBT(n-1))%10000000; 13 | 14 | return ans%10000000; 15 | 16 | 17 | 18 | } 19 | 20 | int main() 21 | { 22 | cout< 2 | using namespace std; 3 | #define LL long long int 4 | #include 5 | //#include< 6 | LL minCount_Memo(int ,int *); 7 | LL minCount(int n) 8 | { 9 | int *ans=new int [n+1]; 10 | for(int i=0;i<=n;i++) 11 | { 12 | /// cout<<"value of i is : "< 2 | using namespace std; 3 | #define LL long long int 4 | 5 | LL Fib(int n,LL *memo) 6 | { 7 | if(n<0) return -1; 8 | if(n==0||n==1) 9 | { 10 | return n; 11 | } 12 | /// this means n is greater than 1 13 | /// int x=0,y=0; 14 | if(memo[n-1]==0) 15 | { 16 | memo[n-1]=Fib(n-1,memo); 17 | 18 | } 19 | if(memo[n-2]==0) 20 | { 21 | memo[n-2]=Fib(n-2,memo); 22 | } 23 | memo[n]=memo[n-1]+memo[n-2]; 24 | return memo[n]; 25 | /*int x= Fib(n-1,memo); 26 | int y=Fib(n-2,memo); 27 | return x+y;*/ 28 | } 29 | LL calculateFibonacci(int n) 30 | { 31 | LL *memo=new LL [n+1]; 32 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int minStepsTo1_DP(int n) 7 | { 8 | int *dp=new int [n+1]; 9 | /// this dp[i] represents min steps taken by number i to reach 1 10 | dp[1]=0; 11 | int y,z; 12 | 13 | for(int i=2;i<=n;i++) 14 | { 15 | y=INT_MAX; 16 | z=INT_MAX; 17 | if(i%2==0) 18 | { 19 | y=dp[i/2]; 20 | 21 | } 22 | if(i%3==0) 23 | { 24 | z=dp[i/3]; 25 | 26 | } 27 | dp[i]=1+min(dp[i-1],min(z,y)); 28 | } 29 | return dp[n]; 30 | } 31 | 32 | int main() 33 | { 34 | 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /DynamicProgramming1/Revise/Fibonacci_DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define LL long long int 4 | LL fibHelper(int n,LL *ans) 5 | { 6 | if(n==0||n==1) 7 | { 8 | return ans[n]; 9 | } 10 | if(ans[n-1]==-1) 11 | { 12 | ///calculate and store ans[n-1] 13 | ans[n-1]=fibHelper(n-1,ans); 14 | 15 | } 16 | if(ans[n-2]==-1) 17 | { 18 | ///calculate and store ans[n-2] 19 | ans[n-2]=fibHelper(n-2,ans); 20 | 21 | } 22 | ans[n]=ans[n-1]+ans[n-2]; 23 | return ans[n]; 24 | 25 | 26 | } 27 | LL fibonacci(int n) 28 | { 29 | LL* ans=new LL [n+1]; 30 | for(int i=0;i<=n;i++) 31 | { 32 | ans[i]=-1; 33 | 34 | } 35 | ans[0]=1; 36 | ans[1]=1; 37 | LL x= fibHelper(n,ans); 38 | delete []ans; ans=NULL; 39 | return x; 40 | } 41 | LL fib_Best(int n) 42 | { 43 | int *ans=new int [n+1]; 44 | ans[0]=1;ans[1]=1; 45 | for(int i=2;i<=n;i++) 46 | { 47 | ans[i]=ans[i-1]+ans[i-2]; 48 | 49 | 50 | } 51 | return ans[n]; 52 | } 53 | int main() 54 | { 55 | cout< 2 | using namespace std; 3 | #include 4 | #include 5 | #define LL long long int 6 | LL minSteps(int n) 7 | { 8 | 9 | LL *ans=new LL[n+1]; 10 | ans[0]=INT_MAX; 11 | ans[1]=0; 12 | LL x,y,z; 13 | 14 | for(int i=2;i<=n;i++) 15 | { 16 | x=y=z=INT_MAX; 17 | x=ans[i-1]; 18 | if(i%2==0) 19 | { 20 | y=ans[i/2]; 21 | } 22 | if(i%3==0) 23 | { 24 | z=ans[i/3]; 25 | } 26 | ans[i]=1+min(x,min(y,z)); 27 | 28 | 29 | } 30 | 31 | LL ret= ans[n]; 32 | delete []ans; 33 | ans=NULL; 34 | return ret; 35 | 36 | } 37 | int main() 38 | { 39 | cout< 2 | using namespace std; 3 | #include"Dynamic_array_class.cpp" 4 | 5 | int main() 6 | { 7 | DynamicArray a; 8 | for(int i=0;i<7;i++) 9 | { 10 | a.add(i); 11 | 12 | } 13 | // DynamicArray b(a); 14 | DynamicArray b(a); 15 | DynamicArray c; 16 | 17 | cout<<"Here"<data=new int[d.Size]; 57 | for(int i=0;idata[i]=d.data[i]; 60 | } 61 | this->nextIndex=d.nextIndex; 62 | this->Size=d.Size; 63 | 64 | } 65 | void operator=(DynamicArray const& d) 66 | { 67 | this->data=new int[d.Size]; 68 | for(int i=0;idata[i]=d.data[i]; 71 | } 72 | this->nextIndex=d.nextIndex; 73 | this->Size=d.Size; 74 | } 75 | 76 | }; 77 | -------------------------------------------------------------------------------- /Extra Codes/Fraction_class.cpp: -------------------------------------------------------------------------------- 1 | class Fraction{ 2 | 3 | private: 4 | int numerator; 5 | int denominator; 6 | public: 7 | //constructor 8 | Fraction(int numerator,int denominator){ 9 | this->numerator=numerator; 10 | this->denominator=denominator; 11 | } 12 | Fraction(){}; 13 | 14 | int getNumerator()const 15 | { 16 | return numerator; 17 | } 18 | int getDenominator() const 19 | { 20 | return denominator; 21 | } 22 | void setNumerator(int n){ 23 | this->numerator=n; 24 | 25 | } 26 | void setDenominator(int n) 27 | { 28 | this->denominator=n; 29 | 30 | 31 | } 32 | void print()/// 1/2 and 3/4 33 | { 34 | cout<numerator<<" / "<denominator< numerator/=gcd; 48 | this-> denominator/=gcd; 49 | 50 | 51 | } 52 | 53 | Fraction operator*(Fraction const&f2) const 54 | { 55 | Fraction f(1,1); 56 | f.numerator=f2.numerator*this->numerator; 57 | f.denominator=f2.denominator*this->denominator; 58 | f.simplify(); 59 | return f; 60 | } 61 | 62 | void multiply(Fraction const& f2) 63 | { 64 | numerator=f2.numerator*this->numerator; 65 | 66 | denominator=f2.denominator*this->denominator; 67 | 68 | simplify(); 69 | } 70 | Fraction operator+(Fraction const&f2)const { 71 | Fraction f(1,1); 72 | f.numerator=(this->numerator*f2.denominator)+(this->denominator*f2.numerator); 73 | f.denominator=this->denominator*f2.denominator; 74 | f.simplify(); 75 | return f; 76 | 77 | } 78 | void add( const Fraction &f2){ 79 | /// f2.setNumerator(10); 80 | numerator=(this->numerator*f2.denominator)+(this->denominator*f2.numerator); 81 | denominator=this->denominator*f2.denominator; 82 | 83 | simplify(); 84 | } 85 | /* void operator++(){ 86 | numerator+=denominator; 87 | 88 | }*/ 89 | Fraction operator++(){ 90 | this->numerator=this->numerator+this->denominator; 91 | return *this; 92 | 93 | } 94 | Fraction& operator++(int){ 95 | Fraction fNew(numerator,denominator); 96 | numerator+=denominator; 97 | fNew.simplify(); 98 | simplify(); 99 | return fNew; 100 | 101 | } 102 | 103 | }; 104 | -------------------------------------------------------------------------------- /Extra Codes/GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | void print(int *a,int n){ 8 | 9 | for(int i=0;i>n; 38 | int *a=new int [n]; 39 | int *b=new int[n]; 40 | for(int i=0;i>a[i]; 43 | b[i]=a[i]; 44 | } 45 | sort(b,b+n); 46 | int *diffarray=new int[n-1];/// the size is n-1; 47 | int j=0; 48 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | vector v; 8 | for(int i=0;i<10;i++) 9 | { 10 | v.push_back(i*i); 11 | 12 | } 13 | ///vector::iterator it=v.begin(); 14 | //cout<<*v.find(it)< myMap; 24 | for(int i=0;i<10;i++) 25 | { 26 | 27 | myMap[i*i]=true; 28 | 29 | } 30 | unordered_map 31 | ::iterator it; 32 | for( it=myMap.begin();it!=myMap.end();it++) 33 | { 34 | cout<first<<" , "; 35 | } 36 | cout< 2 | using namespace std; 3 | void printstars(int n) 4 | { 5 | for(int i=1;i<=n;i++) 6 | { 7 | cout<<"* "; } 8 | } 9 | void specialstarseq(int n){ 10 | 11 | if(n%2==1) 12 | printstars(n/2+1); 13 | else{ 14 | printstars(n/2); 15 | } 16 | } 17 | void printspaces(int n){ 18 | 19 | for(int i=1;i<=n;i++) 20 | { 21 | cout<<" "; 22 | } 23 | } 24 | int main() 25 | { 26 | int n; 27 | cin>>n; 28 | int spaces; 29 | spaces=2*n-4; 30 | cout<<" Spaces = "<=1;i--) 48 | { 49 | if(i%2==0){printspaces(1);} 50 | 51 | specialstarseq(i); 52 | 53 | printspaces(spaces); 54 | spaces+=2; 55 | 56 | specialstarseq(i); 57 | 58 | cout<age=age; 16 | } 17 | 18 | //setters 19 | Student(){ 20 | totalStudents++; 21 | } 22 | void setage(int age){ 23 | 24 | this->age=age; 25 | } 26 | void setroll(int roll) 27 | { 28 | this->roll=roll; 29 | } 30 | 31 | void display() const 32 | { 33 | cout< 2 | using namespace std; 3 | 4 | #define LL long long int 5 | #define FOR(a,b,c) for (int (a)=(b); (a)<(c); (a)++) 6 | #define FORN(a,b,c) for (int (a)=(b); (a)<=(c); (a)++) 7 | #define FORD(a,b,c) for (int (a)=(b); (a)>=(c); (a)--) 8 | #define REP(i,n) FOR(i,0,n) 9 | #define REPN(i,n) FORN(i,1,n) 10 | #define REPD(i,n) FORD(i,n,1) 11 | #include"Fraction_class.cpp" 12 | int main() 13 | { 14 | /* Fraction const f(20,7); 15 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | #define LL long long int 9 | #define FOR(a,b,c) for (int (a)=(b); (a)<(c); (a)++) 10 | #define FORN(a,b,c) for (int (a)=(b); (a)<=(c); (a)++) 11 | #define FORD(a,b,c) for (int (a)=(b); (a)>=(c); (a)--) 12 | #define REP(i,n) FOR(i,0,n) 13 | #define REPN(i,n) FORN(i,1,n) 14 | #define REPD(i,n) FORD(i,n,1) 15 | #include"StudentClass.cpp" 16 | 17 | void print(int *a,int size) 18 | { 19 | FOR(i,0,size) cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | #define LL long long int 8 | #define FOR(a,b,c) for (int (a)=(b); (a)<(c); (a)++) 9 | #define FORN(a,b,c) for (int (a)=(b); (a)<=(c); (a)++) 10 | #define FORD(a,b,c) for (int (a)=(b); (a)>=(c); (a)--) 11 | #define REP(i,n) FOR(i,0,n) 12 | #define REPN(i,n) FORN(i,1,n) 13 | #define REPD(i,n) FORD(i,n,1) 14 | #include"Fraction_class.cpp" 15 | 16 | int main() 17 | { 18 | Fraction f1(3,5); 19 | Fraction f2(1,2); 20 | f1.add(f2); 21 | f1.print(); 22 | f2.print(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Extra Codes/compress_the_string.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Extra Codes/compress_the_string.exe -------------------------------------------------------------------------------- /Extra Codes/compress_the_string.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Extra Codes/compress_the_string.o -------------------------------------------------------------------------------- /Extra Codes/redundant_brackets_using_stacks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | bool checkRedundantBrackets(char *input) { 6 | // Write your code here 7 | 8 | 9 | stack s; 10 | ///cout<<"In stack()"< 3 | using namespace std; 4 | #include 5 | 6 | void print(int *edges[],int n,int sv,bool *visited) 7 | { 8 | cout< q; 24 | q.push(sv); 25 | while(!q.empty()) 26 | { 27 | sv=q.front(); 28 | visited[sv]=true; 29 | q.pop(); 30 | cout<>n>>e; 50 | int**edges=new int*[n]; 51 | for(int i=0;i>f>>s; 70 | edges[f][s]=1; 71 | edges[s][f]=1; 72 | 73 | } 74 | /// print(edges,n,0,visited); 75 | 76 | printBFS(edges,n,0,visited); 77 | ///delete memory now 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Graphs1/DFS Graph Traversal.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | #include 5 | 6 | void print(int *edges[],int n,int sv,bool *visited) 7 | { 8 | cout< q; 24 | q.push(sv); 25 | while(!q.empty()) 26 | { 27 | sv=q.front(); 28 | visited[sv]=true; 29 | q.pop(); 30 | cout<>n>>e; 50 | int**edges=new int*[n]; 51 | for(int i=0;i>f>>s; 70 | edges[f][s]=1; 71 | edges[s][f]=1; 72 | 73 | } 74 | /// print(edges,n,0,visited); 75 | 76 | printBFS(edges,n,0,visited); 77 | ///delete memory now 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Graphs1/DijkstrasAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int pickVertexWithMinDistance(bool* visited,int* distance,int V){ 6 | int x=-1; 7 | for(int i=0;i> V >> E; 49 | 50 | int** adjMat=new int*[V]; 51 | for(int i=0;i>v1>>v2>>w; 61 | adjMat[v1][v2]=w; 62 | adjMat[v2][v1]=w; 63 | 64 | 65 | } 66 | cout< 2 | using namespace std; 3 | #include 4 | vector* getPath(int *edges[],int n,int sv,int ev,bool *visited) 5 | { 6 | if(sv==ev){ 7 | //make a vector 8 | vector *v=new vector; 9 | v->push_back(sv); 10 | return v; 11 | } 12 | vector*temp2=NULL; 13 | visited[sv]=true; 14 | 15 | for(int i=0;i*temp=getPath(edges,n,i,ev,visited); 22 | if(temp!=NULL) 23 | { 24 | temp2=temp; 25 | break; 26 | 27 | } 28 | } 29 | } 30 | if(temp2!=NULL) 31 | { 32 | temp2->push_back(sv); 33 | 34 | } 35 | return temp2; 36 | } 37 | 38 | 39 | int main() 40 | { 41 | int n,e; 42 | cin>>n>>e; 43 | int**edges=new int*[n]; 44 | for(int i=0;i>f>>s; 63 | edges[f][s]=1; 64 | edges[s][f]=1; 65 | 66 | } 67 | int v1,v2; 68 | cin>>v1>>v2; 69 | 70 | /// print(edges,n,0,visited); 71 | vector *v=getPath(edges,n,v1,v2,visited); 72 | if(v==NULL) return 0; 73 | 74 | for(int i=0;isize();i++) 75 | { 76 | cout<at(i)<<" "; 77 | } 78 | ///delete memory now 79 | 80 | } 81 | 82 | 83 | -------------------------------------------------------------------------------- /Graphs1/Get Path DFS.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Graphs1/Get Path DFS.exe -------------------------------------------------------------------------------- /Graphs1/Get Path DFS.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Graphs1/Get Path DFS.o -------------------------------------------------------------------------------- /Graphs1/GetPath BFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | unordered_map myMap; 6 | int printBFS(int *edges[],int n,int sv,int ev,bool *visited) 7 | { 8 | queue q; 9 | q.push(sv); 10 | int flag=0; 11 | while(!q.empty()) 12 | { 13 | sv=q.front(); 14 | visited[sv]=true; 15 | q.pop(); 16 | 17 | ///cout<>n>>e; 57 | int**edges=new int*[n]; 58 | for(int i=0;i>f>>s; 77 | edges[f][s]=1; 78 | edges[s][f]=1; 79 | 80 | } 81 | int v1,v2; 82 | cin>>v1>>v2; 83 | /// print(edges,n,0,visited); 84 | 85 | int ans= printBFS(edges,n,v1,v2,visited); 86 | if(ans==0) return 0; 87 | else 88 | printMap(v1,v2); 89 | ///delete memory now 90 | 91 | } 92 | 93 | -------------------------------------------------------------------------------- /Graphs1/GetPath BFS.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Graphs1/GetPath BFS.exe -------------------------------------------------------------------------------- /Graphs1/GetPath BFS.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Graphs1/GetPath BFS.o -------------------------------------------------------------------------------- /Graphs1/Graphs_Adj_Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void print(int **edges,int n,int sv,bool *visited) 4 | { 5 | cout<>n>>e; 25 | int **edges=new int* [n]; 26 | for(int i=0;i>father>>son;///father son node relation 41 | edges[father][son]=1; 42 | edges[son][father]=1; 43 | 44 | } 45 | cout<<"Here"< 2 | using namespace std; 3 | 4 | bool hasPath(int *edges[],int n,int sv,int ev,bool *visited) 5 | { 6 | 7 | visited[sv]=true; 8 | if(edges[sv][ev]==1) return true; 9 | 10 | /// else part 11 | 12 | for(int i=0;i>n>>e; 29 | int**edges=new int*[n]; 30 | for(int i=0;i>f>>s; 49 | edges[f][s]=1; 50 | edges[s][f]=1; 51 | 52 | } 53 | int v1,v2; 54 | cin>>v1>>v2; 55 | bool ans=hasPath(edges,n,v1,v2,visited); 56 | if(ans=true) 57 | cout<<"true"; 58 | else cout<<"false"; 59 | 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Graphs1/HasPath Problem.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Graphs1/HasPath Problem.exe -------------------------------------------------------------------------------- /Graphs1/HasPath Problem.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Graphs1/HasPath Problem.o -------------------------------------------------------------------------------- /Graphs1/KruskalsAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Edge{ 6 | public: 7 | int source; 8 | int destination; 9 | int weight; 10 | 11 | Edge(){ 12 | this->source=-1; 13 | this->destination=-1; 14 | this->weight=-1; 15 | 16 | } 17 | 18 | Edge(int s,int d,int w){ 19 | this->source=s; 20 | this->destination=d; 21 | this->weight=w; 22 | 23 | } 24 | 25 | }; 26 | 27 | bool wayToSort(Edge e1,Edge e2) 28 | { return e2.weight > e1.weight; 29 | 30 | } 31 | 32 | int getParent(int* parent,int v){ 33 | if(parent[v]==v) 34 | return v; // we want the absolute top one 35 | v=parent[v]; 36 | return getParent(parent,v); 37 | 38 | 39 | } 40 | int main() 41 | { 42 | 43 | int V, E; 44 | cin >> V >> E; 45 | Edge input[E]; 46 | Edge output[V-1];// our MST 47 | //1 take input 48 | for(int i=0;i>s>>d>>w; 51 | input[i]=Edge(s,d,w); 52 | } 53 | 54 | 55 | 56 | 57 | //2 sort the array 58 | sort(input,input+E,wayToSort); 59 | 60 | int parent[V]; 61 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | int e; 7 | cin>>n>>e; 8 | int **edges=new int*[n]; 9 | for(int i=0;i>f>>s; 21 | edges[f][s]=1; 22 | edges[s][f]=1; 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /Graphs1/PrimsAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int findMinVertex(bool* visited,int* weight,int V){ 7 | int x=INT_MAX; 8 | int y=-1; 9 | for(int i=0;i> V >> E; 82 | 83 | int** adjMat=new int*[V]; 84 | for(int i=0;i>v1>>v2>>w; 94 | adjMat[v1][v2]=w; 95 | adjMat[v2][v1]=w; 96 | 97 | 98 | } 99 | cout< 2 | #include 3 | template 4 | class MapNode{ 5 | public: 6 | string key; 7 | int value; 8 | MapNode*next; 9 | MapNode(string key,int value) 10 | { 11 | this->key=key; 12 | this->value=value; 13 | next=NULL; 14 | } 15 | ~MapNode() 16 | { 17 | 18 | delete next; 19 | } 20 | 21 | }; 22 | template 23 | class myMap{ 24 | private: 25 | MapNode** buckets; 26 | int Count; 27 | int numBuckets; 28 | public: 29 | myMap() 30 | { 31 | Count=0; 32 | numBuckets=5; 33 | buckets=new MapNode*[numBuckets]; 34 | for(int i=0;i=0;i--) 47 | { 48 | hashCode=hashCode+key[i]*currentCoeff; 49 | hashCode=hashCode%numBuckets; 50 | currentCoeff*=37; 51 | currentCoeff=currentCoeff%numBuckets; 52 | 53 | } 54 | 55 | return hashCode%numBuckets; 56 | } 57 | public: 58 | 59 | rehash() 60 | { 61 | MapNode**temp=buckets; 62 | buckets=new MapNode* [2*numBuckets]; 63 | for(int i=0;i<2*numBuckets;i++) 64 | { 65 | buckets[i]=NULL; 66 | 67 | } 68 | numBuckets*=2; 69 | Count=0; 70 | for(int i=0;i*head=temp[i]; 73 | while(head!=NULL) 74 | { 75 | Insert(head->key,head->value); 76 | head=head->next; 77 | 78 | } 79 | } 80 | for(int i=0;i 0.7 91 | if(Count/(numBuckets*1.0)>0.7) 92 | { 93 | rehash(); 94 | } 95 | 96 | int bucketIndex=getBucketIndex(key); 97 | MapNode*head=buckets[bucketIndex]; 98 | 99 | while(head!=NULL) 100 | { 101 | ///during this traversal if you happen to find the key 102 | if(head->key==key) 103 | { 104 | head->value=value; 105 | return; 106 | } 107 | head=head->next; 108 | } 109 | head=buckets[bucketIndex]; 110 | MapNode*node=new MapNode(key,value); 111 | node->next=head; 112 | buckets[bucketIndex]=node; 113 | Count++; 114 | return; 115 | 116 | 117 | } 118 | int getSize/*returns number of nodes inserted*/() 119 | { 120 | return Count; 121 | } 122 | V getValue(string key) 123 | { 124 | int bucketIndex=getBucketIndex( key); 125 | MapNode*head=buckets[bucketIndex]; 126 | while(head!=NULL) 127 | { 128 | 129 | ///during this traversal if you happen to find the key 130 | if(head->key==key) 131 | { 132 | return head->value; 133 | } 134 | head=head->next; 135 | } 136 | 137 | return 0; 138 | 139 | } 140 | V Remove(string key) 141 | { 142 | int bucketIndex=getBucketIndex( key); 143 | MapNode*head=buckets[bucketIndex]; 144 | MapNode*prev=NULL; 145 | while(head!=NULL) 146 | { 147 | ///during this traversal if you happen to find the key 148 | if(head->key==key) 149 | { 150 | if(prev==NULL) 151 | { 152 | buckets[bucketIndex]=head->next; 153 | 154 | }else{ 155 | prev->next=head->next; 156 | } 157 | V value=head->value; 158 | head->next=NULL; 159 | delete head; 160 | Count--; 161 | return value; 162 | 163 | } 164 | prev=head; 165 | head=head->next; 166 | } 167 | 168 | return 0; 169 | 170 | } 171 | 172 | bool Search(string key) 173 | { 174 | int bucketIndex=getBucketIndex(key); 175 | 176 | MapNode* tail=buckets[bucketIndex]; 177 | while(tail!=NULL) 178 | { 179 | if(tail->key==key) 180 | { 181 | return true; 182 | } 183 | tail=tail->next; 184 | 185 | } 186 | return false; 187 | 188 | } 189 | 190 | }; 191 | -------------------------------------------------------------------------------- /HashMaps Implementation/MapClassUse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include"MapClass.h" 4 | int main() 5 | { 6 | myMap M; 7 | M.Insert("abc",1000); 8 | M.Insert("def",200); 9 | M.Insert("ghi",2000); 10 | M.Insert("abcd",3100); 11 | M.Insert("cba",2500); 12 | cout<<"size of the map is : "<* root; 6 | int numberOfNodes; 7 | public: 8 | BST() 9 | { 10 | root=NULL; 11 | numberOfNodes=0; 12 | } 13 | private: 14 | 15 | bool hasDataHelper(int data,BinaryTreeNode*node){ 16 | if(node==NULL) return false; 17 | if(node->data==data) return true; 18 | else{ 19 | 20 | ///since this is a BST 21 | if(datadata) 22 | { 23 | return hasDataHelper(data,node->left); 24 | } 25 | else return hasDataHelper(data,node->right); 26 | } 27 | 28 | 29 | } 30 | public: 31 | 32 | bool hasData(int data){ 33 | return hasDataHelper(data,root); 34 | } 35 | 36 | private: 37 | void insertHelper(int data,BinaryTreeNode*&node){ 38 | ///cout<<"In insertHelper"<(data); 43 | 44 | return; 45 | } 46 | if(datadata&&node->left==NULL){ 47 | ///cout<<"I am in root->left"<Data== "<data<*n=new BinaryTreeNode(data); 49 | node->left=n; 50 | return; 51 | } 52 | else if(data>=node->data&&node->right==NULL){ 53 | 54 | BinaryTreeNode*n=new BinaryTreeNode(data); 55 | node->right=n; 56 | return; 57 | } 58 | else{ 59 | if(datadata) 60 | { 61 | insertHelper(data,node->left); return; 62 | } 63 | else{ 64 | insertHelper(data,node->right); return; 65 | } 66 | } 67 | 68 | } 69 | public: 70 | 71 | void Insert(int data){ 72 | ///if(!root) cout<<"Root is NULL"<data<<" : is roots data after insertion "<* root) 79 | { 80 | 81 | if(root==NULL) return; 82 | cout<data<<":"; 83 | if(root->left) 84 | cout<<"L"<left->data<<","; 85 | if(root->right) 86 | { 87 | cout<<"R"<right->data; 88 | 89 | } 90 | cout<left); 92 | printTreeProper(root->right); 93 | return; 94 | 95 | 96 | } 97 | public: 98 | 99 | void printTree() 100 | { 101 | printTreeProper(root); 102 | 103 | } 104 | private: 105 | BinaryTreeNode* minimum(BinaryTreeNode*root) 106 | { 107 | if(!root) return root; 108 | 109 | BinaryTreeNode*Lmin=minimum(root->left); 110 | BinaryTreeNode*Rmin=minimum(root->right); 111 | if(Lmin==NULL&&Rmin==NULL) return root; 112 | else if(Lmin==NULL){ 113 | if(root->datadata) return root; 114 | else return Rmin; 115 | } 116 | else if(Rmin==NULL){ 117 | if(root->datadata) return root; 118 | else return Lmin; 119 | } 120 | if(root->datadata&&root->datadata) return root; 121 | else if(Lmin->datadata){return Lmin;} 122 | else return Rmin; 123 | 124 | 125 | } 126 | BinaryTreeNode* deleteDataHelper(int data,BinaryTreeNode*root){ 127 | if(root==NULL) return root; 128 | if(root->data==data) 129 | { 130 | if(!(root->left||root->right)) 131 | { 132 | return NULL; 133 | 134 | } 135 | else if(!root->left&&root->right) 136 | { 137 | return root->right; 138 | 139 | } 140 | else if(!root->right&&root->left) 141 | { 142 | return root->left; 143 | } 144 | else{ 145 | BinaryTreeNode*node=minimum(root->right); 146 | root->data=node->data; 147 | node=deleteDataHelper(node->data,node); 148 | 149 | } 150 | return root; 151 | } 152 | else{ 153 | if(datadata) 154 | { 155 | root->left= deleteDataHelper(data,root->left); 156 | } 157 | else{ 158 | root->right=deleteDataHelper(data,root->right); 159 | } 160 | return root; 161 | } 162 | 163 | } 164 | public: 165 | void deleteData(int data) 166 | { 167 | root=deleteDataHelper(data,root); 168 | 169 | } 170 | }; 171 | -------------------------------------------------------------------------------- /HashMaps/BTNodeClass.cpp: -------------------------------------------------------------------------------- 1 | template 2 | class BinaryTreeNode{ 3 | public: 4 | int Key; 5 | T data; 6 | BinaryTreeNode*left; 7 | BinaryTreeNode*right; 8 | 9 | BinaryTreeNode(int Key,T data) 10 | { 11 | this->Key=Key; 12 | this->data=data; 13 | left=NULL; 14 | right=NULL; 15 | } 16 | BinaryTreeNode(int data) 17 | { 18 | 19 | this->data=data; 20 | left=right=NULL; 21 | } 22 | 23 | /* void printTree() 24 | { 25 | if(this==NULL) return; 26 | cout<printTree(); 28 | right->printTree(); 29 | return; 30 | 31 | } 32 | */ 33 | ~BinaryTreeNode() 34 | { 35 | 36 | delete left; 37 | delete right; 38 | } 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /HashMaps/BinaryTreeClassUse_Basic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #include"BTNodeClass.cpp" 5 | #include 6 | 7 | void printTreeProper(BinaryTreeNode* root) 8 | { 9 | 10 | if(root==NULL) return; 11 | cout<data<<":"; 12 | if(root->left) 13 | cout<<"L"<left->data<<" "; 14 | if(root->right) 15 | { 16 | cout<<"R"<right->data; 17 | 18 | } 19 | cout<left); 21 | printTreeProper(root->right); 22 | return; 23 | 24 | } 25 | void printLevelWise(BinaryTreeNode*root) 26 | { 27 | if(root==NULL) return; 28 | queue*> q; 29 | q.push(root); 30 | cout<data<*temp=q.front(); 34 | q.pop(); 35 | /// cout<data<<" "; 36 | if(temp->left){ 37 | 38 | cout<right->data<<" "; 39 | q.push(temp->left); 40 | } 41 | if(temp->right){ 42 | cout<right->data; 43 | q.push(temp->right); 44 | } 45 | 46 | cout<* takeInputLevelWise() 51 | { 52 | BinaryTreeNode* root; 53 | BinaryTreeNode* leftChildNode; 54 | BinaryTreeNode* rightChildNode; 55 | int data; 56 | cout<<"Enter Root Data: "<>data; 57 | if(data==-1) return NULL; 58 | 59 | root=new BinaryTreeNode(data); 60 | 61 | queue *> q; 62 | q.push(root); 63 | while(q.size()!=0) 64 | { 65 | int child_left=-1,child_right=-1; 66 | BinaryTreeNode*temp=q.front(); 67 | q.pop(); 68 | cout<<"Enter left child of "<data<>child_left; 70 | cout<<"Enter right child of "<data<>child_right; 72 | if(child_left!=-1) 73 | { 74 | leftChildNode=new BinaryTreeNode (child_left); 75 | temp->left=leftChildNode; 76 | q.push(leftChildNode); 77 | 78 | } 79 | if(child_right!=-1) 80 | { 81 | rightChildNode=new BinaryTreeNode (child_right); 82 | temp->right=rightChildNode; 83 | q.push(rightChildNode); 84 | 85 | } 86 | } 87 | return root; 88 | 89 | } 90 | 91 | void printAtLevelK(BinaryTreeNode*root,int k) 92 | { 93 | if(root==NULL) return; 94 | if(k==0) {cout<data<left,k-1); 98 | printAtLevelK(root->right,k-1); 99 | return; 100 | } 101 | } 102 | int height(BinaryTreeNode*root) 103 | { 104 | if(root==NULL) return 0; 105 | if(root->left==NULL&&root->right==NULL) return 1; 106 | 107 | return 1+max(height(root->left),height(root->right)); 108 | 109 | 110 | }/* 111 | int diameter(BinaryTreeNode*root) 112 | { 113 | if(root) 114 | cout<<"Height of root->left is : "<left)<left); 117 | int rh=height(root->right); 118 | int ld=diameter(root->left); 119 | int rd=diameter(root->right); 120 | return max(lh+rh+1,max(ld,rd)); 121 | } 122 | */ 123 | ///********************************** 124 | class Pair{ 125 | public: 126 | 127 | int height; 128 | int diameter; 129 | Pair(){ 130 | height=0; 131 | diameter=-1; 132 | } 133 | }; 134 | Pair heightAndDiameter(BinaryTreeNode*root) 135 | { 136 | if(root==NULL) { 137 | Pair P; 138 | return P; 139 | } 140 | if(root->left==NULL&&root->right==NULL) 141 | { 142 | Pair P; 143 | P.diameter=1; 144 | P.height=1; 145 | return P; 146 | 147 | } 148 | Pair Lp,Rp; 149 | Lp=heightAndDiameter(root->left); 150 | Rp=heightAndDiameter(root->right); 151 | Pair Ans; 152 | Ans.height=1+max(Lp.height,Rp.height); 153 | Ans.diameter=max(Lp.height+Rp.height+1,max(Lp.diameter,Rp.diameter)); 154 | return Ans; 155 | 156 | 157 | } 158 | int diameter(BinaryTreeNode*root){ 159 | return heightAndDiameter(root).diameter;} 160 | ///********************************** 161 | /* 162 | class Pair{ 163 | public: 164 | 165 | bool isThere; 166 | int depth; 167 | 168 | Pair() 169 | { 170 | isThere=false; 171 | depth=0; 172 | } 173 | Pair(bool a,int d) 174 | { 175 | isThere=a; 176 | depth=d; 177 | } 178 | 179 | }; 180 | Pair isNodeThereAndDepth(BinaryTreeNode*root,int data) 181 | { 182 | if(root==NULL){ 183 | Pair p; 184 | return p; 185 | } 186 | 187 | if(root->data==data) 188 | { 189 | Pair p(true,1);return p; 190 | 191 | } 192 | Pair p1,p2; 193 | p1=isNodeThereAndDepth(root->left,data); 194 | p2=isNodeThereAndDepth(root->right,data); 195 | 196 | if(p1.isThere){ 197 | p1.depth+=1; 198 | return p1; 199 | } 200 | else if(p2.isThere) { 201 | p2.depth+=1; 202 | return p2;} 203 | else{ 204 | Pair p; 205 | return p; 206 | } 207 | 208 | } 209 | 210 | void nodesAtDistanceK(BinaryTreeNode *root, int node, int k) { 211 | // Write your code here 212 | cout<<" i come here"<data<<" value of k is : "<data==node) 216 | { 217 | printAtLevelK(root,k); 218 | } 219 | Pair p1,p2; 220 | 221 | p1=isNodeThereAndDepth(root->left,node); 222 | if(p1.isThere) 223 | { 224 | cout<<" p1 depth is : "<data<right,k-p1.depth-1);///check if it will really print at depth - k or any other higher or lower level 229 | 230 | } 231 | nodesAtDistanceK(root->left,node,k); 232 | } 233 | p2=isNodeThereAndDepth(root->right,node); 234 | if(p2.isThere) 235 | { 236 | if(p2.depth==k) cout<data<left,p2.depth-k);///check if it will really print ar depth - k or any other higher or lower level 240 | 241 | } 242 | nodesAtDistanceK(root->right,node,k); 243 | } 244 | return; 245 | } 246 | */ 247 | ///1 2 3 4 5 -1 -1 6 7 -1 -1 -1 -1 -1 -1 248 | ///5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1 249 | int main() 250 | { 251 | BinaryTreeNode*root; 252 | root=takeInputLevelWise(); 253 | cout< 2 | #include 3 | using namespace std; 4 | #include"BTNodeClass.cpp" 5 | #include 6 | void print(int *a,int s,int e) 7 | { 8 | for(int i=s;i<=e;i++) 9 | { 10 | cout<* root) 15 | { 16 | if(root==NULL) return ; 17 | cout<data<<" "; 18 | printTree(root->left); 19 | printTree(root->right); 20 | 21 | } 22 | void printTreeProper(BinaryTreeNode* root) 23 | { 24 | 25 | if(root==NULL) return; 26 | cout<data<<":"; 27 | if(root->left) 28 | cout<<"L"<left->data<<" "; 29 | if(root->right) 30 | { 31 | cout<<"R"<right->data; 32 | 33 | } 34 | cout<left); 36 | printTreeProper(root->right); 37 | return; 38 | 39 | 40 | } 41 | BinaryTreeNode* takeInput() 42 | { 43 | int data; 44 | cout<<"Enter Data "<>data; 46 | if(data==-1) return NULL; 47 | 48 | BinaryTreeNode*root=new BinaryTreeNode(data); 49 | 50 | root->left=takeInput(); 51 | root->right=takeInput(); 52 | return root; 53 | 54 | 55 | }/// simple input 1 2 -1 4 -1 -1 3 5 -1 -1 6 -1 -1 56 | BinaryTreeNode* takeInputLevelWise() 57 | { 58 | BinaryTreeNode* root; 59 | BinaryTreeNode* leftChildNode; 60 | BinaryTreeNode* rightChildNode; 61 | int data; 62 | cout<<"Enter Root Data: "<>data; 63 | if(data==-1) return NULL; 64 | 65 | root=new BinaryTreeNode (data); 66 | 67 | queue *> q; 68 | q.push(root); 69 | while(q.size()!=0) 70 | { 71 | int child_left=-1,child_right=-1; 72 | BinaryTreeNode*temp=q.front(); 73 | q.pop(); 74 | cout<<"Enter left child of "<data<>child_left; 76 | cout<<"Enter right child of "<data<>child_right; 78 | if(child_left!=-1) 79 | { 80 | leftChildNode=new BinaryTreeNode (child_left); 81 | temp->left=leftChildNode; 82 | q.push(leftChildNode); 83 | 84 | } 85 | if(child_right!=-1) 86 | { 87 | rightChildNode=new BinaryTreeNode (child_right); 88 | temp->right=rightChildNode; 89 | q.push(rightChildNode); 90 | 91 | } 92 | } 93 | return root; 94 | 95 | } 96 | 97 | void InOrder(BinaryTreeNode*root) 98 | { 99 | if(root==NULL) return; 100 | InOrder(root->left); 101 | cout<data<<" "; 102 | InOrder(root->right); 103 | return; 104 | } 105 | void postOrder(BinaryTreeNode*root) 106 | { 107 | if(root==NULL) return; 108 | postOrder(root->left); 109 | postOrder(root->right); 110 | cout<data<<" "; 111 | return; 112 | } 113 | void preOrder(BinaryTreeNode*root) 114 | { 115 | if(root==NULL) return; 116 | cout<data<<" "; 117 | preOrder(root->left); 118 | preOrder(root->right); 119 | return; 120 | } 121 | 122 | /// get Tree works fine:) 123 | BinaryTreeNode* getTree(int *pre,int *in,int preS,int preE,int inS,int inE) 124 | { 125 | ///int size=ei_pre-si_pre+1; 126 | if(inS>inE) return NULL; 127 | 128 | int rootIndex=-1; 129 | for(int i=inS;i<=inE;i++) 130 | { 131 | if(in[i]==pre[preS]) 132 | { 133 | rootIndex=i; 134 | break; 135 | } 136 | 137 | } 138 | int rootData=pre[preS]; 139 | int lPreS=preS+1; 140 | int lInS=inS; 141 | int lInE=rootIndex-1; 142 | int lPreE=lInE-lInS+lPreS; 143 | int rPreS=lPreE+1; 144 | int rPreE=preE; 145 | int rInS=rootIndex+1; 146 | int rInE=inE; 147 | 148 | BinaryTreeNode* root=new BinaryTreeNode (rootData); 149 | root->left=getTree(pre,in,lPreS,lPreE,lInS,lInE); 150 | 151 | root->right=getTree(pre,in,rPreS,rPreE,rInS,rInE); 152 | 153 | return root; 154 | 155 | 156 | } 157 | 158 | BinaryTreeNode* buildTree(int *preorder, int preLength, int *inorder, int inLength) { 159 | // Write your code here 160 | int s1,e1,s2,e2; 161 | s1=0;s2=0;e1=preLength-1;e2=inLength-1; 162 | /// cout<<"I am in First function: "<* getTree1(int *post,int *in,int postS,int postE,int inS,int inE) 168 | { 169 | if(inS>inE) return NULL; 170 | 171 | int rootIndex=-1; 172 | for(int i=inS;i<=inE;i++) 173 | { 174 | if(in[i]==post[postE]) 175 | { 176 | rootIndex=i; 177 | break; 178 | } 179 | 180 | } 181 | int rootData=post[postE]; 182 | 183 | int lInS=inS; 184 | int lInE=rootIndex-1; 185 | int lPostS=postS; 186 | int lPostE=lInE-lInS+lPostS; 187 | int rInS=rootIndex+1; 188 | int rInE=inE; 189 | int rPostS=lPostE+1; 190 | int rPostE=postE-1; 191 | BinaryTreeNode* root=new BinaryTreeNode (rootData); 192 | root->left=getTree(post,in,lPostS,lPostE,lInS,lInE); 193 | 194 | root->right=getTree(post,in,rPostS,rPostE,rInS,rInE); 195 | 196 | return root; 197 | 198 | 199 | } 200 | 201 | BinaryTreeNode* getTreeFromPostorderAndInorder(int *postorder, int postLength, int *inorder, int inLength) { 202 | // Write your code here 203 | int len=inLength-1; 204 | return getTree1(postorder,inorder,0,len,0,len); 205 | 206 | } 207 | 208 | int height(BinaryTreeNode*root) 209 | { 210 | if(root==NULL) return 0; 211 | return 1+max(height(root->left),height(root->right)); 212 | 213 | } 214 | 215 | int diameter(BinaryTreeNode* root) 216 | { 217 | if(root==NULL) return 0; 218 | int h1=height(root->left); 219 | int h2=height(root->right); 220 | /// cout<<"H1 and H2 are : "<left); 222 | int d2=diameter(root->right); 223 | return max(max(h1+h2,d1),d2); 224 | 225 | } 226 | 227 | ///better approach for diameter 13/8/17 228 | 229 | class Pair{ 230 | public: 231 | int height; 232 | int diameter; 233 | Pair() 234 | { 235 | height=diameter=0; 236 | 237 | } 238 | Pair(int h,int d) 239 | { 240 | height=h; 241 | diameter=d; 242 | 243 | } 244 | }; 245 | 246 | Pair heightAndDiameter(BinaryTreeNode*root) 247 | { 248 | Pair HandD; 249 | if(root==NULL) return HandD; 250 | Pair p1=heightAndDiameter(root->left); 251 | Pair p2=heightAndDiameter(root->right); 252 | 253 | int height_leftTree=p1.height; 254 | int height_rightTree=p2.height; 255 | int diameter_leftTree=p1.diameter; 256 | int diameter_rightTree=p2.diameter; 257 | 258 | int height=1+max(height_leftTree,height_rightTree); 259 | int diameter=max(max(height_leftTree+height_rightTree,diameter_leftTree),diameter_rightTree); 260 | 261 | Pair P(height,diameter); 262 | return P; 263 | 264 | } 265 | int diameterAdvanced(BinaryTreeNode*root) 266 | { 267 | return heightAndDiameter(root).diameter; 268 | 269 | } 270 | 271 | 272 | vector findPath(BinaryTreeNode* root,int x) 273 | { 274 | if(! root) { 275 | vector v; 276 | return v; 277 | } 278 | if(root->data==x) 279 | { 280 | 281 | vector v; 282 | v.push_back(x); 283 | return v; 284 | 285 | } 286 | vector vLeft=findPath(root->left,x); 287 | if(vLeft.size()) 288 | { 289 | vLeft.push_back(root->data); 290 | return vLeft; 291 | 292 | } 293 | vector vRight=findPath(root->right,x); 294 | if(vRight.size()) 295 | { 296 | 297 | vRight.push_back(root->data); 298 | return vRight; 299 | } 300 | else return vRight; 301 | } 302 | 303 | int main() 304 | { 305 | BinaryTreeNode*root=new BinaryTreeNode(10); 306 | BinaryTreeNode*node1=new BinaryTreeNode(20); 307 | BinaryTreeNode*node2=new BinaryTreeNode(30); 308 | root->left=node1; 309 | root->right=node2; 310 | node2=new BinaryTreeNode(40); 311 | root->left->right=node2; 312 | /* cout<<"The class print function: "<printTree(); 314 | cout<>n; 331 | size1=size2=n; 332 | for(int i=0;i>a1[i]; 336 | } 337 | for(int i=0;i>a2[i]; 340 | } 341 | 342 | root=buildTree(a1,size1,a2,size2); 343 | printTreeProper(root); 344 | */ 345 | root=takeInputLevelWise(); 346 | printTreeProper(root); 347 | /// postOrder(root);cout< v; 353 | cout<<"I am now to find path "<>n; 357 | v=findPath(root,n); 358 | ///1 2 3 4 5 -1 7 -1 -1 6 -1 -1 -1 -1 -1 359 | for(int i=0;i 2 | using namespace std; 3 | #include"HashMapsClass_TrialAndError.cpp" 4 | #include 5 | int main() 6 | { 7 | HashMap h; 8 | h.Insert(10,"abc"); 9 | h.Insert(20,"def"); 10 | h.Insert(30,"ghi"); 11 | h.Insert(5,"jkl"); 12 | h.Insert(25,"MNO"); 13 | 14 | 15 | cout<<"if Yes then 1>>>"< 3 | class HashMap{ 4 | private: 5 | BinaryTreeNode*root; 6 | int Size; 7 | 8 | public: 9 | HashMap() 10 | { 11 | 12 | root=NULL;Size=0; 13 | } 14 | 15 | void printHelper(BinaryTreeNode*root) 16 | { 17 | if(root==NULL) return; 18 | cout<Key<<":"; 19 | if(root->left) cout<<"L:"<left->Key<<","; 20 | if(root->right) cout<<"R:"<right->Key; 21 | cout<left); 23 | printHelper(root->right); 24 | return; 25 | } 26 | void printTree() 27 | { 28 | 29 | printHelper(root); 30 | return; 31 | } 32 | private: 33 | 34 | 35 | bool searchHelper(BinaryTreeNode*root,int Key) 36 | { 37 | ///cout<<"In search helper"<Key==Key) return true; 40 | if(KeyKey){ 41 | return searchHelper(root->left,Key); 42 | } 43 | else { 44 | return searchHelper(root->right,Key); 45 | } 46 | return false;///No need 47 | } 48 | public: 49 | bool Search(int key) 50 | { 51 | /// cout<<"In search"<* insertHelper(int Key,T data,BinaryTreeNode*node){ 57 | if(node==NULL) 58 | { 59 | node=new BinaryTreeNode(Key,data); 60 | 61 | return node; 62 | } 63 | if(KeyKey&&node->left==NULL){ 64 | BinaryTreeNode*n=new BinaryTreeNode(Key,data); 65 | node->left=n; 66 | return node; 67 | } 68 | else if(Key>=node->Key&&node->right==NULL){ 69 | 70 | BinaryTreeNode*n=new BinaryTreeNode(Key,data); 71 | node->right=n; 72 | return node; 73 | } 74 | else{ 75 | if(KeyKey) 76 | { 77 | node->left= insertHelper(Key,data,node->left); return node; 78 | } 79 | else{ 80 | node->right= insertHelper(Key,data,node->right); return node; 81 | } 82 | } 83 | } 84 | public: 85 | 86 | void Insert(int Key,T data){ 87 | bool isKeyPresent=Search(Key); 88 | if(isKeyPresent){ 89 | cout<<"The Key already Exists"<* deleteDataHelper(int Key,BinaryTreeNode*root){ 98 | if(root==NULL) return root; 99 | if(root->Key==Key) 100 | { 101 | if(!(root->left||root->right)) 102 | { 103 | return NULL; 104 | 105 | } 106 | else if(!root->left&&root->right) 107 | { 108 | return root->right; 109 | 110 | } 111 | else if(!root->right&&root->left) 112 | { 113 | return root->left; 114 | } 115 | else{ 116 | BinaryTreeNode*node=minimum(root->right); 117 | root->Key=node->Key; 118 | node=deleteDataHelper(node->Key,node); 119 | 120 | } 121 | return root; 122 | } 123 | else{ 124 | if(KeyKey) 125 | { 126 | root->left= deleteDataHelper(Key,root->left); 127 | } 128 | else{ 129 | root->right=deleteDataHelper(Key,root->right); 130 | } 131 | return root; 132 | } 133 | 134 | } 135 | public: 136 | void deleteData(int key) 137 | { 138 | if(!Search(key)){ 139 | cout<<"No key Found"< 2 | using namespace std; 3 | #include 4 | #include 5 | #include 6 | vector removeDuplicates(int *a,int n) 7 | { 8 | vector v; 9 | unordered_map seen; 10 | for(int i=0;i0) 13 | continue; 14 | else{ 15 | seen[a[i]]=true; 16 | v.push_back(a[i]); 17 | continue; 18 | 19 | } 20 | 21 | } 22 | return v; 23 | 24 | } 25 | 26 | int main() 27 | { 28 | unordered_map ourmap; 29 | unordered_map myMap; 30 | for(int i=0;i<10;i++) 31 | { 32 | myMap[i*i]=i; 33 | 34 | } 35 | for(auto it=myMap.begin();it!=myMap.end();it++) 36 | { 37 | cout<<"> "<first<<"-> "<second< v=removeDuplicates(a,14); 42 | for(int i=0;idata=data; 9 | next=NULL; 10 | prev=NULL; 11 | } 12 | 13 | }; 14 | -------------------------------------------------------------------------------- /Linklists/Class_LL.cpp: -------------------------------------------------------------------------------- 1 | class Node{ 2 | public: 3 | int data; 4 | Node *next; 5 | 6 | Node(int data){ 7 | this->data=data; 8 | this->next=NULL; 9 | } 10 | 11 | 12 | }; 13 | -------------------------------------------------------------------------------- /Linklists/DoublyList_Use.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include"Class_DoublyLL.cpp" 4 | 5 | Node* takeInput() 6 | { 7 | int data; 8 | cin>>data; 9 | Node*head=NULL; 10 | Node*tail=NULL; 11 | while(data!=-1) 12 | { 13 | Node*n=new Node(data); 14 | if(head==NULL) 15 | { 16 | head=n;tail=n; 17 | 18 | } 19 | else{ 20 | tail->next=n; 21 | n->prev=tail; 22 | tail=tail->next; 23 | } 24 | cin>>data; 25 | } 26 | return head; 27 | 28 | } 29 | 30 | void print(Node* head){ 31 | while(head!=NULL) 32 | { 33 | cout<data; 34 | if(head->next!=NULL) 35 | cout<<"->"; 36 | head=head->next; 37 | } 38 | cout<next!=NULL) { 42 | head=head->next; 43 | } 44 | while(head!=NULL){ 45 | 46 | cout<data<<" "; 47 | 48 | head=head->prev; 49 | 50 | } 51 | return head; 52 | } 53 | int main() 54 | { 55 | Node*head=takeInput(); 56 | print(head); 57 | reverse_LL(head); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Linklists/DoublyList_Use.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Linklists/DoublyList_Use.exe -------------------------------------------------------------------------------- /Linklists/DoublyList_Use.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Linklists/DoublyList_Use.o -------------------------------------------------------------------------------- /Linklists/LL101.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include"Class_LL.cpp" 4 | 5 | Node* takeInput(){ 6 | int data; 7 | cin>>data; 8 | Node*head=NULL; 9 | Node*tail=NULL; 10 | while(data!=-1){ 11 | Node*n=new Node(data); 12 | if(head==NULL) 13 | { 14 | head=n; 15 | tail=n; 16 | } 17 | else{ 18 | 19 | tail->next=n; 20 | tail=tail->next; 21 | 22 | } 23 | cin>>data; 24 | } 25 | return head; 26 | } 27 | 28 | void print(Node*head){ 29 | Node* temp=head; 30 | while(temp!=NULL){ 31 | 32 | cout<data<<" " ; 33 | temp=temp->next; 34 | 35 | } 36 | cout<next=n2; 51 | n2->next=n3; 52 | n3->next=n4; 53 | n4->next=n5; 54 | Node*head; 55 | head=n1; 56 | print(head);*/ 57 | 58 | 59 | return 0; 60 | 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /Linklists/LL101.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Linklists/LL101.exe -------------------------------------------------------------------------------- /Linklists/LL101.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Linklists/LL101.o -------------------------------------------------------------------------------- /Linklists/LL101.pp.c: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Linklists/LinkedList_revise1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include"Node.cpp" 4 | 5 | class Pair{ 6 | public: 7 | Node*head; 8 | Node*tail; 9 | Pair() 10 | { 11 | head=NULL; 12 | tail=NULL; 13 | } 14 | }; 15 | 16 | Pair reverse_ll(Node*head) 17 | { 18 | if(head==NULL||head->next==NULL) 19 | { 20 | Pair Ans; 21 | Ans.head=head; 22 | Ans.tail=head; 23 | return Ans; 24 | } 25 | Pair smallAns=reverse_ll(head->next); 26 | smallAns.tail->next=head; 27 | head->next=NULL; 28 | smallAns.tail=smallAns.tail->next; 29 | return smallAns; 30 | } 31 | 32 | Node* reverse_LL(Node*head){ 33 | Pair Ans; 34 | Ans=reverse_ll(head); 35 | return Ans.head; 36 | } 37 | 38 | Node* takeInput() 39 | { 40 | int data; 41 | cin>>data; 42 | Node* tail; 43 | Node*head=NULL; 44 | while(data!=-1) 45 | { 46 | Node*newNode=new Node(data); 47 | if(head==NULL) 48 | { 49 | head=newNode; 50 | tail=newNode; 51 | } 52 | else{ 53 | tail->next=newNode; 54 | tail=tail->next; 55 | } 56 | cin>>data; 57 | } 58 | return head; 59 | } 60 | void Insert(Node* &head,int i,int data) 61 | { 62 | Node*newNode=new Node(data); 63 | int count=0; 64 | Node*temp=head; 65 | while(countnext!=NULL) 66 | { 67 | temp=temp->next;count++; 68 | 69 | } 70 | if(i==0){ 71 | newNode->next=head; 72 | head=newNode; 73 | 74 | 75 | 76 | } 77 | newNode->next=temp->next; 78 | temp->next=newNode; 79 | 80 | 81 | } 82 | 83 | void print(Node* head){ 84 | while(head!=NULL) 85 | { 86 | cout<data; 87 | if(head->next!=NULL) 88 | cout<<"->"; 89 | head=head->next; 90 | } 91 | cout<data=data; 12 | this->next=NULL; 13 | } 14 | 15 | }; 16 | int Node::size1=0; 17 | -------------------------------------------------------------------------------- /Linklists/Practice/LL_Node.cpp: -------------------------------------------------------------------------------- 1 | ///template 2 | class Node{ 3 | public: 4 | int data; 5 | Node*next; 6 | 7 | Node(int d) 8 | { 9 | data=d; 10 | next=NULL; 11 | 12 | } 13 | 14 | 15 | }; 16 | -------------------------------------------------------------------------------- /Linklists/Practice/LL_class_use.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include"LL_Node.cpp" 4 | int length_ll(Node*head) 5 | { 6 | int count=0; 7 | while(head!=NULL) 8 | { 9 | count++; 10 | head=head->next; 11 | 12 | } 13 | return count; 14 | } 15 | 16 | void print(Node*head) 17 | { 18 | while(head!=NULL) 19 | { 20 | cout<data<<" "; 21 | head=head->next; 22 | 23 | } 24 | cout<>data; Node*head=NULL;Node*tail=NULL; 31 | while(data!=-1) 32 | { 33 | Node*n=new Node(data); 34 | if(head==NULL) 35 | { 36 | head=tail=n; 37 | } 38 | else{ 39 | tail->next=n; 40 | tail=n; 41 | } 42 | cin>>data; 43 | } 44 | 45 | return head; 46 | } 47 | Node* InsertAtI(Node* head,int data,int index) 48 | { 49 | Node*n=new Node(data);Node*temp2=NULL; 50 | if(head==NULL) 51 | { 52 | if(index==0) head=n;return head; 53 | } 54 | else if(length_ll(head)next; 60 | 61 | } 62 | if(temp!=NULL) {temp2=temp->next;} 63 | temp->next=n; 64 | n->next=temp2; 65 | 66 | } 67 | 68 | return head; 69 | } 70 | 71 | int main() 72 | { 73 | Node*head; 74 | head=takeInput(); 75 | print(head); 76 | head=InsertAtI(head,99,4); 77 | print(head); 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Linklists/Practice/LL_class_use.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Linklists/Practice/LL_class_use.exe -------------------------------------------------------------------------------- /Linklists/Practice/LL_class_use.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Linklists/Practice/LL_class_use.o -------------------------------------------------------------------------------- /Linklists/Practice/temp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main ( ) { 4 | 5 | long long int t; 6 | scanf("%lld" , &t); 7 | 8 | while ( t-- ) { 9 | 10 | long long int a3 , al3 , sum; 11 | scanf("%lld%lld%lld" , &a3 , &al3 , &sum ); 12 | 13 | long long int n = ( 2 * sum ) / ( a3 + al3 ) ; 14 | printf("%lld\n" , n); 15 | 16 | long long int d = (al3 - a3)/(n-5); 17 | long long int a1 = a3 - 2 * d; 18 | int i; 19 | printf("%lld " , a1); 20 | for ( i = 1 ; i < n ; i++ ) { 21 | a1 += d; 22 | printf("%lld " , a1); 23 | } 24 | printf("\n"); 25 | 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Linklists/Practice/temp.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Linklists/Practice/temp.exe -------------------------------------------------------------------------------- /Linklists/Practice/temp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Linklists/Practice/temp.o -------------------------------------------------------------------------------- /Linklists/ReverseLL.cpp: -------------------------------------------------------------------------------- 1 | // Reverse LL (Iterative) 2 | 3 | #include 4 | class node{ 5 | public: 6 | int data; 7 | node * next; 8 | node(int data){ 9 | this->data=data; 10 | this->next=NULL; 11 | } 12 | }; 13 | using namespace std; 14 | node* takeinput(){ 15 | int data; 16 | cin>>data; 17 | node* head=NULL,*tail=NULL; 18 | while(data!=-1){ 19 | node *newnode=new node(data); 20 | if(head==NULL) { 21 | head=newnode; 22 | tail=newnode; 23 | } 24 | else{ 25 | tail->next=newnode; 26 | tail=newnode; 27 | } 28 | cin>>data; 29 | } 30 | return head; 31 | } 32 | void print(node *head) 33 | { 34 | node*temp=head; 35 | while(temp!=NULL) 36 | { 37 | cout<data<<" "; 38 | temp=temp->next; 39 | } 40 | cout<next; 51 | curr->next = prev; 52 | prev = curr; 53 | curr = n; 54 | } 55 | 56 | return prev; 57 | } 58 | int main(){ 59 | node* head=takeinput(); 60 | head=rev_linkedlist_itr(head); 61 | print(head); 62 | return 0; 63 | } 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /Linklists/ReverseLL_rec.cpp: -------------------------------------------------------------------------------- 1 | // Reverse Linked List Recursively 2 | #include 3 | class Node 4 | { 5 | public: 6 | int data; 7 | Node *next; 8 | Node(int data) 9 | { 10 | this->data = data; 11 | this->next = NULL; 12 | } 13 | }; 14 | 15 | using namespace std; 16 | Node *takeinput() 17 | { 18 | int data; 19 | cin >> data; 20 | Node *head = NULL, *tail = NULL; 21 | while (data != -1) 22 | { 23 | Node *newnode = new Node(data); 24 | if (head == NULL) 25 | { 26 | head = newnode; 27 | tail = newnode; 28 | } 29 | else 30 | { 31 | tail->next = newnode; 32 | tail = newnode; 33 | } 34 | cin >> data; 35 | } 36 | return head; 37 | } 38 | 39 | void print(Node *head) 40 | { 41 | Node *temp = head; 42 | while (temp != NULL) 43 | { 44 | cout << temp->data << " "; 45 | temp = temp->next; 46 | } 47 | cout << endl; 48 | } 49 | node *reverse_linked_list_rec(node *head) 50 | { 51 | //write your recursive code here 52 | node *temp = head; 53 | 54 | if(head->next == NULL){ 55 | return head; 56 | } 57 | 58 | node *smallAns = reverse_linked_list_rec(temp->next); 59 | 60 | temp->next = smallAns; 61 | 62 | return temp; 63 | } 64 | int main() 65 | { 66 | int t; 67 | cin >> t; 68 | 69 | while(t--) 70 | { 71 | Node *head = takeinput(); 72 | head = reverse_linked_list_rec(head); 73 | print(head); 74 | } 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /New Text Document.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/New Text Document.txt -------------------------------------------------------------------------------- /Priority Queues/InBuilt_PriorityQueues.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | vector sortedArray; 6 | int main() 7 | { 8 | priority_queue myPQ; 9 | myPQ.push(10); 10 | myPQ.push(60); 11 | myPQ.push(20); 12 | myPQ.push(50); 13 | myPQ.push(100); 14 | myPQ.push(40); 15 | 16 | while(!myPQ.empty()) 17 | { 18 | cout<>numElements; 27 | int *arr=new int[numElements]; 28 | for(int i=0;i>arr[i]; 31 | 32 | } 33 | int k;cin>>k; 34 | priority_queue pq; 35 | if(k==0){ 36 | cout<<"The array must be sorted: "< 2 | #include 3 | #include 4 | 5 | class PriorityQueue{ 6 | vector pq; 7 | public: 8 | ///constructor is not needed 9 | int getSize() 10 | { 11 | return pq.size(); 12 | } 13 | bool isEmpty() 14 | { 15 | return pq.size()==0; 16 | } 17 | int getMin() 18 | { 19 | if(isEmpty()) return 0; 20 | else return pq[0]; 21 | 22 | } 23 | void Insert(int value) 24 | { 25 | pq.push_back(value); 26 | int index=pq.size()-1; 27 | while(index>0) 28 | { 29 | if((index-1)/2>=0&&pq[(index-1)/2]>value) 30 | { 31 | swap(pq[index],pq[(index-1)/2]); 32 | index=(index-1)/2; 33 | 34 | } 35 | else break; 36 | } 37 | } 38 | void Remove() 39 | { 40 | //int vectorSize=v.size(); 41 | if(pq.size()==0) return; 42 | cout<<"here 1"<=pq.size()) return ; 50 | if(2*index+1=pq.size()) 51 | { 52 | if(pq[2*index+1] 2 | using namespace std; 3 | #include"PriorityQueueClass.cpp" 4 | 5 | int main() 6 | { 7 | PriorityQueue pq; 8 | pq.Insert(2); 9 | pq.Insert(8); 10 | pq.Insert(6); 11 | pq.Insert(5); 12 | pq.Insert(11); 13 | pq.Insert(7); 14 | pq.Remove(); 15 | 16 | cout< 3 | class Queue{ 4 | 5 | T *data; 6 | int firstIndex; 7 | int nextIndex; 8 | int Size; 9 | int totalSize; 10 | public: 11 | Queue() 12 | { 13 | // if(n==0) return; 14 | this->data=new T[5]; 15 | firstIndex=-1; 16 | nextIndex=0;Size=0;totalSize=5; 17 | 18 | 19 | } 20 | void enqueue(T n) 21 | { 22 | ///cout<<"In enqueue"< 2 | using namespace std; 3 | #include"Queue_Class_T.cpp" 4 | 5 | int main() 6 | { 7 | Queue q; 8 | 9 | for(int i='a';i<'z';i++) 10 | { 11 | q.enqueue(i); 12 | 13 | } 14 | for(int i=0;i<10;i++) 15 | { 16 | ///cout< 2 | #include 3 | using namespace std; 4 | 5 | int operation(int val1,int val2,char ch) 6 | { 7 | if(ch=='+') 8 | { 9 | return val1+val2; 10 | } 11 | else if(ch=='-') 12 | { 13 | return val1-val2; 14 | } 15 | else if(ch=='*') 16 | { 17 | return val1*val2; 18 | } 19 | else 20 | { 21 | return val1/val2; 22 | } 23 | } 24 | 25 | void prefixConversion(string s) 26 | { 27 | stack val; 28 | stack in; 29 | stack post; 30 | 31 | for(int i=s.size()-1;i>=0;i--) 32 | { 33 | char ch=s[i]; 34 | if(ch=='+'|| ch=='-' ||ch=='*'|| ch=='/') 35 | { 36 | int val1=val.top(); 37 | val.pop(); 38 | int val2=val.top(); 39 | val.pop(); 40 | int ans=operation(val1,val2,ch); 41 | val.push(ans); 42 | 43 | string in1=in.top(); 44 | in.pop(); 45 | string in2=in.top(); 46 | in.pop(); 47 | string inAns='('+in1+ch+in2+')'; 48 | in.push(inAns); 49 | 50 | string post1=post.top(); 51 | post.pop(); 52 | string post2=post.top(); 53 | post.pop(); 54 | string postAns=post1+post2+ch; 55 | post.push(postAns); 56 | } 57 | else if(ch==' ') 58 | { 59 | continue; 60 | } 61 | else 62 | { 63 | val.push(ch-'0'); 64 | in.push(string(1,ch)); 65 | post.push(string(1,ch)); 66 | } 67 | } 68 | 69 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int celeb(int n,int **ar) 6 | { 7 | stacks; 8 | for(int i=0;i=2) 14 | { 15 | int i=s.top(); 16 | s.pop(); 17 | int j=s.top(); 18 | s.pop(); 19 | if(ar[i][j]==1) //i knows j --> i can not be celeb. 20 | { 21 | s.push(j); 22 | } 23 | else 24 | { //i does not know j --> j can not be celeb. 25 | s.push(i); 26 | } 27 | } 28 | 29 | int pot=s.top(); 30 | s.pop(); 31 | 32 | for(int i=0;i>n; 48 | int **ar=new int*[n]; 49 | for(int i=0;i>ar[i][j]; 55 | } 56 | } 57 | int ans=celeb(n,ar); 58 | if(ans==0) 59 | { 60 | cout<<"none"< 2 | #include 3 | using namespace std; 4 | 5 | bool Duplicate(string ch) 6 | { 7 | stack s; 8 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | bool balancedBracket(string ch) 6 | { 7 | stack s; 8 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int* ngeor(int n,int *ar,int *ans) 6 | { 7 | stacks; 8 | ans[n-1]=-1; 9 | s.push(ar[n-1]); 10 | for(int i=n-2;i>=0;i--) 11 | { 12 | while(s.size()>0 && s.top()<=ar[i]) 13 | { 14 | s.pop(); 15 | } 16 | if(s.size()==0) 17 | { 18 | ans[i]=-1; 19 | } 20 | else 21 | { 22 | ans[i]=s.top(); 23 | } 24 | s.push(ar[i]); 25 | } 26 | return ans; 27 | } 28 | int main() 29 | { 30 | int n; 31 | cin>>n; 32 | int *ar=new int[n]; 33 | for(int i=0;i>ar[i]; 36 | } 37 | int *ans=new int[n]; 38 | ans=ngeor(n,ar,ans); 39 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int* ngeor(int n,int *ar,int *ans) 6 | { 7 | stack s; 8 | s.push(0); 9 | for(int i=1;i0&&ar[i]>ar[s.top()]) 12 | { 13 | ans[s.top()]=ar[i]; 14 | s.pop(); 15 | } 16 | s.push(i); 17 | } 18 | while(s.size()!=0) 19 | { 20 | ans[s.top()]=-1; 21 | s.pop(); 22 | } 23 | return ans; 24 | } 25 | int main() 26 | { 27 | int n; 28 | cin>>n; 29 | int *ar=new int[n]; 30 | for(int i=0;i>ar[i]; 33 | } 34 | int *ans=new int[n]; 35 | ans=ngeor(n,ar,ans); 36 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int* nseor(int n,int *ar,int *ans) 6 | { 7 | stack s; 8 | ans[n-1]=-1; 9 | s.push(ar[n-1]); 10 | for(int i=n-2;i>=0;i--) 11 | { 12 | while(s.size()>0 && s.top()>=ar[i]) 13 | { 14 | s.pop(); 15 | } 16 | if(s.size()==0) 17 | { 18 | ans[i]=-1; 19 | } 20 | else 21 | { 22 | ans[i]=s.top(); 23 | } 24 | s.push(ar[i]); 25 | 26 | } 27 | 28 | return ans; 29 | } 30 | int main() 31 | { 32 | int n; 33 | cin>>n; 34 | int *ar=new int[n]; 35 | for(int i=0;i>ar[i]; 38 | } 39 | int *ans=new int[n]; 40 | ans=nseor(n,ar,ans); 41 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int* nseor(int n,int *ar,int *ans) 6 | { 7 | stack s; 8 | s.push(0); 9 | for(int i=1;i0 && ar[i]>n; 30 | int *ar=new int[n]; 31 | for(int i=0;i>ar[i]; 34 | } 35 | int *ans=new int[n]; 36 | ans=nseor(n,ar,ans); 37 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int* ngeol(int n,int* ar,int *ans) 6 | { 7 | ans[0]=-1; 8 | stack s; 9 | s.push(n-1); 10 | for(int i=n-2;i>=0;i--) 11 | { 12 | while(s.size()>0 && ar[i]>ar[s.top()]) 13 | { 14 | ans[s.top()]=ar[i]; 15 | s.pop(); 16 | } 17 | s.push(i); 18 | } 19 | while(s.size()!=0) 20 | { 21 | ans[s.top()]=-1; 22 | s.pop(); 23 | } 24 | return ans; 25 | } 26 | int main() 27 | { 28 | int n; 29 | cin>>n; 30 | int *ar=new int[n]; 31 | for(int i=0;i>ar[i]; 34 | } 35 | int *ans=new int[n]; 36 | ans=ngeol(n,ar,ans); 37 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int* ngeol(int n,int *ar,int *ans) 6 | { 7 | ans[0]=-1; 8 | stack s; 9 | s.push(0); 10 | for(int i=1;i0 && ar[i]>ar[s.top()]) 13 | { 14 | s.pop(); 15 | } 16 | 17 | if(s.size()==0) 18 | { 19 | ans[i]=-1; 20 | } 21 | else 22 | { 23 | ans[i]=ar[s.top()]; 24 | } 25 | s.push(i); 26 | } 27 | return ans; 28 | } 29 | int main() 30 | { 31 | int n; 32 | cin>>n; 33 | int *ar=new int[n]; 34 | for(int i=0;i>ar[i]; 37 | } 38 | int *ans=new int[n]; 39 | ans=ngeol(n,ar,ans); 40 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int* stockSpan(int n,int* ar,int* ans) 6 | { 7 | ans[0]=1; 8 | stack s; 9 | s.push(0); 10 | for(int i=1;i0 && ar[i]>ar[s.top()]) 13 | { 14 | s.pop(); 15 | } 16 | if(s.size()==0) 17 | { 18 | ans[i]=i+1; 19 | } 20 | else 21 | { 22 | ans[i]=i-s.top(); 23 | } 24 | s.push(i); 25 | } 26 | return ans; 27 | } 28 | int main() 29 | { 30 | int n; 31 | cin>>n; 32 | int *ar=new int[n]; 33 | for(int i=0;i>ar[i]; 36 | } 37 | int *ans=new int[n]; 38 | ans=stockSpan(n,ar,ans); 39 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int largestAreaUnderHistogram(int n,int *ar,int *lb,int *rb) 6 | { 7 | stack s; 8 | rb[n-1]=n; 9 | s.push(n-1); 10 | for(int i=n-2;i>=0;i--) 11 | { 12 | while(s.size()>0 && ar[i]<=ar[s.top()]) 13 | { 14 | s.pop(); 15 | } 16 | if(s.size()==0) 17 | { 18 | rb[i]=n; 19 | } 20 | else 21 | { 22 | rb[i]=s.top(); 23 | } 24 | s.push(i); 25 | } 26 | while(s.size()!=0) 27 | { 28 | s.pop(); 29 | } 30 | 31 | lb[0]=-1; 32 | s.push(0); 33 | for(int i=1;i0 && ar[i]<=ar[s.top()]) 36 | { 37 | s.pop(); 38 | } 39 | if(s.size()==0) 40 | { 41 | lb[i]=-1; 42 | } 43 | else 44 | { 45 | lb[i]=s.top(); 46 | } 47 | s.push(i); 48 | } 49 | int maxArea=-1; 50 | 51 | for(int i=0;imaxArea) 56 | { 57 | maxArea=area; 58 | } 59 | } 60 | cout<>n; 67 | int *ar=new int[n]; 68 | for(int i=0;i>ar[i]; 71 | } 72 | int *lb=new int[n]; //stores left smallest element 73 | int *rb=new int[n]; //stores right smallest element 74 | largestAreaUnderHistogram(n,ar,lb,rb); 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Stacks/6slidingWindowMaximum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void slidingWindowMaximum(int n,int k,int *ar,int *ans) 6 | { 7 | ans[n-1]=n; 8 | stack s; 9 | s.push(n-1); 10 | 11 | for(int i=n-2;i>=0;i--) 12 | { 13 | while(s.size()>0 && ar[i]>=ar[s.top()]) 14 | { 15 | s.pop(); 16 | } 17 | 18 | if(s.size()==0) 19 | { 20 | ans[i]=n; 21 | 22 | } 23 | else 24 | { 25 | ans[i]=s.top(); 26 | } 27 | s.push(i); 28 | } 29 | int j=0; 30 | for(int i=0;i<=n-k;i++) 31 | { 32 | if(j>n; 47 | int k; 48 | cin>>k; 49 | 50 | int *ar=new int[n]; 51 | for(int i=0;i>ar[i]; 54 | } 55 | int *ans=new int[n]; 56 | slidingWindowMaximum(n,k,ar,ans); 57 | } 58 | -------------------------------------------------------------------------------- /Stacks/7InfixEvaluation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int precedence(char oprtr) 6 | { 7 | if(oprtr=='+') 8 | { 9 | return 1; 10 | } 11 | else if(oprtr=='-') 12 | { 13 | return 1; 14 | } 15 | else if(oprtr=='*') 16 | { 17 | return 2; 18 | } 19 | else 20 | { 21 | return 2; 22 | } 23 | } 24 | 25 | int operation(int v1,int v2,char oprtr) 26 | { 27 | if(oprtr=='+') 28 | { 29 | return v1+v2; 30 | } 31 | else if(oprtr=='-') 32 | { 33 | return v1-v2; 34 | } 35 | else if(oprtr=='*') 36 | { 37 | return v1*v2; 38 | } 39 | else 40 | { 41 | return v1/v2; 42 | } 43 | } 44 | 45 | int infixEval(string s) 46 | { 47 | stack op; 48 | stack opand; 49 | for(int i=0;i0 && op.top()!='(' && precedence(ch)<=precedence(op.top())) 78 | { 79 | char oprtr=op.top(); 80 | op.pop(); 81 | int val2=opand.top(); 82 | opand.pop(); 83 | int val1=opand.top(); 84 | opand.pop(); 85 | int ans=operation(val1,val2,oprtr); 86 | opand.push(ans); 87 | } 88 | op.push(ch); 89 | } 90 | else 91 | { 92 | continue; 93 | } 94 | } 95 | while(op.size()!=0) 96 | { 97 | char oprtr=op.top(); 98 | op.pop(); 99 | int val2=opand.top(); 100 | opand.pop(); 101 | int val1=opand.top(); 102 | opand.pop(); 103 | int ans=operation(val1,val2,oprtr); 104 | opand.push(ans); 105 | } 106 | return opand.top(); 107 | } 108 | 109 | int main() 110 | { 111 | string s; 112 | getline(cin,s); 113 | int ans=infixEval(s); 114 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int precedence(char ch) 6 | { 7 | if(ch=='+') 8 | { 9 | return 1; 10 | } 11 | else if(ch=='-') 12 | { 13 | return 1; 14 | } 15 | else if(ch=='*') 16 | { 17 | return 2; 18 | } 19 | else 20 | { 21 | return 2; 22 | } 23 | } 24 | void infixConversion(string s) 25 | { 26 | stackoprtr; 27 | stackpre; 28 | stackpost; 29 | for(int i=0;i='1' && ch<='9') || (ch>='a' && ch<='z' ) || (ch>='A' && ch<='Z')) 37 | { 38 | pre.push(string(1,ch)); 39 | post.push(string(1,ch)); 40 | } 41 | else if(ch==')') 42 | { 43 | while(oprtr.top()!='(') 44 | { 45 | char op=oprtr.top(); 46 | oprtr.pop(); 47 | string val2=pre.top(); 48 | pre.pop(); 49 | string val1=pre.top(); 50 | pre.pop(); 51 | string val2Post=post.top(); 52 | post.pop(); 53 | string val1Post=post.top(); 54 | post.pop(); 55 | 56 | string prefix=op+val1+val2; 57 | string postfix=val1Post+val2Post+op; 58 | pre.push(prefix); 59 | post.push(postfix); 60 | } 61 | oprtr.pop(); 62 | } 63 | else 64 | { 65 | if(oprtr.size()>0 && oprtr.top()!='(' && precedence(ch)<=precedence(oprtr.top())) 66 | { 67 | char op=oprtr.top(); 68 | oprtr.pop(); 69 | string val2=pre.top(); 70 | pre.pop(); 71 | string val1=pre.top(); 72 | pre.pop(); 73 | string val2Post=post.top(); 74 | post.pop(); 75 | string val1Post=post.top(); 76 | post.pop(); 77 | 78 | string prefix=op+val1+val2; 79 | string postfix=val1Post+val2Post+op; 80 | pre.push(prefix); 81 | post.push(postfix); 82 | } 83 | oprtr.push(ch); 84 | } 85 | } 86 | while(oprtr.size()!=0) 87 | { 88 | char op=oprtr.top(); 89 | oprtr.pop(); 90 | string val2=pre.top(); 91 | pre.pop(); 92 | string val1=pre.top(); 93 | pre.pop(); 94 | string val2Post=post.top(); 95 | post.pop(); 96 | string val1Post=post.top(); 97 | post.pop(); 98 | 99 | string prefix=op+val1+val2; 100 | string postfix=val1Post+val2Post+op; 101 | pre.push(prefix); 102 | post.push(postfix); 103 | } 104 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int operation(int val1,int val2,char ch) 6 | { 7 | if(ch=='+') 8 | { 9 | return val1+val2; 10 | } 11 | else if(ch=='-') 12 | { 13 | return val1-val2; 14 | } 15 | else if(ch=='*') 16 | { 17 | return val1*val2; 18 | } 19 | else 20 | { 21 | return val1/val2; 22 | } 23 | } 24 | void postfixConversion(string s) 25 | { 26 | stack val; 27 | stack in; 28 | stack pre; 29 | 30 | for(int i=0;i0) 20 | nextIndex--; 21 | } 22 | int top() const 23 | { 24 | if(nextIndex>0) return data[nextIndex-1]; 25 | else return -2131233; 26 | } 27 | int size() const{return nextIndex;} 28 | bool isempty() const{ 29 | return (nextIndex==0)? true: false; 30 | 31 | } 32 | ~StackUsingArrays() 33 | { 34 | delete[]data; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Stacks/Dynamic_Stack_Class.cpp: -------------------------------------------------------------------------------- 1 | template 2 | 3 | class StackUsingArrays{ 4 | T *data; 5 | int nextIndex,capacity; 6 | public: 7 | StackUsingArrays() 8 | { 9 | data=new T[5]; 10 | nextIndex=0; 11 | capacity=5; 12 | } 13 | void push(T n) 14 | { 15 | if(nextIndex0) 48 | nextIndex--; 49 | } 50 | T top() const 51 | { 52 | if(nextIndex>0) return data[nextIndex-1]; 53 | else{ 54 | 55 | cout<<"Stack is empty"< 2 | using namespace std; 3 | #include"Dynamic_Stack_Class.cpp" 4 | #include 5 | int main() 6 | {-- 7 | StackUsingArrays s; 8 | for(int i=0;i<10;i++) 9 | { 10 | s.push("hey"); 11 | } 12 | 13 | ///s.push(2); 14 | while(!s.isempty()) 15 | { 16 | cout<< s.top()<<" "; 17 | s.pop(); 18 | } 19 | return 0; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Stacks/StackClassOfArraysUse.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Stacks/StackClassOfArraysUse.exe -------------------------------------------------------------------------------- /Stacks/StackClassOfArraysUse.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Stacks/StackClassOfArraysUse.o -------------------------------------------------------------------------------- /Stacks/Stack_LL.h: -------------------------------------------------------------------------------- 1 | template 2 | class Node{ 3 | public: 4 | T data; 5 | Node*next; 6 | Node(T d) 7 | { 8 | data=d; 9 | next=NULL; 10 | 11 | } 12 | 13 | }; 14 | 15 | template 16 | class StackUsingLL{ 17 | 18 | private: 19 | Node*head; 20 | int Size; 21 | public: 22 | StackUsingLL() 23 | { 24 | head=NULL; 25 | Size=0; 26 | 27 | } 28 | int getSize() 29 | { 30 | return this->Size; 31 | } 32 | bool isempty(){ 33 | return Size==0; 34 | } 35 | 36 | T top() 37 | { 38 | if(head==NULL){ 39 | cout<<"Stack is empty"<data; 44 | 45 | } 46 | 47 | } 48 | void push(T data) 49 | { 50 | Size++;Node*newNode=new Node(data); 51 | if(head==NULL){ 52 | 53 | head=newNode; 54 | 55 | } 56 | else{ 57 | newNode->next=head; 58 | head=newNode; 59 | } 60 | return; 61 | 62 | } 63 | void pop() 64 | { 65 | if(head==NULL) return; 66 | else{ 67 | Node*temp=head; 68 | head=head->next; 69 | delete temp; 70 | Size--; 71 | 72 | } 73 | 74 | 75 | } 76 | }; 77 | -------------------------------------------------------------------------------- /Stacks/Stack_LL_use.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include"Stack_LL.h" 4 | 5 | int main() 6 | { 7 | StackUsingLL s; 8 | for(int i=0;i<10;i++) 9 | { 10 | 11 | s.push(i+1); 12 | } 13 | 14 | for(int i=0;i<10;i++) 15 | { 16 | 17 | cout< 3 | 4 | class Pair{ 5 | T x; 6 | V y; 7 | public: 8 | T getX()const{ 9 | return x; 10 | } 11 | V getY()const 12 | { 13 | return y; 14 | } 15 | void setX(T x) 16 | { 17 | this->x=x; 18 | 19 | } 20 | void setY(V y) 21 | { 22 | this->y=y; 23 | 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Templates/Template_Class_Use.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include"Class_Templates.cpp" 4 | #include 5 | 6 | int main() 7 | { 8 | //Pair p;p.setX(10);p.setY(20); 9 | Pair p; 10 | p.setX(10);p.setY("Daum, did i just do that?"); 11 | Pair,char> p2; 12 | p2.setY('D'); 13 | p2.setX( p); 14 | cout<<"P is : "; 15 | cout<,pair> 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Templates/Template_Class_Use.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Templates/Template_Class_Use.exe -------------------------------------------------------------------------------- /Templates/Template_Class_Use.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Templates/Template_Class_Use.o -------------------------------------------------------------------------------- /Trees/TreeNode.h: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | class TreeNode{ 5 | public: 6 | T data; 7 | vector*> children; 8 | TreeNode(T d){ 9 | data=d; 10 | } 11 | 12 | 13 | }; 14 | -------------------------------------------------------------------------------- /Trees/TreeNode1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | class TreeNode{ 5 | public: 6 | 7 | T data; 8 | vector*> children; 9 | TreeNode(T d){ 10 | data=d; 11 | } 12 | 13 | 14 | }; 15 | -------------------------------------------------------------------------------- /Trees/TreeNode_Use.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"TreeNode.h" 3 | using namespace std; 4 | #include 5 | #define LL long long int 6 | #define FOR(a,b,c) for (int (a)=(b); (a)<(c); (a)++) 7 | #define FORN(a,b,c) for (int (a)=(b); (a)<=(c); (a)++) 8 | #define FORD(a,b,c) for (int (a)=(b); (a)>=(c); (a)--) 9 | #define REP(i,n) FOR(i,0,n) 10 | #define REPN(i,n) FORN(i,1,n) 11 | #define REPD(i,n) FORD(i,n,1) 12 | 13 | 14 | TreeNode* takeInput() 15 | { 16 | int data; 17 | cout<<"Enter data : "<>data; 19 | TreeNode* root=new TreeNode (data); 20 | int n; 21 | cout<<"enter number of children of : "<>n; 23 | for(int i=0;i*child=takeInput(); 26 | root->children.push_back(child); 27 | 28 | 29 | } 30 | return root; 31 | } 32 | 33 | void levelOrderPrint(TreeNode* root) 34 | { 35 | 36 | if(root==NULL) return ; 37 | cout<data<<" : "; 38 | for(int i=0;ichildren.size();i++) 39 | { 40 | if(i!=root->children.size()-1) 41 | cout<children[i]->data<< ","; 42 | else{ 43 | 44 | cout<children[i]->data; 45 | } 46 | } 47 | cout<children.size();i++) 49 | levelOrderPrint(root->children[i]); 50 | } 51 | 52 | void printTree(TreeNode* root){ 53 | 54 | if(root==NULL) return; 55 | 56 | cout<data<children.size();i++) 58 | { 59 | printTree(root->children[i]); 60 | } 61 | 62 | } 63 | 64 | TreeNode* takeInputLevelWise() 65 | { 66 | int rootData; 67 | // cout<<"Enter root data : "<>rootData; 69 | TreeNode* root=new TreeNode (rootData); 70 | queue *> q; 71 | q.push(root); 72 | while(!q.empty()) 73 | { 74 | TreeNode*temp=q.front(); 75 | q.pop(); 76 | int n; 77 | // cout<<"Enter number of children of "<data<<" : "; 78 | cin>>n; 79 | REP(i,n){ 80 | int data; 81 | // cout<<"Enter data : "; 82 | cin>>data; 83 | TreeNode* child=new TreeNode(data); 84 | q.push(child); 85 | temp->children.push_back(child); 86 | } 87 | 88 | } 89 | return root; 90 | 91 | } 92 | 93 | void printLevelWise(TreeNode* root) 94 | { 95 | if(root==NULL) return ; 96 | queue *> q; 97 | int sum=0; 98 | q.push(root); 99 | while(!q.empty()) 100 | { 101 | TreeNode*temp=q.front(); 102 | q.pop(); 103 | ///cout<data<<":"; 104 | sum+=temp->data; 105 | REP(i,temp->children.size()) 106 | { 107 | 108 | q.push(temp->children[i]); 109 | } 110 | ///cout<*root=new TreeNode(10); 117 | TreeNode *n1=new TreeNode(20); 118 | TreeNode*n2=new TreeNode(30); 119 | root->children.push_back(n1); 120 | root->children.push_back(n2); 121 | ///cout<data<<" " <children[0]->data<<" "<children[1]->data<<" "<>ch;int n; 129 | if(ch=='y'||ch=='Y') 130 | { 131 | cin>>n; 132 | TreeNode*root=takeInputLevelWise(); 133 | printLevelWise(root); 134 | 135 | 136 | } 137 | return 0; 138 | 139 | } 140 | -------------------------------------------------------------------------------- /Trees/TreeNode_Use.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Trees/TreeNode_Use.exe -------------------------------------------------------------------------------- /Trees/TreeNode_Use.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashishgupta1350/DataStructures_Algorithms_Coding_Ninjas/012cbe25c2595fa55202c71d451a9bf33903c642/Trees/TreeNode_Use.o -------------------------------------------------------------------------------- /Tries/Trie.h: -------------------------------------------------------------------------------- 1 | #include"TrieNodeClass.h" 2 | #include 3 | class Trie{ 4 | private: 5 | TrieNode *root; 6 | int numWords; 7 | public: 8 | Trie(){ 9 | root=NULL; 10 | numWords=0; 11 | 12 | } 13 | private: 14 | void insertHelper(TrieNode*root,string word) 15 | { 16 | /*char ch=word[0]; 17 | if(ch=='\0') 18 | { 19 | root->isEnd=true; 20 | return; 21 | 22 | } 23 | 24 | if(root->children[ch-'a']==NULL) 25 | { 26 | TrieNode*newNode=new TrieNode(ch); 27 | root->children[ch-'a']=newNode; 28 | } 29 | insertHelper(root->children[ch-'a'],word.substr(1)); 30 | 31 | return;*/ 32 | ///base case 33 | if(word.length()==0) 34 | { 35 | root->isEnd=true; 36 | return; 37 | 38 | } 39 | char chToInsert=word[0]; 40 | int index=ch-'a'; 41 | TrieNode*child; 42 | if(root->children[index]!=NULL) 43 | { 44 | child=root->children[index]; 45 | } 46 | else{ 47 | 48 | child=new TrieNode(chToInsert); 49 | root->children[index]=child; 50 | } 51 | ///recursive call 52 | 53 | insertHelper(child,word.substr(1)); 54 | return; 55 | 56 | } 57 | bool searchWord(TrieNode*root,string word){ 58 | ///base case 59 | if(word.length()==0) 60 | { 61 | if(root->isEnd==true) return true; 62 | else return false; 63 | } 64 | /// small calculation 65 | char firstCharacter=word[0]; 66 | TrieNode*child; 67 | int index=firstCharacter-'a'; 68 | child=root->children[index]; 69 | if(child==NULL) 70 | { 71 | return false; 72 | 73 | } 74 | else{ 75 | ///recursive call 76 | return searchWord(child,word.substr(1)); 77 | } 78 | } 79 | TrieNode*removeWord(TrieNode*root,string word) 80 | { 81 | if(word.length()==0){ 82 | root->isEnd=false; 83 | for(int i=0;i<26;i++) 84 | { 85 | if(root->children[i]!=NULL) 86 | { 87 | return root; 88 | } 89 | } 90 | delete root;root=NULL; 91 | return root; 92 | } 93 | int index=word[0]-'a'; 94 | TrieNode*child=root->children[index]; 95 | if(child==NULL) 96 | { 97 | return root; 98 | } 99 | else{ 100 | 101 | root->children[index]=removeWord(child,word.substr(1)); 102 | return root; 103 | } 104 | } 105 | public: 106 | 107 | void insertWord(string word){ 108 | ///word.lower(); 109 | numWords++; 110 | return insertHelper(root,word); 111 | 112 | } 113 | bool searchWord(string word){ 114 | return searchWord(root,word); 115 | } 116 | void removeWord(string word){ 117 | root= removeWord(root,word); 118 | 119 | 120 | } 121 | 122 | 123 | 124 | }; 125 | -------------------------------------------------------------------------------- /Tries/TrieNodeClass.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Tries/TrieNodeClass.h: -------------------------------------------------------------------------------- 1 | class TrieNode{ 2 | private: 3 | char data; 4 | TrieNode **children; 5 | bool isEnd; 6 | public: 7 | TrieNode() 8 | { 9 | children=new TrieNode* [26]; 10 | for(int i=0;i<26;i++) 11 | { 12 | children[i]=NULL; 13 | 14 | } 15 | isEnd=false; 16 | } 17 | TrieNode(char data) 18 | { 19 | this->data=data; 20 | children=new TrieNode*[26]; 21 | for(int i=0;i<26;i++) 22 | { 23 | children[i]=NULL; 24 | 25 | } 26 | isEnd=false; 27 | 28 | 29 | } 30 | 31 | }; 32 | 33 | --------------------------------------------------------------------------------