-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new branch with list and intermidiate solution of linked list #1
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,19 +16,71 @@ public BinaryTree() { | |||||||||||||||||
// Values that are greater than the value in the current node - should be placed in the right subtree | ||||||||||||||||||
public void add(int value) { | ||||||||||||||||||
// TODO implement me | ||||||||||||||||||
Node addNode = new Node(); | ||||||||||||||||||
addNode.value = value; | ||||||||||||||||||
addNode.left = null; | ||||||||||||||||||
addNode.right = null; | ||||||||||||||||||
if (root == null) { | ||||||||||||||||||
root = addNode; | ||||||||||||||||||
} else { | ||||||||||||||||||
Node thisNode = root; | ||||||||||||||||||
while (thisNode.right != null && thisNode.left != null) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrong loop logic, I guess it's better to check whether thisNode != null |
||||||||||||||||||
if (value > thisNode.value) thisNode = thisNode.right; | ||||||||||||||||||
if (value <= thisNode.value) thisNode = thisNode.left; | ||||||||||||||||||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use curly brackets for one-line if, please
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
if (value > thisNode.value) { | ||||||||||||||||||
thisNode.right = addNode; | ||||||||||||||||||
} else thisNode.left = addNode; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for else |
||||||||||||||||||
} | ||||||||||||||||||
size++; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
// Should remove specified value from tree and return true | ||||||||||||||||||
// If value does not exist in this tree - return false | ||||||||||||||||||
public boolean remove(int value) { | ||||||||||||||||||
// TODO implement me | ||||||||||||||||||
return false; | ||||||||||||||||||
Node removeNode = root; | ||||||||||||||||||
while (removeNode.value != value) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if removeNode == nul ?? |
||||||||||||||||||
if (value > removeNode.value) removeNode = removeNode.right; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||||||||||||||||||
else removeNode = removeNode.left; | ||||||||||||||||||
} | ||||||||||||||||||
if (removeNode.value != value) return false; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can set code formatting in IntelijiIdea as google code style. |
||||||||||||||||||
else { | ||||||||||||||||||
if (removeNode.right == null && removeNode.left == null) { | ||||||||||||||||||
removeNode = null; | ||||||||||||||||||
} else if (removeNode.right == null && removeNode.left != null) { | ||||||||||||||||||
removeNode = removeNode.left; | ||||||||||||||||||
} else if (removeNode.left == null && removeNode.right != null) { | ||||||||||||||||||
removeNode = removeNode.right; | ||||||||||||||||||
} else { | ||||||||||||||||||
Node replaceNode = removeNode.left; | ||||||||||||||||||
while (replaceNode.right != null) replaceNode = replaceNode.right; | ||||||||||||||||||
if (replaceNode.left == null) { | ||||||||||||||||||
removeNode.value = replaceNode.value; | ||||||||||||||||||
replaceNode = null; | ||||||||||||||||||
} else { | ||||||||||||||||||
removeNode.value = replaceNode.value; | ||||||||||||||||||
replaceNode = replaceNode.left; | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
size--; | ||||||||||||||||||
return true; | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
// Should return true if this tree contains specified value, false - otherwise | ||||||||||||||||||
public boolean contains(int value) { | ||||||||||||||||||
// TODO implement me | ||||||||||||||||||
return false; | ||||||||||||||||||
Node thisNode = root; | ||||||||||||||||||
while (thisNode.value != value) { | ||||||||||||||||||
if (value > thisNode.value) { | ||||||||||||||||||
thisNode = thisNode.right; | ||||||||||||||||||
} else { | ||||||||||||||||||
thisNode = thisNode.left; | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
if (thisNode.value == value) return true; | ||||||||||||||||||
else return false; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
public int getSize() { | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's redundant initialisation