diff --git a/Makefile b/Makefile index 6a17adbf5ab25731bee9032e41acf199e52e03f1..781166c51d0dc7346a538840b7f4a0b5fdeaff73 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,10 @@ LIB=./lib INCLUDE=./include SRC=./src OBJ=./obj -LIBFLAGS = -lbst_lib FLAGS = -Wall main: bst_lib - $(CC) $(SRC)/main.cpp $(FLAGS) -I$(INCLUDE) -L$(LIB) $(LIBFLAGS) -o bst + $(CC) $(SRC)/main.cpp $(OBJ)/bst_lib.o $(FLAGS) -I$(INCLUDE) -L$(LIB) -o bst bst_lib: $(CC) -c $(SRC)/bst_lib.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/bst_lib.o diff --git a/include/bst_lib.h b/include/bst_lib.h index 2aeccbac16eeb6b4e07261f7e2741529ec84c2dd..6dbde87be1b6a7fd818f85889ef0529835fd276b 100644 --- a/include/bst_lib.h +++ b/include/bst_lib.h @@ -1,19 +1,36 @@ // Implementado por Eduardo Machado // 2015 +#include <iostream> + +using namespace std; + class Node{ private: int key; - char truck; + string truck; Node *leftNode; Node *rightNode; Node *fatherNode; public: void setKey(int newKey); - void setTruck(char newTruck); + void setTruck(string newTruck); void setLeftNode(Node *newLeftNode); void setRightNode(Node *newRightNode); void setFatherNode(Node *newRatherNode); + + int getKey(); + string getTruck(); + Node* getLeftNode(); + Node* getRightNode(); + Node* getFatherNode(); + void printInOrder(); }; + +Node* createNode(int key, string truck); + +void insertNode(Node *root, int key, string truck); + +Node* binarySearch(Node *root, int key); diff --git a/src/bst_lib.cpp b/src/bst_lib.cpp index 43afaf97263e5288cc47508e493cc48b4ef63ef9..d1fa51f718cd51bf6d2c5c7757d100465d4bb256 100644 --- a/src/bst_lib.cpp +++ b/src/bst_lib.cpp @@ -11,7 +11,7 @@ void Node::setKey(int newKey){ key=newKey; } -void Node::setTruck(char newTruck){ +void Node::setTruck(string newTruck){ truck=newTruck; } @@ -27,46 +27,84 @@ void Node::setFatherNode(Node *newFatherNode){ fatherNode=newFatherNode; } +int Node::getKey(){ + return(this->key); +} + +string Node::getTruck(){ + return(this->truck); +} + +Node* Node::getLeftNode(){ + return(this->leftNode); +} + +Node* Node::getRightNode(){ + return(this->rightNode); +} + +Node* Node::getFatherNode(){ + return(this->fatherNode); +} + void Node::printInOrder(){ + std::cout << "printInOrder - 1" << std::endl; if(this->leftNode!=NULL){ - this->leftNode->printInOrder() + std::cout << "printInOrder - 2" << std::endl; + this->leftNode->printInOrder(); } + std::cout << "printInOrder - 3" << std::endl; std::cout << " " << this->truck << this->key << " " << std::endl; if(this->rightNode!=NULL){ - this->rightNode.printInOrder(); + std::cout << "printInOrder - 4" << std::endl; + this->rightNode->printInOrder(); } } // Funções de manipulação da árvore -Node* createNode(int key, char truck){ - Node *newNode=malloc(sizeof(Node)); - newNode.setkey(key); - newNode.setTruck(truck); - newNode.setLeftNode(NULL); - newNode.setRightNode(NULL); - newNode.setFatherNode(NULL); +Node* createNode(int key, string truck){ + Node *newNode=new Node; + std::cout << "createNode - 1" << std::endl; + newNode->setKey(key); + std::cout << "createNode - 2" << std::endl; + newNode->setTruck(truck); + std::cout << "createNode - 3" << std::endl; + newNode->setLeftNode(NULL); + std::cout << "createNode - 4" << std::endl; + newNode->setRightNode(NULL); + std::cout << "createNode - 5" << std::endl; + newNode->setFatherNode(NULL); return(newNode); } -void insertNode(Node *root, int key, char truck){ +void insertNode(Node *root, int key, string truck){ if(root==NULL){ + std::cout << "insertNode - 1" << std::endl; root=createNode(key,truck); - }else if(root.key>key){ - insertNode(root->leftNode); - }else if(root.key<key){ - insertNode(root->rightNode); + }else if(root->getKey() > key){ + std::cout << "insertNode - 2" << std::endl; + insertNode(root->getLeftNode(), key, truck); + }else if(root->getKey() < key){ + std::cout << "insertNode - 3" << std::endl; + insertNode(root->getRightNode(), key, truck); } + return; } Node* binarySearch(Node *root, int key){ - if(root.key==key){ + if(root==NULL){ + std::cout << "binarySearch - 1" << std::endl; + return(NULL); + }else if(root->getKey() > key){ + std::cout << "binarySearch - 2" << std::endl; + return(binarySearch(root->getLeftNode(), key)); + }else if(root->getKey() < key){ + std::cout << "binarySearch - 3" << std::endl; + return(binarySearch(root->getRightNode(), key)); + }else if(root->getKey() == key){ + std::cout << "binarySearch - 4" << std::endl; return(root); - }else if(root.key>key){ - return(binarySearch(root->leftNode)); - }else if(root.key<key){ - return(binarySearch(root->rightNode)); - }else if(root==NULL){ - return (NULL); } + return(NULL); } diff --git a/src/main.cpp b/src/main.cpp index 83d683f6f6fa347fdad45df846c651ddc3f9b0e0..f4297bdbc5d62c1d11bbc23e8d059baea985470c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,19 +8,19 @@ using namespace std; int main(int argc, char *argv[]) { Node *tree=NULL; - char *input, auxTruck; + string input, auxTruck; int auxKey; while (1) { - std::cin >> input; - if(strcmp(input,"insert")==0){ - std::cin >> auxTruck; - std::cin >> auxKey; - if(binarySearch(tree,auxKey)!=NULL){ + cin >> input; + if(input == "insert"){ + cin >> auxTruck; + cin >> auxKey; + if(binarySearch(tree,auxKey) == NULL){ insertNode(tree,auxKey,auxTruck); } - }else if(strcmp(input,"print")==0){ - tree.printInOrder; + }else if(input == "print"){ + tree->printInOrder(); } }