Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Maycon Santos
BST
Commits
d2370cc4
Commit
d2370cc4
authored
May 27, 2018
by
Emanoel Dantas Pereira
Browse files
Ultima Versao
parent
2ee7cd21
Changes
2
Hide whitespace changes
Inline
Side-by-side
arquivoDeComandos
View file @
d2370cc4
...
...
@@ -18,4 +18,4 @@ REMOVA 8
IMPRIMA
MEDIANA
CHEIA
COMPLETA
\ No newline at end of file
COMPLETA
include/BinarySearchTree.h
View file @
d2370cc4
...
...
@@ -16,6 +16,7 @@
#include
<stack>
// stack
#include
<queue>
// queue
#include
<math.h>
//pow
using
namespace
std
;
...
...
@@ -398,10 +399,9 @@ class BinarySearchTree{
alturaAntigaDaArvore
=
this
->
raiz
->
altura
;
atualizarAlturaDosNos
(
newNode
);
if
(
this
->
raiz
->
altura
>
alturaAntigaDaArvore
){
/// SE A ÁRVORE CRESCEU EM ALTURA
quantidadeDeElementosMinima
*=
2
;
quantidadeDeElementosMaxima
+
=
(
(
this
->
raiz
->
altura
-
1
)
*
2
)
;
quantidadeDeElementosMaxima
=
pow
(
2
,
this
->
raiz
->
altura
)
-
1
;
}
}
...
...
@@ -415,7 +415,8 @@ class BinarySearchTree{
if
(
this
->
raiz
->
altura
>
alturaAntigaDaArvore
){
/// SE A ÁRVORE CRESCEU EM ALTURA
quantidadeDeElementosMinima
*=
2
;
quantidadeDeElementosMaxima
+=
(
(
this
->
raiz
->
altura
-
1
)
*
2
);
quantidadeDeElementosMaxima
=
pow
(
2
,
this
->
raiz
->
altura
)
-
1
;
}
}
...
...
@@ -470,33 +471,24 @@ class BinarySearchTree{
Node
*
nodeAlvo
=
this
->
raiz
;
/// BUSCANDO SE O NÓ EXISTE
while
(
nodeAlvo
!=
nullptr
&&
value
!=
nodeAlvo
->
chave
){
cout
<<
endl
<<
"nodeAlvo(485): "
<<
nodeAlvo
->
chave
<<
" n_esquerda: "
<<
nodeAlvo
->
n_esquerda
<<
" n_direita: "
<<
nodeAlvo
->
n_direita
<<
endl
<<
endl
;
while
(
nodeAlvo
!=
nullptr
&&
value
!=
nodeAlvo
->
chave
){
if
(
value
<
nodeAlvo
->
chave
){
/// Value MENOR QUE CHAVE DO PONTEIRO ATUAL
nodeAlvo
=
nodeAlvo
->
esquerda
;
nodeAlvo
=
nodeAlvo
->
esquerda
;
}
else
{
/// Value MAIOR QUE CHAVE DO PONTEIRO ATUAL
nodeAlvo
=
nodeAlvo
->
direita
;
nodeAlvo
=
nodeAlvo
->
direita
;
}
}
if
(
nodeAlvo
==
nullptr
){
/// O NÓ CORRESPONDENTE A CHAVE NÃO FOI ENCONTRADO NA ÁRVORE
return
false
;
}
if
(
nodeAlvo
->
esquerda
==
nullptr
){
/// O NÓ nodeAlvo A SER REMOVIDO **NÃO TEM** FILHO À ESQUERDA
cout
<<
endl
<<
"nodeAlvo(493): "
<<
nodeAlvo
->
chave
<<
" n_esquerda: "
<<
nodeAlvo
->
n_esquerda
<<
" n_direita: "
<<
nodeAlvo
->
n_direita
<<
endl
<<
endl
;
transplant
(
nodeAlvo
,
nodeAlvo
->
direita
);
}
else
if
(
nodeAlvo
->
direita
==
nullptr
){
/// O NÓ nodeAlvo A SER REMOVIDO **NÃO TEM** FILHO À DIREITA
transplant
(
nodeAlvo
,
nodeAlvo
->
esquerda
);
...
...
@@ -514,43 +506,56 @@ class BinarySearchTree{
transplant
(
nodeAlvo
,
menorMaiorNoh
);
menorMaiorNoh
->
esquerda
=
nodeAlvo
->
esquerda
;
menorMaiorNoh
->
esquerda
=
menorMaiorNoh
;
raiz
->
n_esquerda
--
;
}
--
(
this
->
quantidadeDeElementos
);
/// DECREMENTANDO NA QUANTIDADE TOTAL DE NÓS DA ÁRVORE
alturaAntigaDaArvore
=
this
->
raiz
->
altura
;
atualizarAlturaDosNos
(
nodeAlvo
->
p
);
Node
*
nodeAlvo2
=
nodeAlvo
->
p
;
//atualizando a altura da arvore
while
(
nodeAlvo2
!=
nullptr
){
if
(
nodeAlvo2
->
esquerda
!=
nullptr
&&
nodeAlvo2
->
direita
!=
nullptr
){
if
(
nodeAlvo2
->
esquerda
->
altura
>
nodeAlvo2
->
direita
->
altura
){
nodeAlvo2
->
altura
=
nodeAlvo2
->
esquerda
->
altura
+
1
;
}
else
{
nodeAlvo2
->
altura
=
nodeAlvo2
->
direita
->
altura
+
1
;
}
}
else
if
(
nodeAlvo2
->
esquerda
!=
nullptr
){
nodeAlvo2
->
altura
=
nodeAlvo2
->
esquerda
->
altura
+
1
;
}
else
if
(
nodeAlvo2
->
direita
!=
nullptr
){
nodeAlvo2
->
altura
=
nodeAlvo2
->
direita
->
altura
+
1
;
if
(
this
->
raiz
->
altura
>
alturaAntigaDaArvore
){
/// SE A ÁRVORE CRESCEU EM ALTURA
}
else
{
nodeAlvo2
->
altura
=
1
;
}
nodeAlvo2
=
nodeAlvo2
->
p
;
}
if
(
this
->
raiz
->
altura
<
alturaAntigaDaArvore
){
/// SE A ÁRVORE CRESCEU EM ALTURA
quantidadeDeElementosMinima
/=
2
;
quantidadeDeElementosMaxima
-
=
(
(
this
->
raiz
->
altura
-
1
)
*
2
)
;
quantidadeDeElementosMaxima
=
pow
(
2
,
this
->
raiz
->
altura
)
-
1
;
}
nodeAlvo
=
nodeAlvo
->
p
;
while
(
nodeAlvo
!=
raiz
){
/// SUBINDO PELA ÁRVORE
cout
<<
endl
<<
"nodeAlvo(534): "
<<
nodeAlvo
->
chave
<<
" n_esquerda: "
<<
nodeAlvo
->
n_esquerda
<<
" n_direita: "
<<
nodeAlvo
->
n_direita
<<
endl
<<
endl
;
if
(
nodeAlvo
->
chave
==
nodeAlvo
->
p
->
esquerda
->
chave
){
/// Value MENOR QUE CHAVE DO PONTEIRO ATUAL
--
nodeAlvo
->
n_esquerda
;
cout
<<
endl
<<
"nodeAlvo(540): "
<<
nodeAlvo
->
chave
<<
" n_esquerda: "
<<
nodeAlvo
->
n_esquerda
<<
" n_direita: "
<<
nodeAlvo
->
n_direita
<<
endl
<<
endl
;
if
(
nodeAlvo
->
chave
==
nodeAlvo
->
p
->
esquerda
->
chave
){
--
nodeAlvo
->
n_esquerda
;
}
else
{
--
nodeAlvo
->
n_direita
;
--
nodeAlvo
->
n_direita
;
}
nodeAlvo
=
nodeAlvo
->
p
;
}
raiz
->
n_esquerda
--
;
return
true
;
}
...
...
@@ -598,7 +603,7 @@ cout << endl << "nodeAlvo(540): " << nodeAlvo->chave <<
alvo
->
p
->
altura
+=
1
;
}
else
if
(
alvo
==
alvo
->
p
->
esquerda
){
/// O NÓ **ALVO É O FILHO ESQUERDO** DO PAI
if
(
alvo
->
altura
>
alvo
->
p
->
direita
->
altura
){
/// COMPANDO NÓ ALVO COM O FILHO DIREITO DO PAI
if
(
alvo
->
altura
>
alvo
->
p
->
direita
->
altura
){
/// COMPA
RA
NDO NÓ ALVO COM O FILHO DIREITO DO PAI
alvo
->
p
->
altura
+=
1
;
}
else
{
...
...
@@ -614,8 +619,7 @@ cout << endl << "nodeAlvo(540): " << nodeAlvo->chave <<
}
}
alvo
=
alvo
->
p
;
alvo
=
alvo
->
p
;
}
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment