【发布时间】:2011-11-02 21:18:14
【问题描述】:
我有以下情况:我有很多BSTs,我想合并同构子树以节省空间。
我将二叉搜索树节点散列成一个“唯一表”——基本上是 BST 节点的散列。
具有相同左右子节点和相同键的节点具有相同的哈希码,并且我已经适当地覆盖了节点类的等于。
一切正常,除了计算哈希值很昂贵 - 它涉及计算子节点的哈希值。
我想缓存一个节点的哈希值。我遇到的问题是这样做的自然方式,从节点到整数的 HashMap 本身会调用节点上的哈希函数。
我通过在节点中声明一个新字段来解决这个问题,我用它来存储哈希码。但是,我觉得这不是正确的解决方案。
我真正想要的是使用使用节点地址的哈希将节点映射到它们的哈希码。我想我可以通过制作 HashMap 并将节点转换为对象来做到这一点,然后它会在对象上调用 hashCode 方法,但这不起作用(插入哈希仍然调用节点哈希和相等函数。
我希望深入了解实现节点以散列代码缓存的最佳方式。我在下面附上了代码,说明了下面发生的事情。
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;
class Bst {
int key;
String name;
Bst left;
Bst right;
public Bst( int k, String name, Bst l, Bst r ) {
this.key = k;
this.name = name;
this.left = l;
this.right = r;
}
public String toString() {
String l = "";
String r = "";
if ( left != null ) {
l = left.toString();
}
if ( right != null ) {
r = right.toString();
}
return key + ":" + name + ":" + l + ":" + r;
}
@Override
public boolean equals( Object o ) {
System.out.println("calling Bst's equals");
if ( o == null ) {
return false;
}
if ( !(o instanceof Bst) ) {
return false;
}
Bst n = (Bst) o;
if ( n == null || n.key != key ) {
return false;
} else if ( n.left != null && left == null || n.right != null && right == null ||
n.left == null & left != null || n.right == null && right != null ) {
return false;
} else if ( n.left != null && n.right == null ) {
return n.left.equals( left );
} else if ( n.left != null && n.right != null ) {
return n.left.equals( left ) && n.right.equals( right );
} else if ( n.left == null && n.right != null ) {
return n.right.equals( right );
} else {
return true;
}
}
@Override
public int hashCode() {
// the real hash function is more complex, entails
// calling hashCode on children if they are not null
System.out.println("calling Bst's hashCode");
return key;
}
}
public class Hashing {
static void p(String s) { System.out.println(s); }
public static void main( String [] args ) {
Set<Bst> aSet = new HashSet<Bst>();
Bst a = new Bst(1, "a", null, null );
Bst b = new Bst(2, "b", null, null );
Bst c = new Bst(3, "c", null, null );
Bst d = new Bst(1, "d", null, null );
a.left = b;
a.right = c;
d.left = b;
d.right = c;
aSet.add( a );
if ( aSet.contains( d ) ) {
p("d is a member of aSet");
} else {
p("d is a not member of aSet");
}
if ( a.equals( d ) ) {
p("a and d are equal");
} else {
p("a and d are not equal");
}
// now try casts to objects to avoid calling Bst's HashCode and equals
Set<Object> bSet = new HashSet<Object>();
Object foo = new Bst( a.key, a.name, a.left, a.right );
Object bar = new Bst( a.key, a.name, a.left, a.right );
bSet.add( foo );
p("added foo");
if ( bSet.contains( bar ) ) {
p("bar is a member of bSet");
} else {
p("bar is a not member of bSet");
}
}
}
【问题讨论】:
标签: java overriding equals hashcode superclass