自定义哈希表删除函数

zbsbpyhn  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(182)

所以我有这个哈希表自定义实现。我的自定义哈希表类中的remove方法有问题。另外,我是sof的新手,所以如果有任何改进,我可以为我的问题让我知道。
它的表有一个数组列表

private ArrayList<BST<Song>> Table;

另一个哈希表,我使用这个哈希表中的整数作为

private Hashtable<String, Integer> map;

hashtable.java类中的remove方法。由于某种原因,它并没有删除所有适用的song obj,这是我有问题的部分。

public boolean remove(Song song) throws NullPointerException {

    if (song == null)
        throw new NullPointerException("Value is null, unable to remove!");

    else if (!this.contains(song)) {
        System.out.println("\nSong not found in the hashtable. Unable to remove!\n");
        return false;
    }

    Set<String> keys = map.keySet();
    for (String key : keys) {

        try {
            Song song1 = Table.get(map.get(key)).search(song, false, new NameComparator());
        if (song1.compareTo(song) == 0) {
            Table.get(map.get(key)).remove(song, new NameComparator());
            songsDeleted++;
            numElements--;
            System.out.println("DELETED");
        }
        }
        catch(Exception e) {

        }
    }

    return true;
}

bst.java的一部分

/**
     * Removes a value from the BST
     * 
     * @param data the value to remove
     * @param c    the Comparator indicating how data in the tree is organized Note:
     *             updates nothing when the element is not in the tree
     */
    public void remove(T data, Comparator<T> c) throws NoSuchElementException {

        if ( isEmpty())
            throw new NoSuchElementException("Unable to remove data, data does not exist");

        else if ( getSize() == 1)
            root = null;

        else
        remove(data, root, c);
    }

    private Node remove(T data, Node root, Comparator<T> c) {

         /* Base Case: If the tree is empty */
        if (root == null)
            return root;

        /* Otherwise, recur down the tree */
        if (c.compare(data, root.data) < 0)
            root.left = remove(data, root.left,  c);
        else if (c.compare(data, root.data) > 0)
            root.right = remove(data, root.right, c);

        // if key is same as root's 
        // key, then This is the
        // node to be deleted
        else {
            // node with only one child or no child
            if (root.left == null)
                return root.right;
            else if (root.right == null)
                return root.left;

            // node with two children: Get the inorder
            // successor (smallest in the right subtree)
            root.data = findMin(root.right);

            // Delete the inorder successor
            root.right = remove(root.data, root.right, c);
        }

        return root;

bst的比较器

public class NameComparator implements Comparator<Song> {

    @Override
    public int compare(Song song1, Song song2) {
        if (song1.hashCode() == song2.hashCode())
            return 0;
        else if (song1.hashCode() > song2.hashCode())
            return 1;
        else
            return -1;
    }

}

song.java的一部分

public class Song implements Comparable<Song> {

    private String name;
    private String composer;
    private int songLength;
    private int releaseYear;
    private String lyrics;

//getterr setter

    @Override
    public int hashCode() {
        String key = name.toLowerCase() + composer.toLowerCase();
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }
        return sum;
    }

    // used to compare song obj to song obj

    @Override
    public int compareTo(Song o) {
        if (o == this) {
            return 0;
        } else {
            if (this.hashCode() == o.hashCode())
                return 0;
            else if (this.hashCode() > o.hashCode())
                return 1;
            else
                return -1;
        }

    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题