update: implement search recursiv
This commit is contained in:
@@ -7,6 +7,37 @@ public class BinaryTree {
|
||||
insert(content);
|
||||
}
|
||||
|
||||
public boolean search(int value) {
|
||||
if (rootNode == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return searchRecursiv(rootNode, value);
|
||||
}
|
||||
|
||||
private boolean searchRecursiv(Node node, int value) {
|
||||
int content = node.getContent();
|
||||
|
||||
if (value == content) {
|
||||
return true;
|
||||
} else if (value > content) {
|
||||
Node leftNode = node.getLeftNode();
|
||||
if (leftNode == null) {
|
||||
return false;
|
||||
}
|
||||
return searchRecursiv(leftNode, value);
|
||||
} else if (value < content) {
|
||||
Node rightNode = node.getRightNode();
|
||||
if (rightNode == null) {
|
||||
return false;
|
||||
}
|
||||
return searchRecursiv(rightNode, value);
|
||||
} else {
|
||||
System.out.println("Something went wrong!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void insert(int content) {
|
||||
if (rootNode == null) {
|
||||
Node newNode = new Node(content);
|
||||
|
||||
Reference in New Issue
Block a user