Java BinarySearchTrees: input key return value(lookup)
I'm trying to implement a database interface using BSTs. I have an inner class BTSEntry which represents a node with variables key, value and left/right nodes. Each left node is less(in alphabetical order) than its parent, while each right node is bigger than its parent.
The first problem is that I don't know what is the "nextNode()" in the Entry inner class supposed to be. Is it simply the right node? Or is it what I have done below?
private BinarySearchTreeEntry getLeftMost() {
BinarySearchTreeEntry n = this;
while (n.left != null) {
n = n.left;
}
return n;
}
public BinarySearchTreeEntry getNext() {
if (right != null) {
return right.getLeftMost();
} else {
BinarySearchTreeEntry n = this;
while (n.parent != null && n == n.parent.right) {
n = n.parent;
}
return n.parent;
}
}
The second problem is that I don't really know how to implement the "Int value get(Str key)" method. EDIT: I have tried to do the get(key) method. Is it correct? Will recursion work for this?
public Integer get(String key) throws NoSuchElementException {
BinarySearchTreeEntry curr = root;
if(curr == null){
return null;
} else if(curr.getKey().equals(key)){
return curr.getValue();
} else if(key.compareTo(curr.getKey()) < 0){
curr = curr.getLeft();
get(key);
} else{
curr = curr.getRight();
get(key);
}
return null;
}
Here is what I have done so far. Any help would be greatly appreciated! :)
package database;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Stack;
public class BinarySearchTree<K, V> implements Dictionary<String, Integer> {
private class BinarySearchTreeEntry
implements DictionaryEntry<String, Integer>{
private String key;
private Integer value;
private BinarySearchTreeEntry left;
private BinarySearchTreeEntry right;
private BinarySearchTreeEntry parent;
BinarySearchTreeEntry(String key, Integer value,
BinarySearchTreeEntry left,
BinarySearchTreeEntry right) {
this.key = key;
this.value = value;
this.left = left;
this.right = right;
if (left != null) left.parent = this;
if (right != null) right.parent = this;
}
private BinarySearchTreeEntry getLeftMost() {
BinarySearchTreeEntry n = this;
while (n.left != null) {
n = n.left;
}
return n;
}
private BinarySearchTr开发者_StackOverflow社区eeEntry getRightMost() {
BinarySearchTreeEntry n = this;
while (n.right != null) {
n = n.right;
}
return n;
}
public BinarySearchTreeEntry getNext() {
if (right != null) {
return right.getLeftMost();
} else {
BinarySearchTreeEntry n = this;
while (n.parent != null && n == n.parent.right) {
n = n.parent;
}
return n.parent;
}
}
public String getKey() {
return key;
}
public Integer getValue() {
return value;
}
public BinarySearchTreeEntry getLeft() {
return left;
}
public BinarySearchTreeEntry getRight() {
return right;
}
}
private class ListIterator
implements Iterator<DictionaryEntry<String, Integer>> {
private BinarySearchTreeEntry current;
Stack<BinarySearchTreeEntry> workList;
public ListIterator(BinarySearchTreeEntry entry){
current = entry;
}
public boolean hasNext() {
return current != null;
}
public BinarySearchTreeEntry next() {
BinarySearchTreeEntry result = null;
current = root;
while(current!=null){
workList.push(current);
current = current.getLeft();
}
if(!workList.isEmpty()){
result = (BinarySearchTreeEntry) workList.pop();
current = result.getRight();
}
return result;
}
public void remove() {
}
}
private BinarySearchTreeEntry root;
private int items;
public BinarySearchTree(){
root = null;
items = 0;
}
public int size() {
ListIterator iter = iterator();
while(iter.hasNext()){
items += 1;
}
return items;
}
public boolean isEmpty() {
return size() == 0;
}
public Integer get(String key) throws NoSuchElementException {
BinarySearchTreeEntry curr = root;
if(curr == null){
return null;
} else if(curr.getKey().equals(key)){
return curr.getValue();
} else if(key.compareTo(curr.getKey()) < 0){
//Now what?
}
return null;
}
public void put(String key, Integer value) {
}
public void clear() {
ListIterator iter = iterator();
BinarySearchTreeEntry curr;
curr = root;
while(iter.hasNext()){
remove(curr.getKey());
curr = iter.next();
}
remove(curr.getKey());
}
public void remove(String key) throws NoSuchElementException {
}
public ListIterator iterator() {
return (new ListIterator(root));
}
}
I don't know exactly what your getNext() method is supposed to do, but I'm guessing it should get the next biggest element. If that's the case, then it looks like what you're doing is correct.
For your second question, no, recursion will not work. You will (probably) want to use a loop that goes until either the current node has the key you are looking for (and return the value like you are doing), or until there is not a left or right node left to check (the curr node is null). Also, based on the method header, it looks like you will want to throw an exception if the key is not found rather than returning null. It looks like you've got the right conditions, you just need some minor changes to what it does when those conditions are true.
精彩评论