Recursive Search Not Working - c++

I have been building binary tree in C++. I tried to add search element functionality via recursive method.
It's not working properly.
Please see below code and guide.
bool Binarytree::SearchElement(node* SearchPtr,int key,bool found) {
if(SearchPtr->data==key)
found=true;
if(SearchPtr==NULL || found==true)
return found;
else
{
if(SearchPtr->data>key)
{ cout<<endl<<"Seaching in Left Sub tree"<<endl;
SearchElement(SearchPtr->left,key,found);
}
else
{ cout<<endl<<"Seaching in right Sub tree"<<endl;
SearchElement(SearchPtr->right,key,found);
}
}
}
The function SearchElement is called as below: (Note value 1 is not present in tree)
if(SearchElement(root,1,false))
cout<<endl<<"Element Found"<<endl;
else
cout<<endl<<"Element not Found"<<endl;

You're not returning anything. Once you've found NULL you need to return false. And if you've found the element you need to return true. Once you've known whether the element is present or not, you need to return for all the recursive calls. Check the code below.
Note: You've mentioned binary tree and searched like a binary search tree. Considering it is a binary search tree, it will be like this:
bool Binarytree::SearchElement(node* SearchPtr,int key) {
if(SearchPtr->data==key)
return true;
if(SearchPtr==NULL)
return false;
if(SearchPtr->data < key) //not greater than, but less than
{ cout<<endl<<"Seaching in Left Sub tree"<<endl;
return SearchElement(SearchPtr->left,key);
}
else
{ cout<<endl<<"Seaching in right Sub tree"<<endl;
return SearchElement(SearchPtr->right,key);
}
}
If it is binary tree, you'll have to traverse the whole tree to find whether the element is present or not. Like this:
bool Binarytree::SearchElement(node* SearchPtr,int key) {
if (SearchPtr==NULL)
return false;
if(SearchPtr->data==key)
return true;
if (SearchElement(SearchPtr->left,key))
return true;
if (SearchElement(SearchPtr->right,key))
return true;
return false;
}

Related

DFS search time complexity analysis

In finding the cycle in the dfs if we pass & to the boolean vector the code correctely submitted but when I I don't use it give time complexity erroe
bool dfs(vector<int>adj[],int V,vector<bool>&b,int i,vector<bool>&d){
b[i]=true;
d[i]=true;
vector<int>::iterator it;
for(it=adj[i].begin();it!=adj[i].end();++it){
if(b[*it]!=true){
if(dfs(adj,V,b,*it,d)){
return true;
}
}
else if(d[*it]==true){
return true;
}
}
d[i]=false;
return false;
}

std::unordered_map::count is not working in my code

I have a doubt with the solution of this question which is stated below -
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Strings["aa", "ab"] should return false and strings["aa", "aab"] should return true according to question.
Here is the code which I have attempted in the first place and I'm not getting a required output as mentioned above.
unordered_map<char,int>umap;
for(char m:magazine)
{
umap[m]++;
}
for(char r:ransomNote)
{
if(umap.count(r)<=1)
{
return false;
}
else{
umap[r]--;
}
}
return true;
}
In the above code, I have used umap.count(r)<=1 to return false if there is no key present.
For the strings ["aa","aab"], it is returning true but for strings ["aa","ab"], it is also returning true but it should return false.
Then I used another way to solve this problem by using just umap[r]<=0 in the place of umap.count(r)<=1 and it is working just fine and else all code is same.
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char,int>umap;
for(char m:magazine)
{
umap[m]++;
}
for(char r:ransomNote)
{
if(umap[r]<=0)
{
return false;
}
else{
umap[r]--;
}
}
return true;
}
I'm not able to get what i'm missing in the if condition of first code. Can anyone help me to state what I'm doing wrong in first code. Any help is appreciated.
unordered_map::count returns the number of items with specified key.
As you don't use multi_map version, you only have 0 or 1.
Associated value doesn't change presence of key in map.
To use count, you should remove key when value reaches 0:
for (char r : ransomNote) {
if (umap.count(r) == 0) {
return false;
} else {
if (--umap[r] == 0) {
umap.erase(r);
}
}
}
return true;

Where am I making mistake in creating this function to search element from singly linked list in C++?

