Exemplo n.º 1
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());
	}
}
Exemplo n.º 2
0
/*
Function: writeToFile()

Description: Will write the contents of the symbol table to the output stream. 
*/
void symbolTable::writeToScreen() {
	// variables
	bstItr bstItr; 
	symTblItr symbolTableItr; 
	Bst* currentBst;
	int scopeLevel = table.size() - 1; 

	if (scopeLevel == -1) {
		std::cout << "Symbol table is empty!" << std::endl;
	}

	else {
		std::cout << "=== DISPLAYING SYMBOL TABLE ===" << std::endl; 
		for (symbolTableItr = table.begin(); 
				symbolTableItr != table.end(); 
				symbolTableItr++) {
			currentBst = symbolTableItr->getBst();
			for (int i = 0; i < symbolTableItr->getScopeLevel(); i++) {
				std::cout << "\t";
			}

			std::cout << ">> Scope level " << symbolTableItr->getScopeLevel() << " in scope ";
			std::cout << symbolTableItr->getOuterScope() << "." << std::endl;
			if (currentBst->empty() ) {
				std::cout << "\tNo identifiers in this scope." << std::endl; 
			}
			else {
				for (bstItr = currentBst->begin(); bstItr != currentBst->end(); bstItr++) {
					bstItr->second.displayIdentifierAttributes(symbolTableItr->getScopeLevel()); 
				}
			}
		}
		std::cout << "=== END OF SYMBOL TABLE DISPLAY ===" << std::endl; 
	}
}
Exemplo n.º 3
0
Arquivo: bst.cpp Projeto: angad/algos
int main() {

	node root;
	Bst *t = new Bst(&root, 10);
	t->insert(&root, 5);
	t->printTree(&root);

	return 0;
}
Exemplo n.º 4
0
/*
Function: insertNewSymbol(std::string name, int line)

Description: This function will add a new entry to the current scope level
on the stack. 

Parameters:
std::string name: The name of the identifier to be added to the current scope 
level. 
int line: The line number that the associated identifier is located at in the
source program. 
*/
symbolTableEntry* symbolTable::insertNewSymbol(std::string name, int line) {
	symbolTableEntry* newEntry = new symbolTableEntry(name, line);
	Bst* currentVars = table[table.size() - 1].getBst();
	currentVars->insert(entry(name, *newEntry));
	bstItr bitr = currentVars->find(name);
	if(bitr != currentVars->end()){
		return &bitr->second; 
	}
	else{
		return NULL;
	}
}
Exemplo n.º 5
0
/*
Function: insertNewSymbol(std::string name, int line)

Description: This function will add a new entry to the current scope level
on the stack. 

Parameters:
std::string name: The name of the identifier to be added to the current scope 
level. 
int line: The line number that the associated identifier is located at in the
source program. 
*/
symbolTableEntry* symbolTable::insertNewSymbol(std::string name, int line) {
	// allocate a new symbol table entry object and add it to the BST at
	// the current scope 
	symbolTableEntry* newEntry = new symbolTableEntry(name, line);
	Bst* currentVars = table[table.size() - 1].getBst();
	currentVars->insert(entry(name, *newEntry));
	
	// search for and declare a pointer to the symbol table entry object in the
	// BST and NOT the one we just created in this function 
	bstItr bItr = currentVars->find(name);
	if(bItr != currentVars->end()) {
		return &bItr->second; 
	}
	else return NULL; 
}
Exemplo n.º 6
0
/*
Function: writeToFile()

Description: Will write the contents of the symbol table to a file that
will be located in the "outputFiles" directory.  
*/
void symbolTable::writeToFile() {
	// variables
	bstItr bstItr; 
	symTblItr symbolTableItr; 
	Bst* currentBst;
	std::ofstream outFile;
	outFile.open("../outputFiles/symbolTableContents.txt", std::ofstream::out); 
	int scopeLevel = table.size() - 1;  

	if (scopeLevel == -1) {
		outFile << "Symbol table is empty!" << std::endl;
	}

	else {
		outFile << "=== DISPLAYING SYMBOL TABLE ===" << std::endl;
		for (symbolTableItr = table.begin(); 
				symbolTableItr != table.end(); 
				symbolTableItr++) {
			currentBst = symbolTableItr->getBst();
			for (int i = 0; i <= symbolTableItr->getScopeLevel(); i++) {
				outFile << "\t";
			}

			outFile << ">> Scope level " << symbolTableItr->getScopeLevel() << " in scope ";
			outFile << symbolTableItr->getOuterScope() << "." << std::endl;
			if (currentBst->empty() ) {
				outFile << "\tNo identifiers in this scope." << std::endl; 
			}

			else {
				for (bstItr = currentBst->begin(); bstItr != currentBst->end(); bstItr++) {
					for (int i = 0; i < symbolTableItr->getScopeLevel(); i++) {
						outFile << "\t";
					}
					outFile << "\tVariable: " << bstItr->first << std::endl;
					for (int i = 0; i < symbolTableItr->getScopeLevel(); i++) {
						outFile << "\t";
					}
					//outFile << "\tType: " << bstItr->second.getTypeStr() << std:: endl;
				}
			}
		}
		outFile << "=== END OF SYMBOL TABLE DISPLAY ===" << std::endl;
	}

	// file writing complete 
	outFile.close(); 	
}
Exemplo n.º 7
0
/*
Function: searchForSymbol(std::string symbolToSearch)

Description: This function will only search the top of the symbol table
for a given variable. If this variable is found, a pointer to the symbol
table entry will be returned. Otherwise, if the function was unable to locate
the symbol table entry or if the symbol table is empty, the function will 
return NULL. 
*/
symbolTableEntry* symbolTable::searchTopOfStack(std::string symbolToSearch) {
	if (table.size() == 0) {
		return NULL; 
	}

	// variables
	Bst* topBst = table[table.size()-1].getBst();
	bstItr scopeItr;

	// iterate through the top BST in the symbol table
	for (scopeItr = topBst->begin(); scopeItr != topBst->end(); scopeItr++) {
		// check for the desired symbol
		if (symbolToSearch == scopeItr->first) {
			return &(scopeItr->second); 
		}
	}
	return NULL; 
}
Exemplo n.º 8
0
void  
Val::
extendTrees(Bst& bst2,int pos)
{
  len_++;
  vec_.push_back(0);
  if(bsts_.size() == 0) bsts_.push_back(&bst2);
  else if(pos < 2) bsts_.push_front(&bst2);
  else bsts_.push_back(&bst2);
  prob_ *= bst2.prob();
}
Exemplo n.º 9
0
/*
Function: writeToFile()

Description: Will write the contents of the symbol table to the output stream. 
*/
void symbolTable::writeToScreen() {
	// variables
	bstItr bstItr; 
	symTblItr symbolTableItr; 
	Bst* currentBst;
	int scopeLevel = table.size() - 1; 

	if(scopeLevel == -1){
		std::cout << "Symbol table is empty!" << std::endl;
	}

	else {
		for (symbolTableItr = table.begin(); symbolTableItr != table.end(); symbolTableItr++) {
			currentBst = symbolTableItr->getBst();
			for( int i = 0; i <= symbolTableItr->getScopeLevel(); i++ ){
				std::cout << "\t";
			}
			std::cout << "Scope Level " << symbolTableItr->getScopeLevel() << " in Scope ";
			std::cout << symbolTableItr->getOuterScope() << std::endl;
			if (currentBst->empty() ) {
				std::cout << "\tNo identifiers in this scope." << std::endl; 
			}
			else {
				for (bstItr = currentBst->begin(); bstItr != currentBst->end(); bstItr++) {
					for( int i = 0; i < symbolTableItr->getScopeLevel(); i++ ){
						std::cout << "\t";
					}
				
					std::cout << "\tVariable: " << bstItr->first << std::endl;
					for( int i = 0; i < symbolTableItr->getScopeLevel(); i++ ){
						std::cout << "\t";
					}
					std::cout << "\tType: " << bstItr->second.getTypeStr() << std:: endl;
				}
			}
		}
	}
}
Exemplo n.º 10
0
void prepare()
{
    int N = 0;
    for (int i = 1; i <= MAXLUCKY; i += 2)
        tree.K[N++] = i;

    tree.init(N);

    for (int i = 1; ; ++i) {
        int t = tree.find_kth(i);
        if (t == Nil) break;

        int mul = tree.get(t);
        for (int j = mul * (tree.size() / mul); j > 0; j -= mul)
            tree.remove(tree.find_kth(j - 1));
    }

    lucky_term = 1;
    tree.inorder(tree.root);
}
Exemplo n.º 11
0
int main(){
	Bst<int> bst;
	bst.bst_insert(53);
	bst.bst_insert(30);
	bst.bst_insert(72);
	bst.bst_insert(14);
	bst.bst_insert(39);
	bst.bst_insert(61);
	bst.bst_insert(81);
	/*bst.bst_update("ccc","ddd");
	bst.bst_insert("eee");*/
	bst.bst_travel();
	cout<<"height:"<<bst.bst_height()<<endl;
	cout<<"size:"<<bst.bst_size()<<endl;
	cout<<"leapNum:"<<bst.bst_leapNum()<<endl; //还有问题
	bst.bst_erase(14);
	bst.bst_travel();
	cout<<"size:"<<bst.bst_size()<<endl;
	cout<<"leapNum:"<<bst.bst_leapNum()<<endl;
	cout<<"=====clear========"<<endl;
	bst.bst_clear();
	bst.bst_travel();
	cout<<"height:"<<bst.bst_height()<<endl;
	return 0;
}
Exemplo n.º 12
0
int main()
{
   char pause;
   LoneString* StrPtr_;

   Bst<LoneString, LoneString>::SetComparisonFunction(CompLoneString);   
   Bst<LoneString, LoneString> dict;
   Bst<LoneString, LoneString> another;

   dict.Insert("715", "Nou");
   dict.Insert("720", "Vince");
   dict.Insert("30",  "Habib");
   dict.Insert("216", "Lorene");
   dict.Insert("316", "Pari");
   dict.Insert("740", "Michael");
   dict.Insert("730", "Pierre");
   dict.Insert("615", "Ardeshir");
   dict.Insert("615", "Ardeshir2");

   cout << "Finished Inserting:\n";

   dict.DumpNode(dict.Root(), cout);
   cout << "Type any character to continue:";
   cin >> pause;

   another = dict;
   another = another;
   another = dict;

   cout << "Finished assignment:\n";

   another.DumpNode(another.Root(), cout);
   cout << "Type any character to continue:";
   cin >> pause;

   dict.Del("216");
   dict.Del("316");
   dict.Del("740");
   dict.Del("999");
   dict.Del("715");
   dict.Del("740");

   cout << "Finished deleting:\n";

   dict.DumpNode(dict.Root(), cout);
   cout << "Type any character to continue:";
   cin >> pause;

   //  Note this does searching on the (another) tree without deletions.
   cout << "Key 715 - Value: " << ((StrPtr_ = another.SearchNode("715", another.Root())) ? *StrPtr_:"(null)") << '\n';
   cout << "Key  30 - Value: " << ((StrPtr_ = another.SearchNode("30", another.Root())) ? *StrPtr_:"(null)") << '\n';
   cout << "Key 720 - Value: " << ((StrPtr_ = another.SearchNode("720", another.Root())) ? *StrPtr_:"(null)") << '\n';
   cout << "Key 615 - Value: " << ((StrPtr_ = another.SearchNode("615", another.Root())) ? *StrPtr_:"(null)") << '\n';
   cout << "Key 740 - Value: " << ((StrPtr_ = another.SearchNode("740", another.Root())) ? *StrPtr_:"(null)") << '\n';
   cout << "Key 730 - Value: " << ((StrPtr_ = another.SearchNode("730", another.Root())) ? *StrPtr_:"(null)") << '\n';
   cout << "Key 999 - Value: " << ((StrPtr_ = another.SearchNode("999", another.Root())) ? *StrPtr_:"(null)") << "\n\n";

   cout << "Finished searching\n\n";

   cout << "Type any character to continue:";
   cin >> pause;

   another.InOrder(another.Root(), cout, applytest);

   cout << "Finished applying\n\n";

   cout << "Type any character to continue:";
   cin >> pause;

   cout << "Test complete (for now)\n\n";

   return 0;
}
Exemplo n.º 13
0
int main()
{
	using namespace std;
	Bst bst;
	bst.AddElement(&bst.root, 50,0);
	bst.AddElement(&bst.root, 25,0);
	bst.AddElement(&bst.root, 47,0);
	bst.AddElement(&bst.root, 60,0);
	bst.AddElement(&bst.root, 42,0);
	bst.AddElement(&bst.root, 100,0);
	bst.AddElement(&bst.root, 55,0);

	cout<<"InOrderTraversal"<<endl;
	bst.InOrderTraversal(bst.root);
	cout<<endl<<"PreOrderTraversal"<<endl;
	bst.PreOrderTraversal(bst.root);
	cout<<endl<<"PreOrderTraversal"<<endl;
	bst.PostOrderTraversal(bst.root);
	cout<<endl<<"LevelOrderTraversal"<<endl;
	bst.LevelOrderTraversal(bst.root);
	cout<<endl<<"Deleting All:"<<endl;
	bst.DeleteAll(&bst.root);


	return 0;
}