'BinarySearchTree - Wrong Result Search Method
If I search at BST for element for example 40, I get the value of the Node if it exists 39 or 41 otherwise I get the value from the correct node. I tried everything to fix it, but it still does not work. My Code:
@Override
public void insert(int key, String string) {
head = recursiveInsert(head,key,string);
}
public Node recursiveInsert(Node node, int key, String string) {
if (this.head == null) {
head = new SearchTree.Node(key,string);
} else {
if (head.key < key) {
head.left = recursiveInsert(head.left, key,string);
} else {
head.right = recursiveInsert(head.right,key,string);
}
}
return node;
}
@Override
public void delete(int key) {
}
@Override
public String search(int key) {
return recursiveSearch(head,key).value;
}
public Node recursiveSearch(SearchTree.Node node, int key){
if (head == null) {
return null;
} else if (head.key == key) {
return head;
} else {
if (key < head.key){
return recursiveSearch(head.left, key);
}else return recursiveSearch(head.right,key);
}
}
@Override
public long getCumulativeLengthOfSearchPaths() {
return 0;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
