Java 确定二元搜索树(递归)中的单词是否工作极慢/不准确
我不确定下面代码中的containsKey()方法有什么问题。如果单词在地图中,则返回true;如果单词不在地图中,则返回false。我有一个BSTMap类和一个内部TreeNode类(每个单词都是TreeNode键,单词出现的次数就是TreeNode值)。请帮忙!!我认为这与第183行中的递归有关,但我不知道我做错了什么。比较器应该能够加快查找单词所需的时间,因为它不会遍历整个树Java 确定二元搜索树(递归)中的单词是否工作极慢/不准确,java,recursion,binary-search-tree,Java,Recursion,Binary Search Tree,我不确定下面代码中的containsKey()方法有什么问题。如果单词在地图中,则返回true;如果单词不在地图中,则返回false。我有一个BSTMap类和一个内部TreeNode类(每个单词都是TreeNode键,单词出现的次数就是TreeNode值)。请帮忙!!我认为这与第183行中的递归有关,但我不知道我做错了什么。比较器应该能够加快查找单词所需的时间,因为它不会遍历整个树 /** * Jesse Aphridion * 04/04/2017 * BSTMap.java */
/**
* Jesse Aphridion
* 04/04/2017
* BSTMap.java
*/
import java.util.ArrayList;
import java.util.Comparator;
public class BSTMap<Key,Value> implements MyMap<Key,Value> {
private TreeNode<Key,Value> root;
private Comparator<Key> comparator;
private int size;
private int depth;
public BSTMap( Comparator<Key> comparator ) {
this.comparator = comparator;
this.root = null;
this.size = 0;
this.depth = 0;
}
public boolean containsKey( Key k ){
if ( this.root == null ){
return false;
}
else{
return root.containsKey( k, this.comparator );
// System.out.println( "if contain: " + root.ifContain );
// return root.ifContain;
}
}
public static void main( String[] args ) {
System.out.println( "test code" );
BSTMap<String,Integer> map = new BSTMap<String,Integer>(new AscendingStringComparator());
}
class TreeNode<Key,Value> {
private KeyValuePair<Key,Value> pair;
private TreeNode<Key,Value> left;
private TreeNode<Key,Value> right;
public boolean ifContain;
public TreeNode( Key this_key, Value this_val, TreeNode<Key,Value> l, TreeNode<Key,Value> r ) {
; // code here
this.pair = new KeyValuePair<Key, Value>(this_key, this_val);
this.left = l;
this.right = r;
this.ifContain = false;
}
public Key getKey(){
return this.pair.getKey();
}
public boolean containsKey( Key k, Comparator<Key> comparator ){ ////USE COMPARATORRRRRRR
this.ifContain = false;
int compRslt = comparator.compare(k, pair.getKey());
System.out.println( k );
if ( compRslt == 0 ){
System.out.println("comparator 0, set true");
this.ifContain = true;
return this.ifContain;
}
else if ( compRslt < 0 ){
if (left == null) {
System.out.println("comparator < 0, set false");
this.ifContain = false;
return this.ifContain;
}
else {
System.out.println("call left contains key");
return this.left.containsKey( k, comparator );
}
}
else{
if (right == null) {
System.out.println("right is null, set false");
this.ifContain = false;
return this.ifContain;
}
else {
System.out.println("call right contains key");
return this.right.containsKey( k, comparator );
}
}
//System.out.println( this.ifContain );
//return this.ifContain;
}
}
class AscendingStringComparator implements Comparator<String> {
public int compare( String i1, String i2 ) {
// returns negative number if i2 comes after i1 lexicographically
//0 if same
// pos number if i2 comes before i1
return i1.compareTo(i2);
}
}
/**
*杰西失语
* 04/04/2017
*BSTMap.java
*/
导入java.util.ArrayList;
导入java.util.Comparator;
公共类BSTMap实现MyMap{
独活根;
专用比较器;
私有整数大小;
私有整数深度;
公共BSTMap(比较器比较器){
这个比较器=比较器;
this.root=null;
此值为0.size=0;
这个深度=0;
}
公共布尔值containsKey(键k){
if(this.root==null){
返回false;
}
否则{
返回root.containsKey(k,this.comparator);
//System.out.println(“如果包含:+root.ifContain”);
//返回root.ifContain;
}
}
公共静态void main(字符串[]args){
System.out.println(“测试代码”);
BSTMap map=新的BSTMap(新的AscendingStringComparator());
}
三烯类{
私钥对;
私有树节点左;
私有树节点权;
公共布尔包含;
公共树节点(键入此键,值为此值,树节点l,树节点r){
;//这里的代码
this.pair=新的KeyValuePair(此密钥,此值);
这个左=l;
这个。右=r;
this.ifContain=false;
}
公钥getKey(){
返回此.pair.getKey();
}
public boolean containsKey(键k,Comparator Comparator){///使用comparatorrrrrr
this.ifContain=false;
int compRslt=comparator.compare(k,pair.getKey());
系统输出println(k);
如果(压缩==0){
System.out.println(“比较器0,设置为真”);
this.ifContain=true;
返回此.ifContain;
}
否则如果(压缩<0){
if(left==null){
System.out.println(“比较器<0,设置为false”);
this.ifContain=false;
返回此.ifContain;
}
否则{
System.out.println(“调用左键包含键”);
返回this.left.containsKey(k,comparator);
}
}
否则{
if(right==null){
System.out.println(“right为null,set为false”);
this.ifContain=false;
返回此.ifContain;
}
否则{
System.out.println(“调用权包含键”);
返回这个.right.containsKey(k,comparator);
}
}
//System.out.println(this.ifContain);
//返回此.ifContain;
}
}
类AscendingStringComparator实现Comparator{
公共整数比较(字符串i1、字符串i2){
//如果按字典顺序i2位于i1之后,则返回负数
//如果相同,则为0
//如果i2在i1之前,则为pos编号
返回i1。比较(i2);
}
}
要读取的代码太多。请尝试将代码减少到问题所在。减少到相关代码!要读取的代码太多。尝试将代码减少到问题所在。减少到相关代码!