Esempio n. 1
0
int searchHelper(Node *node, unsigned short x) {
	if (node==NULL) return 0;
	if (node->x == x) return 1;
	else {
		if (x < node->x) 
			searchHelper(node->left, x);
		else searchHelper(node->right, x);
	}
	return 0;
}
Esempio n. 2
0
//Search element in BST
Node * BST::search(int data){
	if(root == NULL){
			cout<<endl<<"BST empty.So no element to search."<<endl;
			return NULL;
		}
		else{
			if(searchHelper(root,data))
			return searchHelper(root,data);
			else
				return NULL;
		}
}
Esempio n. 3
0
TreeNode<NODETYPE>* Tree<NODETYPE>::searchHelper(TreeNode<NODETYPE> *ptr, const NODETYPE &value) const {
    // base case
    if (ptr == nullptr || ptr->getData() == value) {
        return ptr;
    }

    // greater
    if (value > ptr->getData()) {
        return searchHelper(ptr->rightPtr, value);
    }

    // smaller
    return searchHelper(ptr->leftPtr, value);
}  // end method searchHelper
Esempio n. 4
0
 bool searchHelper(vector<vector<int> > &matrix, int beg, int end, int target) {
     if(beg > end) {
         return false;
     }
     int mid = (beg+end) / 2;
     int col = mid / matrix[0].size();
     int row = mid % matrix[0].size();
     if(matrix[col][row] == target) {
         return true;
     } else if(matrix[col][row] > target) {
         return searchHelper(matrix,0,mid-1,target);
     } else {
         return searchHelper(matrix,mid+1,end,target);
     }
 }
Esempio n. 5
0
/*
Function: symbolTableEntry* searchHelper(std::string symbolToSearch, 
											int& levelSymbolWasFound,
											int searchLevel)

Description: This function will search through the binary seach tree at a 
specific scope level. If the identifier is not located within the current
binary search tree, the function will recursively check the outer scope levels
to see if the identifier can be located. 

Parameters:
std::string symbolToSearch: The name of the identifier to be searched for.
int& levelSymbolWasFound: Will contain the scope level where symbolToSearch
was located.
int searchLevel: Used to resursively check the outer scope levels of a given
scope.  
*/
symbolTableEntry* symbolTable::searchHelper(std::string symbolToSearch, 
											int&levelSymbolWasFound, 
											int searchLevel){
	// variables
	Bst* searchBst = table[searchLevel].getBst();
	bstItr scopeItr;

	// iterate through the current BST in the symbol table
	for (scopeItr = searchBst->begin(); scopeItr != searchBst->end(); scopeItr++) {
		// check for the desired symbol
		if (symbolToSearch == scopeItr->first) {
			levelSymbolWasFound = searchLevel;
			return &(scopeItr->second); 
		}
	}

	// if identifier not found and in global scope, there is nowhere else
	// to search
	if(searchLevel == 0){
		levelSymbolWasFound = -1;
		return NULL;
	}

	// check next outer scope
	else{
		return searchHelper(symbolToSearch, levelSymbolWasFound, 
							table[searchLevel].getOuterScope());
	}
}
Esempio n. 6
0
 void searchHelper(int A[], int b, int e, int target) {
     if(b > e) {
         return;
     }
     int mid = (b+e) / 2;
     if(A[mid] == target) {
         found = true;
         beg = min(beg,mid);
         end = max(end,mid);
         searchHelper(A,b,mid-1,target);
         searchHelper(A,mid+1,e,target);
     } else if(A[mid] > target) {
         searchHelper(A,b,mid-1,target);
     } else {
         searchHelper(A,mid+1,e,target);
     }
 }
Esempio n. 7
0
/*
Function: symbolTableEntry* searchForSymbol(std::string symbolToSearch, 
											int& levelSymbolWasFound)

Description: This function will search the symbol table for the desired 
symbol and return a pointer to the corresponding symbol table entry. 

This function replies on the function searchHelper() to search for the
symbol. 

Parameters:
std::string symbolToSearch: The name of the identifier to be searched for.
int& levelSymbolWasFound: Will contain the scope level where symbolToSearch
was located. 
*/
symbolTableEntry* symbolTable::searchForSymbol(std::string symbolToSearch, 
												int& levelSymbolWasFound) {
	if(table.size() == 0){
		levelSymbolWasFound = -1;
		return NULL;
	}
	// recursively search the other scopes to see if the symbol can be located
	return searchHelper(symbolToSearch, levelSymbolWasFound, table.size()-1);
}
Esempio n. 8
0
Node * BST::searchHelper(Node *node,int data){
	if(node == NULL){
		return NULL;
	}
	if(data == node->data){
		return node;
	}
	else if(data < node->data){
		return searchHelper(node->left,data);
	}
	else if(data > node->data){
		return searchHelper(node->right,data);
	}
	else{
		cout<<endl<<"Not Found:"<<data<<endl;
		return NULL;
	}
}
Esempio n. 9
0
    int searchHelper(vector<int>& nums, int target, int start, int end) {
        // if(start == end) return -1;
        if(start == end-1) return nums[start] == target? start:-1;
        int middle = (start + end)/2;

        if(nums[middle] > nums[start]) {
        
            if(target >= nums[middle] || target < nums[start]) return searchHelper(nums, target, middle, end);
            
            else return binarySearch(nums, target, start, middle-1);
        }
        
        else {
                    
            if(target >= nums[middle] && target < nums[start]) return binarySearch(nums, target, middle, end-1);
            
            else return searchHelper(nums, target, start, middle);
        }
    }
