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

objetos arrumados

parent 54a61b65
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
// 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);
......@@ -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){
return(root);
}else if(root.key>key){
return(binarySearch(root->leftNode));
}else if(root.key<key){
return(binarySearch(root->rightNode));
}else if(root==NULL){
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);
}
return(NULL);
}
......@@ -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();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment