如何检查两个二叉搜索树是否具有相同的确切键(忽略值)?

时间:2016-04-04 08:24:19

标签: java tree polymorphism key binary-tree

如何实现最后的方法?我已经实现了这个多态二叉搜索树的大部分开头部分,但无法弄清楚如何检查两棵树是否具有相同的密钥。密钥可以是任何顺序,但是两棵树需要具有相同的大小和相同的密钥(值无关紧要)。如果此键具有与otherTree相同的键,则此方法hasSameKeys返回一个布尔值(最底部的方法)。我首先检查树木是否具有相同的尺寸但是除此之外还不知道什么。我不能使用任何数组或其他Java库类,但我可能会添加辅助方法(可能是递归的)。建议?

@SuppressWarnings("unchecked")
public class NonEmptyTree<K extends Comparable<K>, V> implements Tree<K, V> {

    public K key;
    public V value;
    public Tree<K,V> leftTree;
    public Tree<K,V> rightTree;


    public NonEmptyTree(K key, V value, Tree<K,V> leftTree, 
            Tree<K,V> rightTree) {
        this.key=key;
        this.value=value;
        this.leftTree=leftTree;
        this.rightTree=rightTree;
    }

    public NonEmptyTree<K, V> addKeyWithValue(K keyToAdd, V valueToAdd) {
       if(keyToAdd.compareTo(key)==0) {
         value = valueToAdd;
       }

       if(keyToAdd.compareTo(key)>0) {
         rightTree = rightTree.addKeyWithValue(keyToAdd, valueToAdd);
       }

       if(keyToAdd.compareTo(key)<0) {
         leftTree = leftTree.addKeyWithValue(keyToAdd, valueToAdd);
       }

       return this;
    }

    public int size() {
       return 1 + leftTree.size() + rightTree.size();
    }

    public V lookup(K lookUpKey) {
       if(lookUpKey.compareTo(key)>0) {
          return this.rightTree.lookup(lookUpKey);
       }
       if(lookUpKey.compareTo(key)<0) {
          return this.leftTree.lookup(lookUpKey);
       }
       if(lookUpKey.compareTo(key)!=0) {
          return null;
       }
       return this.value;
    }

    public K max() throws EmptyTreeException {    
       try{
          K temp = this.rightTree.max();
          return temp;
       }
       catch(EmptyTreeException e) {
          return key;
       }
    }

    public K min() throws EmptyTreeException {    
       try{
            K temp = this.leftTree.min();
            return temp;
       }
       catch(EmptyTreeException e) {
            return key;
       }
    }

    public Tree<K, V> deleteKeyAndValue(K keyToDelete) {
       if(keyToDelete.compareTo(key)>0) {
          rightTree = rightTree.deleteKeyAndValue(keyToDelete);
       }

       if(keyToDelete.compareTo(key)<0) {
          leftTree = leftTree.deleteKeyAndValue(keyToDelete);
       }

       if(keyToDelete.compareTo(key)==0) {
          try{
              value = this.lookup(rightTree.min());
              key = rightTree.min();
          }
          catch(EmptyTreeException e) {
              return this.leftTree;
          }
       }
       return this;
    }

    public boolean haveSameKeys(Tree<K, V> otherTree) { 
        boolean check = true;

        if(this.size()!=otherTree.size()) {
           check = false;
        }
    }

    // Tests haveSameKeys() with two empty trees.
    @Test public void testPublic9() {
       Tree<Byte, Boolean> tree= EmptyTree.getInstance();
       Tree<Byte, Boolean> tree2= EmptyTree.getInstance();

       assertTrue(tree.haveSameKeys(tree2));
       assertTrue(tree2.haveSameKeys(tree));
    }

    // Tests haveSameKeys() with an empty tree and a nonempty tree
    @Test public void testPublic10() {
       Tree<String, Integer> tree= EmptyTree.getInstance();
       Tree<String, Integer> tree2= TestCode.exampleTree1();

       assertFalse(tree.haveSameKeys(tree2));
       assertFalse(tree2.haveSameKeys(tree));
    }

    // Tests haveSameKeys() with two nonempty trees that have the same keys.
    @Test public void testPublic11() {
       Tree<String, Integer> tree= TestCode.exampleTree1();
       Tree<String, Integer> tree2= TestCode.exampleTree1();

       assertTrue(tree.haveSameKeys(tree2));
       assertTrue(tree2.haveSameKeys(tree));
    }

1 个答案:

答案 0 :(得分:1)

您可以使用递归检查树中的每个键是否都包含在另一个树中,因为如果不存在此类密钥,lookup将返回null

为此,我做了一些假设:

  • EmptyTree.haveSameKeys返回otherTree.size() == 0
  • EmptyTree.hasSameKeys始终返回true
  • EmptyTree.lookup始终返回null
  

我可以将公共方法添加到Empty / NonEmpty树实现的Tree接口,但是如果我愿意的话......

因此,您需要将hasSameKeys添加到Tree界面:

// Checks if every key in 'this' tree is contained in 'otherTree'
public boolean hasSameKeys(Tree<K, V> otherTree) {
    if (otherTree.lookup(this.key) == null) { // if key does not exist
        return false;
    }

    return (leftTree.hasSameKeys(otherTree) && rightTree.hasSameKeys(otherTree));
}

public boolean haveSameKeys(Tree<K, V> otherTree) { // Both trees should have the same keyset
    return hasSameKeys(otherTree) && otherTree.hasSameKeys(this); 
}

这会带来什么:

  • EmptyTree和EmptyTree; true && true。 =&GT;空树具有相同的密钥集
  • NonEmptyTree和EmptyTree; false && true。 =&GT;或者相反。总是。
  • NonEmptyTree和NonEmptyTree; true =&gt;如果两者都具有完全相同的密钥集。