my knowledge is limited, writing in C++ for 2 months
In this function string code is recursively decrements chars until the base case "" is found. I want to prune some paths before the base case is found, and for some string code a path to the base case will not be found. For the prune I want to compare an attribute in the path with parameter int time. This searches a trie made of 'nodeT'
struct charT {
char letter;
nodeT *next;
};
struct nodeT {
bool isOperation;
bool isCode;
int time;
Vector<charT> alpha;
};
nodeT *root
usage:
string code = "12345";
int time = convertToEpoch(20120815); //my epoch function
containsCode(code, time)
bool containsCode(string code, int time)
{
if(root == NULL) return false;
else return containsCodeHelper(root, code, time);
}
bool containsCodeHelper(nodeT *w, string code, int time)
{
if(code == "") //base case: all char found
return w->isCode;
else {
if (w->isOperation && w->time != time) return false; //case 2: time check OK <- at a midpoint in the path
for(int i = 0; i < w->alpha.size(); i++) { //Loop through the leaf
if (w->alpha[i].letter == code[0]) //case 3: leaf exists
return containsCodeHelper(w->alpha[i].next, code.substr(1), time);
}
}
return false; //if no path
}
This function worked well before adding the time check prune, it now loops, returns false if outside time but then starts again with the candidate string code from char location 0.
Questions: 1) Is a nested return false kicking the recursion back to the next call for loop, 2) should the time prune be placed in the for loop with a logical return false or return 'path', 3) is this more fundamentally messed-up and I need to learn a C++ concept <- please explain if yes.
Also, the posted function is a simplified version of the actual function - there is a modifier to time and a 'step over' path that I left out. In past question I found that these 'addons' distract from the question.
after some reworking it now functions fine - possibly it always did; I changed to read attribute return w->isCode to return true, that seemed to be the biggest issue - I will debug the trie constructor and see if it is setting the attribute at the end of each path.
boolcontainsCodeHelper(nodeT *w, string code, int time)
{
if(code == "") //base case: all char found
return true;
else {
if ( w->isOperation && (!((w->begin-wag) <= time && time <= (w->end+wag) ) && time != 9999 ) )
return false; //case 2: time
else {
for(int i = 0; i < w->alpha.size(); i++) { //Loop through all of the children of the current node
if (w->alpha[i].letter == word[0])
return containsCodeHelper(w->alpha[i].next, word.substr(1), time, wag);
else if (word[0] == 'ΕΎ') //step over '0' all subnodes
if (containsCodeHelper(w->alpha[i].next, word.substr(1), time, wag))
return true;
}
}
}
return false; //if char is missing - meaning the exact code is not there - terminates garbage subnode paths
}
I don't see any difference between having return false; at the end and leaving it out. Also still confused as why the special case needs if( bool fn()) return true; rather than just return ( bool fn()); i found that solution through trial and error with help from another stack overflow thread
Related
I am practicing this code(from LeetCode) to be better in C++. Unfortunately, I am unable to get 'find' to work correctly.
This code is used to search word from a vector of vector of type char (i.e. board) without visiting the same letter twice(visitedSoFar keeps a track of the x,y positions of the letters visitedSoFar).
A vector of class Node is used to store the positions visited so far.
Here is the code snippet I have written:
class Node{
private:
int x;
int y;
public:
Node(int a, int b):x(a),y(b){};
bool operator==(Node newNode){
if(this->x == newNode.x && this->y == newNode.y)
return true;
else
return false;
}
};
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
vector <Node> visitedSoFar;
for(int r =0; r< board.size(); r++){
for(int c=0; c<board[r].size(); c++){
if(board[r][c] == word.at(0)){
if(search(board, word, visitedSoFar, board[r].size(), r, c))
return true;
}
}
}
return false;
}
private:
bool search(vector<vector<char>>& board, string word, vector<Node>& visitedSoFar, int size, int r, int c){
Node newNode(r,c);
visitedSoFar.push_back(newNode);
if(word.size() == 1)
return true;
Node toSearch1(r-1,c);
if(r-1 >= 0 && find(visitedSoFar.begin(), visitedSoFar.end(), toSearch1) == visitedSoFar.end()){
if(board[r-1][c] == word.at(1))
if(search(board, word.substr(1), visitedSoFar, size, r-1, c))
return true;
}
Node toSearch2(r+1,c);
if(r+1 < size && find(visitedSoFar.begin(), visitedSoFar.end(), toSearch2) == visitedSoFar.end()){
if(board[r+1][c] == word.at(1))
if(search(board, word.substr(1), visitedSoFar, size, r+1, c))
return true;
}
Node toSearch3(r,c-1);
if(c-1 >= 0 && find(visitedSoFar.begin(), visitedSoFar.end(), toSearch3) == visitedSoFar.end()){
if(board[r][c-1] == word.at(1))
if(search(board, word.substr(1), visitedSoFar, size, r, c-1))
return true;
}
Node toSearch4(r,c+1);
if(c+1 < size && find(visitedSoFar.begin(), visitedSoFar.end(), toSearch4) == visitedSoFar.end()){
if(board[r][c+1] == word.at(1))
if(search(board, word.substr(1), visitedSoFar, size, r, c+1))
return true;
}
visitedSoFar.pop_back();
return false;
}
};
If I comment the find I get the correct output, but this would not work for all test cases.
Thank You.
Edit
In method search, corrected if statement to check against size for (r+1) and (c+1).
Edit
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
Edit
Design Error: The find operation should not be able to find(indicating that the node has not been visited so far) to then proceed with search in it. Hence altered find to be == visitedSoFar.end() rather than != visitedSoFar.end().
I think you should use a simpler design of your solution.
The idea behind checking every boardpoint is most likely unnecessary work, right? Using your approach you are constantly checking if the work has already been done. This checking includes a linear search through your board (every node will be saved at some time) for every search step. This means pretty much you can avoid checking it most likely, since the work to be done is almost the same.
Therefor a fastly coded solution would be like this.
bool row_contains_word(vector<char> const& row, string word)
{
if(word.size() > row.size())
throw std::invalid_argument("Word is longer then the row!!");
// linear search for the word in board
for(int i = 0; i < row.size() - word.size(); ++i) // start point
{
// check realtive to the start point if its the word there
for(int j = 0; j < word.size(); ++j)
{
if(row[i+j] != word[j])
break; // we can continue to check the next position
// last position we check here, and match means its the word
else if(j == (word.size() - 1) && row[i+j] == word[j])
return true;
}
}
return false;
Using this function (i dont think its a really good way to accomplish it, but just to have an example) you can simply loop:
for(int r = 0; r < board.size(); ++r)
{
if(row_contains_word(board[r], word))
return true;
}
// and same with colums as well
As mentioned in the comments, Solution isnt a candidate for a class.
It could be written like this:
namespace Solution
{
bool search(vector<vector<char>> const& board, string word); // implementaion follows
namespace // anonymous namespace, not accessible from the outside world, but only this compilation unit(.cpp file)
{
bool row_contains_word(vector<char> const& row, string word);
bool col_contains_word(/*vector<char> const& row,*/ string word); // this needs more work, since the col is a little different
}
}
This can hide the implementation from the searching from the interface of deciding if a word is contained in the board.
I have a BST program and this is my search function that returns true if the specified data (d) is found in a node. When it is called, node *s points to the root node of the tree.
This program works perfectly when I compile it on my university's virtual machine, but returns false when I compile and run it on my macbook. What would be causing a completely different output? I added a line to print out the data of each node it passes through while searching, and it finds the node with the correct data, but still returns false.
I'd appreciate any help, I can't think of a reason why this function would break under a different compiler.
This is the information on my mac compiler
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
This is the information on my virtual machine compiler
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Here is my BST search function:
bool bst::search(int d, node *s){
cout << s->data << endl;
if(s->data == d){
curRoot = s;
return 1;
}
else if(d > s->data){
if(s->right == NULL)
return 0;
else{
if(s->right->data == d)
pRoot = s;
search(d,s->right);
}
}
else{
if(s->left == NULL)
return 0;
else{
if(s->left->data == d)
pRoot = s;
search(d,s->left);
}
}
}
There are several problems/redundancies in your current function.
First of all, you need to return the value of your recursive function search() right to the top of the stack.
The redundancies are in the form of multiple NULL checks, multiple data equality checks.
A condensed form of the function to serve the purpose is as follows-
bool bst::search(int d, node *s){
if(s == NULL) {
return 0;
}
if(s->data == d) {
return 1;
}
if(d > s->data) {
return search(d,s->right);
}
else {
return search(d,s->left);
}
}
You seem to think the search function should return only once... that's indeed how it should be for an iterative implementation. So let's look at one, based on sray's simplification.
bool bst::search(int d, const node *s)
{
while (s) {
if(s->data == d) return 1;
if(d > s->data) {
s = s->right;
}
else {
s = s->left;
}
}
return 0;
}
This was possible because the recursion was tail-recursive -- we can just replace the arguments and loop.
Recursion itself doesn't work that way. It works just like any other function call. If you called sqrt inside your function, you wouldn't think that sqrt would replace your function, rather you would use the result in your calculation. Same thing when you call your own function. And sometimes recursive results need to be used for further calculation. Consider this function to count the nodes in a tree:
bool count(const node *s)
{
if (s)
return count(s->left) + 1 + count(s->right);
return 0;
}
It would do you no good to have the deepest calls set the return value for the whole tree.
my knowledge is limited but I have been working (hacking) at this specific data structure for awhile
I use a trie to store ontology strings that are then returned as a stack including the 'gap' proximity when get (string) is called. As an add on the trie stores attributes on the key. The further down the string the greater the detail of the attribute. This is working well for my purposes.
As an additional add on, I use a wildcard to apply an attribute to all child nodes. For example, to add 'paws' to all subnodes of 'mammals.dogs.' I push(mammals.dogs.*.paws). Now, all dogs have paws.
The problem is only the first dog get paws. The function works for push attributes without wild
If you want I can clean this up and give a simplified version, but in the past i've found on stackoverflow it is better to just give the code; I use 'z' as the '*' wild
void Trie::push(ParseT & packet)
{
if (root==NULL) AddFirstNode(); // condition 1: no nodes exist, should this be in wrapper
const string codeSoFar=packet.ID;
AddRecord(root, packet, codeSoFar); //condotion 2: nodes exist
}
void Trie::AddFirstNode(){ // run-once, initial condition of first node
nodeT *tempNode=new nodeT;
tempNode->attributes.planType=0;
tempNode->attributes.begin = 0;
tempNode->attributes.end = 0;
tempNode->attributes.alt_end = 0;
root=tempNode;
}
//add record to trie with mutal recursion through InsertNode
//record is entered to trie one char at a time, char is removed
//from record and function repeats until record is Null
void Trie::AddRecord(nodeT *w, ParseT &packet, string codeSoFar)
{
if (codeSoFar.empty()) {
//copy predecessor vector at level n, overwrites higher level vectors
if (!packet.predecessorTemp.empty())
w->attributes.predecessorTemp = packet.predecessorTemp;
return; //condition 0: record's last char
}
else { //keep parsing down record path
for (unsigned int i = 0; i < w->alpha.size(); i++) {
if (codeSoFar[0] == w->alpha[i].token_char || codeSoFar[0] == 'z') {
return AddRecord(w->alpha[i].next, packet, codeSoFar.substr(1)); // condition 2: char exists
}
}
InsertNode(w, packet, codeSoFar); //condition 3: no existing char --> mutal recursion
}
}
//AddRecord() helper function
void Trie::InsertNode(nodeT *w, ParseT &packet, string codeSoFar) // add new char to vector array
{
for (unsigned int i=0; i <=w->alpha.size(); i++) { // loop and insert tokens in sorted vector
if (i==w->alpha.size() || codeSoFar[0] < w->alpha[i].token_char) { //look for end of vector or indexical position
//create new TokenT
tokenT *tempChar=new tokenT;
tempChar->next=NULL;
tempChar->token_char=codeSoFar[0];
//create new nodeT
nodeT *tempLeaf=new nodeT;
tempLeaf->attributes.begin = 0;
tempLeaf->attributes.end = 0;
tempLeaf->attributes.planType = 0;
tempLeaf->attributes.alt_end = 0;
//last node
if (codeSoFar.size() == 1){
tempLeaf->attributes.predecessorTemp = packet.predecessorTemp;
}
//link TokenT with its nodeT
tempChar->next=tempLeaf;
AddRecord(tempLeaf, packet, codeSoFar.substr(1)); //mutual recursion --> add next char in record, if last char AddRecord will terminate
w->alpha.insert(w->alpha.begin()+i, *tempChar);
return;
}
}
}
root is global nodeT *w
struct ParseT {
string ID; //XML key
int begin = 0; //planned or actual start date
int end = 0; //planned or actual end date - if end is empty then assumed started but not compelted and flag with 9999 and
int alt_end = 0; //in case of started without completion 9999 case, then this holds expected end
int planType = 0; //actuals == 1, forecast == 2, planned == 3
map<string, string> aux;
vector<string> resourceTemp;
vector<string> predecessorTemp;
};
In this code
for (unsigned int i = 0; i < w->alpha.size(); i++) {
if (codeSoFar[0] == w->alpha[i].token_char || codeSoFar[0] == 'z') {
return AddRecord(w->alpha[i].next, packet, codeSoFar.substr(1)); // condition 2: char exists
}
}
you are returning as soon as you call AddRecord, even if it is because of a wildcard. It might be easier to have a separate loop when codeSoFar[0] == 'z' that goes through all the alphas and adds the record. Then have an else clause that does your current code.
Edit: Here is what I meant, in code form:
else { //keep parsing down record path
// Handle wildcards
if (codeSoFar[0] == 'z') {
for (unsigned int i = 0; i < w->alpha.size(); i++) {
AddRecord(w->alpha[i].next, packet, codeSoFar.substr(1)); // condition 2: char exists
}
}
else {
// Not a wildcard, look for a match
for (unsigned int i = 0; i < w->alpha.size(); i++) {
if (codeSoFar[0] == w->alpha[i].token_char) {
return AddRecord(w->alpha[i].next, packet, codeSoFar.substr(1)); // condition 2: char exists
}
}
InsertNode(w, packet, codeSoFar); //condition 3: no existing char --> mutal recursion
}
}
I'm trying to make function that has a loop that checks every member of an array made from boolean variables and exits when it finds the first "true" value.
That's what I have now:
bool solids[50];
int a,i;
//"equality" is a function that checks the equality between "a" and a defined value
solids[0] = equality(a,&value_1);
solids[1] = equality(a,&value_1);
solids[2] = equality(a,&value_1);
solids[3] = equality(a,&value_1);
for (i = 0; solids[i] != true; i++)
{
[...]
}
But I have no idea, what should I put into the loop?
My attempt was
for (i = 0; i <= 50; i++)
{
if (solids[i] == true)
{
return true;
break;
} else {
return false;
}
}
,that should return true after the first found true and return false if the array has no member with true value, but it doesn't seem to work in the code.
Is it wrong? If yes, what is the problem?
PS.: I may count the number of trues with a counter but that's not an optimal solve to the problem, since I just look for the FIRST true value and consequently, the program doesn't have to check all the 50 members. Needley to count, how many unnecesary steps should this solve would mean.
here's a short example usage of std::find() as advised by #chris:
bool find_element_in_array() {
bool solids[50];
int length;
/* ... do many operations, and keep length as the size of values inserted in solids */
bool* location = std::find(solids, length, true);
// if element is found return true
if (location != solids + length)
return true;
// else return false
return false;
}
Once you have solids correctly set (it looks like you're currently setting every value to the same thing), you can make a loop that exits on the first true like this:
for (i = 0; i < 50; i++)
{
if (solids[i] == true)
{
return true;
}
}
return false;
I'd also just move the declaration of i into the for loop body, since it's not used outside, but the above answers your question.
return immediately exits the function, so there is no need to break the loop after.
If it's sufficient to exit the function right after the search, you should write something like:
for (int i = 0; i < 50; i++) {
if (solids[i]) return true;
}
return false;
If you need to use the result of the search in the same function, use additional variable:
bool found = false;
for (int = 0; i < 50; i++) {
if (solids[i]) {
bool = true;
break;
}
}
if (found) { ...
This is one of the solution of getting true or false from given set and target value
bool subsetSumExists(Set<int> & set, int target) {
if (set.isEmpty()) {
return target == 0;
} else {
int element = set.first();
Set<int> rest = set - element;
return subsetSumExists(rest, target)
|| (subsetSumExists(rest, target- element));
}
}
However, this solution will return true or false value only. How is it possible to get the element that involve in the subset(set that add together will equal to target) as well?
Do I have to use dynamic programming? Coz as i know.. recursion is building up stack actually and after the function return the value, the value inside the frame will be discarded as well.
So, is it possible to get the elements that add up equal to the target value.
Is passing an object a solution of the problem?
Thank you
First of all you can optimize your program a little bit - check if target is 0 and if it is always return true. Now what you need is to have somewhere to store the elements that you have already used. I will show you a way to do that with a global "stack"(vector in fact so that you can iterate over it), because then the code will be easier to understand, but you can also pass it by reference to the function or avoid making it global in some other way.
By the way the stl container is called set not Set.
vector<int> used;
bool subsetSumExists(Set<int> & set, int target) {
if (target == 0) {
cout << "One possible sum is:\n";
for (int i = 0; i < used.size(); ++i) {
cout << used[i] << endl;
}
return true;
} else if(set.empty()) {
return false;
}else {
int element = set.first();
Set<int> rest = set - element;
used.push_back(element);
if (subsetSumExists(rest, target- element)) {
return true;
} else {
used.pop_back();
}
return subsetSumExists(rest, target);
}
}
Hope this helps.