You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于二叉搜索树的C++ HashMap实现问题排查求助

排查基于二叉搜索树的HashMap实现问题

我正在LeetCode上练习「设计哈希映射」题目,尝试以二叉搜索树(Binary Search Tree)为底层数据结构,用C++实现HashMap。目前该实现已通过大部分测试用例,但存在一个超长测试用例显示结果错误,且无法查看具体错误细节,导致难以定位问题。在此恳请有二叉搜索树开发经验的人士帮忙检查我的实现是否存在设计缺陷,感谢指导!

以下是我的实现代码:

#define MAXBUCKETS 769 // large prime number that to minimize collisions

class Bucket{ //binary search tree bucket for hashmap
    // define what a node looks like
    struct node{
        int key;
        int value;
        struct node *left, *right;
    };
    
    struct node* root;

    
    // create a new node
    struct node* newNode(int newKey, int newValue)
    {
        struct node* temp = new struct node;
        temp->key = newKey;
        temp->value = newValue;
        temp->left = NULL;
        temp->right = NULL;
        return temp;
    }
    
    // insert a new node given its key
    struct node* insertNode(struct node* insertUnder, int newKey, int newVal)
    {
        // tree is empty, create a new node at this point
        // this will make it so that we create roots for empty
        // trees, or add as a leaf in the right location
        if( insertUnder == NULL)
        {
            return newNode(newKey, newVal);;
        }
        
        // find the right position for this new key and value
        if( newKey < insertUnder->key)
        {
            // smaller keys to the left
            insertUnder->left = insertNode(insertUnder->left, newKey, newVal);
        }
        else if (newKey > insertUnder->key)
        {
            // bigger keys to the right
            insertUnder->right = insertNode(insertUnder->right, newKey, newVal);
        }
        else
        {
            // matching key, update value
            insertUnder->value = newVal;
        }
        return insertUnder;
    }
    
    // find a node in the tree
    int search(struct node* currentNode, int key)
    {
        if(currentNode == NULL)
        {
            // empty node, so key is not present
            return -1;
        }
        if(currentNode->key == key)
        {
            // found key, return its value
            return currentNode->value;
        }
        if(key < currentNode->key)
        {
            // key could be to the left, search there
            return search(currentNode->left, key);
        }
        else
        {
            // key could be to the right, search there
            return search(currentNode->right, key);
        }
    }
    
        
    // find the predecessor of a node
    struct node* findSuccessor(struct node* node)
    {
        // to find the successor of input node, move to the right
        // to search the nodes bigger than the input node, and 
        // then move to the left as many times as you can to find 
        // the smallest node in that right subree.
        node = node->right;
        while(node->left)
        {
            node = node->left;
        }
        return node;
    }
    
    // find the successor of a node
    struct node* findPredecessor(struct node* node)
    {
        // to find the successor of input node, move to the left
        // to search the nodes smaller than the input node, and 
        // then move to the right as many times as you can to find 
        // the biggest node in that left subree.
        node = node->left;
        while(node->right)
        {
            node = node->right;
        }
        return node;
    }
    
    
    // remove a node from the tree
    struct node* removeNode(struct node* node, int key)
    {
        if(node == NULL)
        {
            // node is empty, done searching
            return NULL;
        }
        
        // search through the tree to find the location of the key
        if(key < node->key)
        {
            node->left = removeNode(node->left, key);
        }
        else if(key > node->key)
        {
            node->right = removeNode(node->right, key);
        }
        
        if(node->key == key)
        {
            // found the right node
            if(node->right == NULL && node->left == NULL) // node is a leaf
            {
                //simply remove the node
                // node = NULL;
                delete node;
                return NULL;
            }
            else
            {
                if(node->right)
                {
                    // node has right child, replace by successor (smallest of right children), 
                    // and delete the successor from the subtree
                    struct node* successor = findSuccessor(node);
                    node->key = successor->key;
                    node->value = successor->value;
                    node->right = removeNode(successor, successor->key);
                    
                }
                else //there is a left child
                {
                    // node has no right child, but has a left child,
                    // so replace by its predecessor (biggest of the left children), and
                    // delete the predecessor from the subtree
                    struct node* predecessor = findPredecessor(node);
                    node->key = predecessor->key;
                    node->value = predecessor->value;
                    node->left = removeNode(predecessor, predecessor->key);
                }
            }
        }
        return node;
    }

    public:
    Bucket(){
        root = NULL;
    }
    void add(int key, int value)
    {
        root = insertNode(root, key, value);
    }
    void remove(int key)
    {
        root = removeNode(root, key);
    }
    int findInBucket(int key)
    {
        return search(root, key);
    }
};

class MyHashMap {
    Bucket buckets[MAXBUCKETS];
public:
    MyHashMap() {
        for(int i = 0; i < MAXBUCKETS; i++)
        {
            buckets[i] = Bucket();
        }
    }
    
    void put(int key, int value) {
        int bucketi = key % MAXBUCKETS;
        buckets[bucketi].add(key, value);
    }
    
    int get(int key) {
        int bucketi = key % MAXBUCKETS;
        return buckets[bucketi].findInBucket(key);
    }
    
    void remove(int key) {
        int bucketi = key % MAXBUCKETS;
        buckets[bucketi].remove(key);
    }
};



/**
 * Your MyHashMap object will be instantiated and called as such:
 * MyHashMap* obj = new MyHashMap();
 * obj->put(key,value);
 * int param_2 = obj->get(key);
 * obj->remove(key);
 */

内容的提问来源于stack exchange,提问作者WKGuy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 13:04:50