Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/org/way2it/data_structures/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 54 additions & 2 deletions src/org/way2it/data_structures/binary_tree/BinaryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +21 to +22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's redundant initialisation

Suggested change
addNode.left = null;
addNode.right = null;

if (root == null) {
root = addNode;
} else {
Node thisNode = root;
while (thisNode.right != null && thisNode.left != null) {

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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 = thisNode.right;
if (value <= thisNode.value) thisNode = thisNode.left;
if (value > thisNode.value) {
thisNode = thisNode.right;
}
if (value <= thisNode.value) {
thisNode = thisNode.left;
}

}
if (value > thisNode.value) {
thisNode.right = addNode;
} else thisNode.left = addNode;

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if removeNode == nul ??
Please, recheck loop condition

if (value > removeNode.value) removeNode = removeNode.right;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

else removeNode = removeNode.left;
}
if (removeNode.value != value) return false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can set code formatting in IntelijiIdea as google code style.
And then you can format code in right way
https://stackoverflow.com/questions/42979700/how-to-configure-google-java-code-formatter-in-intellij-idea-17

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() {
Expand Down
1 change: 0 additions & 1 deletion src/org/way2it/data_structures/linked_list/LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package org.way2it.data_structures.linked_list;

public class LinkedList {

// Represents the first node of this list
Expand Down