Esempio n. 10
0
 bool searchMatrix(vector<vector<int> > &matrix, int target) {
     int m = matrix.size();
     if(m == 0) {
         return false;
     }
     int n = matrix[0].size();
     if(n == 0) {
         return false;
     }
     return searchHelper(matrix,0,m*n-1,target);
 }
    int searchHelper(vector<int>& nums, int s, int e, int target){
      if(s == e && nums[s] == target) return true;
      if(s == e && nums[s] != target) return false;
      if(s > e) return false;
      int mid = (s + e) / 2;
      if(target == nums[mid]) return true;
      if(nums[mid] > nums[s]){ 
	if(target >= nums[s] && target < nums[mid])
	  return searchHelper(nums, s, mid - 1, target);
	else
	  return searchHelper(nums, mid + 1, e, target);
      }
      else if(nums[mid] < nums[s]){
	if(target > nums[mid] && target <= nums[e])
	  return searchHelper(nums, mid + 1, e, target);
	else
	  return searchHelper(nums, s, mid - 1, target);
      }
      else{
	return searchHelper(nums, s + 1, e, target); // if[s] = [e], continue
      }
    }
int Heap::searchHelper(int index, int item) {
    if (index > arr.size() - 1) {
        return -1;
    } else {
        if (arr[index] == arr[item]) {
            return index;
        } else {

            if (arr[index] == item) {
                return index;
            }

            int left = searchHelper(2 * index + 1, item);
            int right = searchHelper(2 * index + 2, item);

            if (left != -1) {
                return left;
            } else {
                return right;
            }
        }
    }
}
Esempio n. 13
0
 vector<int> searchRange(int A[], int n, int target) {
     vector<int> ret;
     beg = INT_MAX;
     end = INT_MIN;
     found = false;
     searchHelper(A, 0, n-1, target);
     if(found) {
         ret.push_back(beg);
         ret.push_back(end);
     } else {
         ret.push_back(-1);
         ret.push_back(-1);
     }
     return ret;
 }
Esempio n. 14
0
node* searchHelper(node* root,string str,string fullstr)
{
	if(root==NULL)
	{
		return NULL;
	}
	else if(str.size()==0)
	{
		return root;
	}
	else
	{
		char c=str[0];
		int k=c;
		if(!(k<123 && k>96))
		{
			k=k+32;
			c=k;
			str[0]=c;
		}
		
		list<node*>::iterator itr;
		
		int flag=0;
		
		for(itr=root->childList.begin();itr!=root->childList.end();itr++)
		{
			if(str[0]==(*itr)->name[0])
			{
				flag=1;
				return searchHelper(*itr,str.substr(1,str.size()),fullstr);
				break;
			}
			else
			{
				flag=0;
				continue;
			}
		}
		
		if(flag==0)
			return NULL;
	}
}
Esempio n. 15
0
/* returns "True" (i.e. 1) if findMe is located somewhere in the tree, otherwise "False" (i.e. 0). */ 
int bst_isValueInTree(BST * tree, unsigned short findMe) {
	if (tree==NULL) return 0;
	return searchHelper(tree->root, findMe);
	
}
 bool search(vector<int>& nums, int target) {
   return searchHelper(nums, 0, nums.size() - 1, target);
 }
Esempio n. 17
0
node* tries::search(string str)
{
	//cout<<"*********search Result**************"<<endl;
	return searchHelper(root,str,str);
}				
Esempio n. 18
0
 int search(vector<int>& nums, int target) {
     
     return searchHelper(nums, target, 0, nums.size());
     
 }
Esempio n. 19
0
TreeNode<NODETYPE>* Tree<NODETYPE>::binaryTreeSearch(const NODETYPE &value) const {
    return searchHelper(rootPtr, value);
}  // end method binaryTreeSearch
int Heap::search(int item) {
    return searchHelper(0, item);
}