AntInLoop

Trie (Prefix Tree) Code Implementations

Production-ready Trie (Prefix Tree) data structure implementations in C++, Python, Java, and JavaScript on AntInLoop.

Study Trie (Prefix Tree) Guide ยท Launch Visualizer

CPP Implementation

#include <vector> 
#include <list> 
#include <algorithm> 

// HashTable class 
class HashTable { 
private: 
    std::vector<std::list<int>> table; 
    int elements; 
    int getHash(int key) { 
        return key % table.size(); 
    } 
public: 
    // Constructor 
    HashTable(int size) : table(size), elements(0) {} 

    // Insert method 
    void insert(int key) { 
        table[getHash(key)].push_back(key); 
        ++elements; 
    } 

    // Remove method 
    void remove(int key) { 
        std::list<int>& keys = table[getHash(key)]; 
        keys.remove(key); 
        --elements; 
    } 

    // Search method 
    bool search(int key) { 
        std::list<int>& keys = table[getHash(key)]; 
        return std::find(keys.begin(), keys.end(), key) != keys.end(); 
    } 

    // Size method 
    int size() { 
        return elements; 
    } 

    // IsEmpty method 
    bool isEmpty() { 
        return elements == 0; 
    } 
};