search方法返回值java_Java BinarySearchTrees:輸入鍵返回值(查找)

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.

我正在嘗試使用BST實現數據庫接口。我有一個內部類BTSEntry,它表示一個帶有變量鍵,值和左/右節點的節點。每個左節點都比其父節點少(按字母順序排列),而每個右節點都比其父節點大。

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?

第一個問題是我不知道Entry內部類中的“nextNode()”應該是什么。它只是正確的節點嗎?或者是我在下面做了什么?

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?

第二個問題是我真的不知道如何實現“Int value get(Str key)”方法。編輯:我試圖做get(key)方法。這是對的嗎?遞歸會為此工作嗎?

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 implements Dictionary {

private class BinarySearchTreeEntry

implements DictionaryEntry{

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 BinarySearchTreeEntry 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> {

private BinarySearchTreeEntry current;

Stack 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));

}

}

1 个解决方案

#1

0

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.

我不確切知道你的getNext()方法應該做什么,但我猜它應該得到下一個最大的元素。如果是這種情況,那么看起來你正在做的事情是正確的。

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.

對於你的第二個問題,不,遞歸將不起作用。您(可能)想要使用一個循環,直到當前節點具有您正在查找的鍵(並返回您正在執行的值),或者直到沒有左側或右側節點進行檢查( curr節點為null)。此外,基於方法標頭,如果找不到密鑰而不是返回null,您似乎希望拋出異常。看起來你已經擁有了合適的條件,當這些條件成立時你只需要對它的作用進行一些細微的改動。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值