Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
alg3-trab1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Harbor Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Eduardo Machado
alg3-trab1
Commits
4c0bbe82
Commit
4c0bbe82
authored
9 years ago
by
Eduardo Machado
Browse files
Options
Downloads
Patches
Plain Diff
objetos arrumados
parent
54a61b65
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
Makefile
+1
-2
1 addition, 2 deletions
Makefile
include/bst_lib.h
+19
-2
19 additions, 2 deletions
include/bst_lib.h
src/bst_lib.cpp
+60
-22
60 additions, 22 deletions
src/bst_lib.cpp
src/main.cpp
+8
-8
8 additions, 8 deletions
src/main.cpp
with
88 additions
and
34 deletions
Makefile
+
1
−
2
View file @
4c0bbe82
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
include/bst_lib.h
+
19
−
2
View file @
4c0bbe82
// 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
);
This diff is collapsed.
Click to expand it.
src/bst_lib.cpp
+
60
−
22
View file @
4c0bbe82
...
...
@@ -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
);
}
This diff is collapsed.
Click to expand it.
src/main.cpp
+
8
−
8
View file @
4c0bbe82
...
...
@@ -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
()
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment