From 5f720da9f77045d5a0aab2de55780350df49e4d3 Mon Sep 17 00:00:00 2001 From: nkkumawat Date: Sun, 1 Oct 2017 13:26:16 +0530 Subject: [PATCH 1/2] added tree problem 'common nodes in to BSTs' --- .../common_nodes_in_2_BSTs/common_nodes.cpp | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 data_structures/common_nodes_in_2_BSTs/common_nodes.cpp diff --git a/data_structures/common_nodes_in_2_BSTs/common_nodes.cpp b/data_structures/common_nodes_in_2_BSTs/common_nodes.cpp new file mode 100644 index 0000000000..984310d261 --- /dev/null +++ b/data_structures/common_nodes_in_2_BSTs/common_nodes.cpp @@ -0,0 +1,149 @@ +// Iterative traversal based method to find common elements +// in two BSTs. +#include +#include +using namespace std; + +// A BST node +struct Node +{ + int key; + struct Node *left, *right; +}; + +// A utility function to create a new node +Node *newNode(int ele) +{ + Node *temp = new Node; + temp->key = ele; + temp->left = temp->right = NULL; + return temp; +} + +// Function two print common elements in given two trees +void printCommon(Node *root1, Node *root2) +{ + // Create two stacks for two inorder traversals + stack stack1, s1, s2; + + while (1) + { + // push the Nodes of first tree in stack s1 + if (root1) + { + s1.push(root1); + root1 = root1->left; + } + + // push the Nodes of second tree in stack s2 + else if (root2) + { + s2.push(root2); + root2 = root2->left; + } + + // Both root1 and root2 are NULL here + else if (!s1.empty() && !s2.empty()) + { + root1 = s1.top(); + root2 = s2.top(); + + // If current keys in two trees are same + if (root1->key == root2->key) + { + cout << root1->key << " "; + s1.pop(); + s2.pop(); + + // move to the inorder successor + root1 = root1->right; + root2 = root2->right; + } + + else if (root1->key < root2->key) + { + // If Node of first tree is smaller, than that of + // second tree, then its obvious that the inorder + // successors of current Node can have same value + // as that of the second tree Node. Thus, we pop + // from s2 + s1.pop(); + root1 = root1->right; + + // root2 is set to NULL, because we need + // new Nodes of tree 1 + root2 = NULL; + } + else if (root1->key > root2->key) + { + s2.pop(); + root2 = root2->right; + root1 = NULL; + } + } + + // Both roots and both stacks are empty + else break; + } +} + +// A utility function to do inorder traversal +void inorder(struct Node *root) +{ + if (root) + { + inorder(root->left); + cout<key<<" "; + inorder(root->right); + } +} + +/* A utility function to insert a new Node with given key in BST */ +struct Node* insert(struct Node* node, int key) +{ + /* If the tree is empty, return a new Node */ + if (node == NULL) return newNode(key); + + /* Otherwise, recur down the tree */ + if (key < node->key) + node->left = insert(node->left, key); + else if (key > node->key) + node->right = insert(node->right, key); + + /* return the (unchanged) Node pointer */ + return node; +} + +// Driver program +int main() +{ + // Create first tree as shown in example + Node *root1 = NULL; + root1 = insert(root1, 5); + root1 = insert(root1, 1); + root1 = insert(root1, 10); + root1 = insert(root1, 0); + root1 = insert(root1, 4); + root1 = insert(root1, 7); + root1 = insert(root1, 9); + + // Create second tree as shown in example + Node *root2 = NULL; + root2 = insert(root2, 10); + root2 = insert(root2, 7); + root2 = insert(root2, 20); + root2 = insert(root2, 4); + root2 = insert(root2, 9); + + cout << "Tree 1 : "; + inorder(root1); + cout << endl; + + cout << "Tree 2 : "; + inorder(root2); + + cout << "\nCommon Nodes: "; + printCommon(root1, root2); + + return 0; +} \ No newline at end of file From 966c3592cdf5e5d906a1cece99689273bd1917e2 Mon Sep 17 00:00:00 2001 From: nkkumawat Date: Mon, 2 Oct 2017 09:14:42 +0530 Subject: [PATCH 2/2] dynamic programming added --- .../gold_mine_dp/gold_mine_dp.cpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Dynamic Programing/gold_mine_dp/gold_mine_dp.cpp diff --git a/Dynamic Programing/gold_mine_dp/gold_mine_dp.cpp b/Dynamic Programing/gold_mine_dp/gold_mine_dp.cpp new file mode 100644 index 0000000000..c16df1ab64 --- /dev/null +++ b/Dynamic Programing/gold_mine_dp/gold_mine_dp.cpp @@ -0,0 +1,61 @@ +// C++ program to solve Gold Mine problem +#include +using namespace std; + +const int MAX = 100; + +// Returns maximum amount of gold that can be collected +// when journey started from first column and moves +// allowed are right, right-up and right-down +int getMaxGold(int gold[][MAX], int m, int n) +{ + // Create a table for storing intermediate results + // and initialize all cells to 0. The first row of + // goldMineTable gives the maximum gold that the miner + // can collect when starts that row + int goldTable[m][n]; + memset(goldTable, 0, sizeof(goldTable)); + + for (int col=n-1; col>=0; col--) + { + for (int row=0; row) + int right = (col==n-1)? 0: goldTable[row][col+1]; + + // Gold collected on going to the cell to right up (/) + int right_up = (row==0 || col==n-1)? 0: + goldTable[row-1][col+1]; + + // Gold collected on going to the cell to right down (\) + int right_down = (row==m-1 || col==n-1)? 0: + goldTable[row+1][col+1]; + + // Max gold collected from taking either of the + // above 3 paths + goldTable[row][col] = gold[row][col] + + max(right, max(right_up, right_down)); + ; + } + } + + // The max amount of gold collected will be the max + // value in first column of all rows + int res = goldTable[0][0]; + for (int i=1; i