diff --git a/lib/bst_lib.h b/lib/bst_lib.h index 4a4c4b12b8f2eb9944bef26199e57ed715faa7e4..2aeccbac16eeb6b4e07261f7e2741529ec84c2dd 100644 --- a/lib/bst_lib.h +++ b/lib/bst_lib.h @@ -15,10 +15,5 @@ class Node{ void setLeftNode(Node *newLeftNode); void setRightNode(Node *newRightNode); void setFatherNode(Node *newRatherNode); - - int getKey(); - char getTruck(); - Node* getLeftNode(); - Node* getRightNode(); - Node* getFatherNode(); -} + void printInOrder(); +}; diff --git a/src/bst_lib.cpp b/src/bst_lib.cpp index 19eff2a74a9ae4e9a6fecdfc6eaa523c04dbb78b..0164a3317960e8e5c8fa49c46e21722339245428 100644 --- a/src/bst_lib.cpp +++ b/src/bst_lib.cpp @@ -2,7 +2,7 @@ // 2015 #include <iostream> -#include "bst_lib.h" +#include "lib/bst_lib.h" using namespace std; @@ -22,28 +22,51 @@ void Node::setLeftNode(Node *newLeftNode){ void Node::setRightNode(Node *newRightNode){ rightNode=newRightNode; } + void Node::setFatherNode(Node *newFatherNode){ fatherNode=newFatherNode; } -int Node::getKey(){ - return(this->key); +void Node::printInOrder(){ + if(this->leftNode!=NULL){ + this->leftNode.printInOrder() + } + std::cout << " " << this->truck << this->key << " " << std::endl; + if(this->rightNode!=NULL){ + this->rightNode.printInOrder(); + } } -char Node::getTruck(){ - return(this->truck); -} +// Funções de manipulação da árvore -Node* Node::getLeftNode(){ - return(this->leftNode); +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); + return(newNode); } -Node* Node::getRightNode(){ - return(this->rightNode); +void insertNode(Node *root, int key, char truck){ + if(root==NULL){ + root=createNode(key,truck); + }else if(root.key>key){ + insertNode(root->leftNode); + }else if(root.key<key){ + insertNode(root->rightNode); + } } -Node* Node::getFatherNode(){ - return(this->fatherNode); +Node* binarySearch(Node *root, int key){ + if(root.key==key){ + 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); + } } - -// Funções de manipulação da árvore diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eeb0ff4e4594a530c85addfb6fab7fd460d633ef --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,28 @@ +// Implementado por Eduardo Machado +// 2015 + +#include <iostream> +#include "lib/bst_lib.h" + +using namespace std; + +int main(int argc, char *argv[]) { + Node *tree=NULL; + char *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){ + insertNode(tree,auxKey,auxTruck); + } + }else if(strcmp(input,"print")==0){ + tree.printInOrder; + } + } + + return 0; +}