Skip to content
Snippets Groups Projects
Commit 51dbeed1 authored by Eduardo Machado's avatar Eduardo Machado
Browse files

andando lentamente

parent 4c0bbe82
Branches
No related tags found
No related merge requests found
bst 0 → 100755
File added
......@@ -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();
}
File added
File added
......@@ -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);
}
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment