diff --git a/bst b/bst new file mode 100755 index 0000000000000000000000000000000000000000..8d8a007d49b6d11b2d787706b3f8eefa89eaa5c4 Binary files /dev/null and b/bst differ diff --git a/include/bst_lib.h b/include/bst_lib.h index 6dbde87be1b6a7fd818f85889ef0529835fd276b..aa1e147ec05f739c1124ee265e6182a9cbea8daf 100644 --- a/include/bst_lib.h +++ b/include/bst_lib.h @@ -25,12 +25,14 @@ class Node{ 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); +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(); +} diff --git a/lib/bst_lib.a b/lib/bst_lib.a new file mode 100644 index 0000000000000000000000000000000000000000..056cce11464e5b3d7728331b543beea4ef418121 Binary files /dev/null and b/lib/bst_lib.a differ diff --git a/obj/bst_lib.o b/obj/bst_lib.o new file mode 100644 index 0000000000000000000000000000000000000000..a0d54764a627c5fe3cea02c25946252311d3da12 Binary files /dev/null and b/obj/bst_lib.o differ diff --git a/src/bst_lib.cpp b/src/bst_lib.cpp index d1fa51f718cd51bf6d2c5c7757d100465d4bb256..4630d1d804fc253f20f7cffcc15a031f4e01c8ad 100644 --- a/src/bst_lib.cpp +++ b/src/bst_lib.cpp @@ -28,83 +28,85 @@ void Node::setFatherNode(Node *newFatherNode){ } int Node::getKey(){ - return(this->key); + return(key); } string Node::getTruck(){ - return(this->truck); + return(truck); } Node* Node::getLeftNode(){ - return(this->leftNode); + return(leftNode); } Node* Node::getRightNode(){ - return(this->rightNode); + return(rightNode); } Node* Node::getFatherNode(){ - return(this->fatherNode); + return(fatherNode); } -void Node::printInOrder(){ +// Funções de manipulação da árvore + +void Tree::printInOrder(){ std::cout << "printInOrder - 1" << std::endl; - if(this->leftNode!=NULL){ + if(leftNode!=NULL){ std::cout << "printInOrder - 2" << std::endl; - this->leftNode->printInOrder(); + leftNode->printInOrder(); } std::cout << "printInOrder - 3" << std::endl; - std::cout << " " << this->truck << this->key << " " << std::endl; - if(this->rightNode!=NULL){ + std::cout << " " << truck << key << " " << std::endl; + if(rightNode!=NULL){ std::cout << "printInOrder - 4" << std::endl; - this->rightNode->printInOrder(); + rightNode->printInOrder(); } + return; } -// Funções de manipulação da árvore - -Node* createNode(int key, string truck){ - Node *newNode=new Node; +void Tree::createNode(int key, string truck, Node *father){ + root = new Node; std::cout << "createNode - 1" << std::endl; - newNode->setKey(key); + root->setKey(key); std::cout << "createNode - 2" << std::endl; - newNode->setTruck(truck); + root->setTruck(truck); std::cout << "createNode - 3" << std::endl; - newNode->setLeftNode(NULL); + root->setLeftNode(NULL); std::cout << "createNode - 4" << std::endl; - newNode->setRightNode(NULL); + root->setRightNode(NULL); std::cout << "createNode - 5" << std::endl; - newNode->setFatherNode(NULL); - return(newNode); + root->setFatherNode(father); + return; } -void insertNode(Node *root, int key, string truck){ +// ARRUMAR O insertNode +void Tree::insertNode(int key, string truck, Node *father){ if(root==NULL){ std::cout << "insertNode - 1" << std::endl; - root=createNode(key,truck); + createNode(key, truck, root->father); }else if(root->getKey() > key){ std::cout << "insertNode - 2" << std::endl; - insertNode(root->getLeftNode(), key, truck); + root->leftNode->insertNode(key, truck, root); }else if(root->getKey() < key){ std::cout << "insertNode - 3" << std::endl; - insertNode(root->getRightNode(), key, truck); + root->rightNode->insertNode(key, truck, root); } return; } -Node* binarySearch(Node *root, int key){ - if(root==NULL){ +Node* Tree::binarySearch(int key){ + if(this==NULL){ std::cout << "binarySearch - 1" << std::endl; return(NULL); - }else if(root->getKey() > key){ + }else if(getKey() > key){ std::cout << "binarySearch - 2" << std::endl; - return(binarySearch(root->getLeftNode(), key)); - }else if(root->getKey() < key){ + return(leftNode->binarySearch(key)); + }else if(getKey() < key){ std::cout << "binarySearch - 3" << std::endl; - return(binarySearch(root->getRightNode(), key)); - }else if(root->getKey() == key){ + return(rightNode->binarySearch(key)); + }else if(getKey() == key){ std::cout << "binarySearch - 4" << std::endl; - return(root); + return(this); } return(NULL); } diff --git a/src/main.cpp b/src/main.cpp index f4297bdbc5d62c1d11bbc23e8d059baea985470c..e711e90d9881d6224527f41450907ce95373da62 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,8 +16,8 @@ int main(int argc, char *argv[]) { if(input == "insert"){ cin >> auxTruck; cin >> auxKey; - if(binarySearch(tree,auxKey) == NULL){ - insertNode(tree,auxKey,auxTruck); + if(thee->binarySearch(auxKey) == NULL){ + tree->insertNode(tree,auxKey,auxTruck); } }else if(input == "print"){ tree->printInOrder();