diff --git a/bst b/bst index 8d8a007d49b6d11b2d787706b3f8eefa89eaa5c4..e8d3bae141ce5eca528de1da897443cd5fc7bdec 100755 Binary files a/bst and b/bst differ diff --git a/include/bst_lib.h b/include/bst_lib.h index aa1e147ec05f739c1124ee265e6182a9cbea8daf..e174a8d08b49040d3802fce1501c1f8c14e7f911 100644 --- a/include/bst_lib.h +++ b/include/bst_lib.h @@ -25,14 +25,14 @@ class Node{ Node* getLeftNode(); Node* getRightNode(); Node* getFatherNode(); + + bool printInOrder(); }; class Tree{ - private: - Node* root; public: - void createNode(int key, string truck, Node *father); - void insertNode(int key, string truck, Node *father); - Node* binarySearch(int key); - void printInOrder(); -} + const Node *root; + Node* createNode(int key, string truck, Node *father); + bool insertNode(int key, string truck, Node *father, Node *current); + Node* binarySearch(int key, Node *current); +}; diff --git a/lib/bst_lib.a b/lib/bst_lib.a index 056cce11464e5b3d7728331b543beea4ef418121..484b95738fdf068abadd705af671f782108a700d 100644 Binary files a/lib/bst_lib.a and b/lib/bst_lib.a differ diff --git a/obj/bst_lib.o b/obj/bst_lib.o index a0d54764a627c5fe3cea02c25946252311d3da12..2e8af15074e4149ab478669e7dba59e563592235 100644 Binary files a/obj/bst_lib.o and b/obj/bst_lib.o differ diff --git a/src/bst_lib.cpp b/src/bst_lib.cpp index 4630d1d804fc253f20f7cffcc15a031f4e01c8ad..0aa816862c078c624746bb0b75056b57316226b4 100644 --- a/src/bst_lib.cpp +++ b/src/bst_lib.cpp @@ -49,7 +49,10 @@ Node* Node::getFatherNode(){ // Funções de manipulação da árvore -void Tree::printInOrder(){ +bool Node::printInOrder(){ + if(this==NULL){ + return 0; + } std::cout << "printInOrder - 1" << std::endl; if(leftNode!=NULL){ std::cout << "printInOrder - 2" << std::endl; @@ -61,52 +64,54 @@ void Tree::printInOrder(){ std::cout << "printInOrder - 4" << std::endl; rightNode->printInOrder(); } - return; + return 1; } -void Tree::createNode(int key, string truck, Node *father){ - root = new Node; +Node* Tree::createNode(int key, string truck, Node *father){ + Node *aux=new Node; std::cout << "createNode - 1" << std::endl; - root->setKey(key); - std::cout << "createNode - 2" << std::endl; - root->setTruck(truck); + aux->setKey(key); + std::cout << "createNode - 2 " << key << std::endl; + aux->setTruck(truck); std::cout << "createNode - 3" << std::endl; - root->setLeftNode(NULL); + aux->setLeftNode(NULL); std::cout << "createNode - 4" << std::endl; - root->setRightNode(NULL); + aux->setRightNode(NULL); std::cout << "createNode - 5" << std::endl; - root->setFatherNode(father); - return; + aux->setFatherNode(father); + return aux; } -// ARRUMAR O insertNode -void Tree::insertNode(int key, string truck, Node *father){ - if(root==NULL){ +bool Tree::insertNode(int key, string truck, Node *father, Node *current){ + if(current==NULL){ std::cout << "insertNode - 1" << std::endl; - createNode(key, truck, root->father); - }else if(root->getKey() > key){ + current=createNode(key, truck, father); + }else if(current->getKey() > key){ std::cout << "insertNode - 2" << std::endl; - root->leftNode->insertNode(key, truck, root); - }else if(root->getKey() < key){ + insertNode(key, truck, current, current->getLeftNode()); + }else if(current->getKey() < key){ std::cout << "insertNode - 3" << std::endl; - root->rightNode->insertNode(key, truck, root); + insertNode(key, truck, current, current->getRightNode()); + }else{ + std::cout << "Chave ocupada!" << std::endl; + return 0; } - return; + return 1; } -Node* Tree::binarySearch(int key){ - if(this==NULL){ +Node* Tree::binarySearch(int key, Node *current){ + if(current==NULL){ std::cout << "binarySearch - 1" << std::endl; return(NULL); - }else if(getKey() > key){ + }else if(current->getKey() > key){ std::cout << "binarySearch - 2" << std::endl; - return(leftNode->binarySearch(key)); - }else if(getKey() < key){ + return(binarySearch(key,current->getLeftNode())); + }else if(current->getKey() < key){ std::cout << "binarySearch - 3" << std::endl; - return(rightNode->binarySearch(key)); - }else if(getKey() == key){ + return(binarySearch(key, current->getRightNode())); + }else if(current->getKey() == key){ std::cout << "binarySearch - 4" << std::endl; - return(this); + return(current); } return(NULL); } diff --git a/src/main.cpp b/src/main.cpp index e711e90d9881d6224527f41450907ce95373da62..30c3234c141ca51e3114672bd358ecc04310e162 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,20 +7,22 @@ using namespace std; int main(int argc, char *argv[]) { - Node *tree=NULL; + Tree binaryTree; string input, auxTruck; int auxKey; + binaryTree.root=NULL; + while (1) { cin >> input; if(input == "insert"){ cin >> auxTruck; cin >> auxKey; - if(thee->binarySearch(auxKey) == NULL){ - tree->insertNode(tree,auxKey,auxTruck); + if(binaryTree.binarySearch(auxKey, binaryTree.root) == NULL){ + binaryTree.insertNode(auxKey, auxTruck, NULL, binaryTree.root); } }else if(input == "print"){ - tree->printInOrder(); + binaryTree.root->printInOrder(); } }