简体   繁体   English

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

[英]How to check if two binary search trees have the same exact keys (ignoring values)?

HOW TO IMPLEMENT LAST METHOD?如何实施最后一种方法? I have implemented most of the beginning parts of this polymorphic binary search tree but can't figure out how to check that two trees have the same keys.我已经实现了这个多态二叉搜索树的大部分开始部分,但无法弄清楚如何检查两棵树是否具有相同的键。 Keys could be in any order but the two trees need to have identical size and identical keys (values don't matter).键可以按任何顺序排列,但两棵树需要具有相同的大小和相同的键(值无关紧要)。 This method haveSameKeys returns a boolean (method at very bottom) if this has the same keys as otherTree.如果此方法与 otherTree 具有相同的键,则该方法 hasSameKeys 返回一个布尔值(最底部的方法)。 I first check if the trees have the same size but don't know anything further than this.我首先检查树木是否具有相同的大小,但除此之外一无所知。 I cannot use any arrays or other Java Library classes but I may add helper methods (probably recursive).我不能使用任何数组或其他 Java 库类,但我可以添加辅助方法(可能是递归的)。 Suggestions?建议?

@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));
    }

You can use recursion to check if every key in the tree is contained in the other tree, since lookup returns null if no such key exists.您可以使用递归来检查树中的每个键是否都包含在另一棵树中,因为如果不存在这样的键, lookup将返回null

For this, I'm making some assumptions:为此,我做了一些假设:

  • EmptyTree.haveSameKeys returns otherTree.size() == 0 EmptyTree.haveSameKeys返回otherTree.size() == 0
  • EmptyTree.hasSameKeys always returns true EmptyTree.hasSameKeys总是返回true
  • EmptyTree.lookup always returns null . EmptyTree.lookup总是返回null

I CAN add public methods to the Tree interface that Empty/NonEmpty trees implement though if i please...我可以向 Empty/NonEmpty 树实现的 Tree 接口添加公共方法,但如果我愿意的话......

Thus, you'll need to add hasSameKeys to the Tree interface:因此,您需要将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); 
}

What this returns:这将返回什么:

  • EmptyTree and EmptyTree;空树和空树; true && true . true && true => Empty trees have the same keyset => 空树具有相同的键集
  • NonEmptyTree and EmptyTree;非空树和空树; false && true . false && true => Or the other way around. => 或者反过来。 Always.总是。
  • NonEmptyTree and NonEmptyTree;非空树和非空树; true => If both have the exact same key set. true => 如果两者具有完全相同的键集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM