Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Isaac Xu
ex08-verifyavl-public
Commits
eaada400
Commit
eaada400
authored
Jan 29, 2022
by
Isaac Xu
Browse files
simplified
parent
4e3efe45
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/verifyavl/VerifyAVL.java
View file @
eaada400
...
...
@@ -6,24 +6,12 @@ public class VerifyAVL {
return
true
;
}
boolean
isBst
=
verifyBst
(
root
,
null
,
null
);
//int hDiff = verifyBalance(root);
//boolean isBalanced = true;
//if (hDiff > 1) {
//isBalanced = false;
//}
//if (root.height != Math.max(root.right.height, root.left.height) + 1) {
//return false;
//}
boolean
isBalanced
=
verifyBalance
(
root
);
if
(!
isBst
||
!
isBalanced
)
{
return
false
;
}
return
true
;
return
verifyBstAndBalance
(
root
,
null
,
null
);
}
/* TODO: Add private methods if you want (recommended) */
private
static
boolean
verifyBst
(
AVLNode
root
,
Integer
low
,
Integer
high
)
{
private
static
boolean
verifyBst
AndBalance
(
AVLNode
root
,
Integer
low
,
Integer
high
)
{
if
(
root
==
null
)
{
return
true
;
}
...
...
@@ -31,18 +19,11 @@ public class VerifyAVL {
if
((
low
!=
null
&&
root
.
key
<=
low
)
||
(
high
!=
null
&&
root
.
key
>=
high
))
{
return
false
;
}
// The left and right subtree must also be valid.
return
verifyBst
(
root
.
right
,
root
.
key
,
high
)
&&
verifyBst
(
root
.
left
,
low
,
root
.
key
);
}
private
static
boolean
verifyBalance
(
AVLNode
root
)
{
if
(
root
==
null
)
{
return
true
;
}
if
(!
verifyNode
(
root
))
{
return
false
;
}
return
verifyBalance
(
root
.
left
)
&&
verifyBalance
(
root
.
right
);
// The left and right subtree must also be valid.
return
verifyBstAndBalance
(
root
.
right
,
root
.
key
,
high
)
&&
verifyBstAndBalance
(
root
.
left
,
low
,
root
.
key
);
}
private
static
boolean
verifyNode
(
AVLNode
root
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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