I'm trying to make a function which searches element from singly linked list and returns address of the value found. otherwise returns null.
But when I enter the value to search only the value at 1st node/position gets searched. For all the other input value it returns NULL and the message pops up saying "Item not found"
Means it only works for the head node but I can't understand why. I thought the while loop would traverse till the end and if the element matched the data in the list it would return the address which is not happening at all.
HERE'S MY CODE:
node *searchData(int key)
{
node *curNode=head; //head is global variable
while (curNode!=NULL)
{
if(curNode->data==key)
{
return curNode;
break;
}
else
return NULL;
curNode=curNode->link;
}
}
MY MAIN FUNCTION:
cout<<"Enter The element to search?"<<endl;
cin>>elem;
b=searchData(elem);//use searchData function here
if(b==NULL)
cout<<"Item Not Found!!";
else
cout<<"Element "<<elem<<" was found at address: "<<b<<endl;
}
node *searchData(int key)
{
node *curNode=head; //head is global variable
while (curNode!=NULL)
{
if(curNode->data==key)
{
return curNode;
// break; // redundant, you have already returned.
}
// else // no need to guard code against True, True returned.
// return NULL; // your not done going thru the linked list yet
curNode=curNode->link;
}
return NULL; // went thru the list to no avail.
}
the short version
node *searchData(int key)
{
for( node *curNode=head; curNode!=NULL; curNode=curNode->link)
if(curNode->data==key)
return curNode;
return NULL;
}

Binary Tree findHeight

So I'm trying to add a findHeight method to my professors Binary Tree Class, and I'm having a bit of trouble.
BTNode<T> findParent(BTNode<T> Root, BTNode<T> child)
{
if(!root)
{
return Null;
}
if(Root.left* == child || Root.right* == child)
{
return Root;
}
else
{
//recursively checks left tree to see if child node is pointed to
findParent(Root.left, child);
//recursively checks right tree to see if child node is pointed to
findParent(Root.right, child);
}
int findHeight(BTNode<T> thisNode)
{
if (Count.findParent(.root, thisNode) == null)
{
return 0;
}
return findHeight(thisNode.findParent) + 1;
}
My problem is that in the findHeight method, it calls the findParent() method, and I have to reference the root of the binary tree that the parameter thisNode comes from, and because this is just part of the class, I don't know how I am supposed to reference the root. The BT (binary tree) class has a function that returns the root of the tree, but since I have no Binary Tree to reference, I don't know what to do. Please Help!!!
Normally, the findHeight function wouldn't "worry" about finding the root of the tree -- it just finds the height of the tree below whatever node it's passed. It usually looks something like this:
int findHeight(BTNode <T> *thiNode) {
if (thisNode == NULL)
return 0;
int left_height = findHeight(thisNode->left);
int right_height = findHeight(thisNode->right);
return 1 + max(left_height, right_height);
}
It's then up to the user to pass in the root of the tree whose height they want.

C++ array - uses only first line

i have problems with the array function.. -
i put my mac on fist line and then says me approved , but if is on 2st line rejected. my real one is B6 on end. /address down are not real../
in heder file settings >
#define CLIENTSNUMBER 2
BOOL Checking2(LPCSTR MacID);
cpp >
char ClientMacs[CLIENTSNUMBER*1][18] = {
"5A-77-77-97-87-B7",
"5A-77-77-97-87-B6"
};
BOOL Checking2(LPCSTR MacID)
{
for(int x=0;x<CLIENTSNUMBER;x++)
{
if(!strcmp(MacID,ClientMacs[x]))
{
MessageBoxA(NULL,MacID,"APPROVED!",MB_OK);
return false;
} else {
MessageBoxA(NULL,MacID,"REJECTED!",MB_OK);
return false;
}
}
return false;
}
Because you return from your function (breaking out of your loop) when something matches or doesn't match. It will never actually loop.
Edit because it's a slow morning:
You need to go through the entire array and look at every element for a match before declaring it's rejected:
BOOL Checking2(LPCSTR MacID)
{
for(int x=0;x<CLIENTSNUMBER;x++)
{
if(strcmp(MacID,ClientMacs[x]) == 0)
{
MessageBoxA(NULL,MacID,"APPROVED!",MB_OK);
return false;
}
}
MessageBoxA(NULL,MacID,"REJECTED!",MB_OK);
return false;
}
Also, do you really mean to return false in both cases? I would assume if you find a match it should return true