Untitled

 avatar
unknown
plain_text
6 months ago
4.3 kB
3
Indexable
class TrieNode {
    TrieNode[] children = new TrieNode[26];
    boolean isEndOfWord;
    int index = -1;

    public TrieNode() {
        isEndOfWord = false;
    }
}

public class Trie {
    private TrieNode root;
    private int currentIndex = 0;
    private int size = 0; // Biến đếm số lượng từ trong trie

    public Trie() {
        root = new TrieNode();
    }

    // Thêm từ vào trie và gán chỉ số cho từ đó
    public void insert(String word) {
        TrieNode current = root;
        for (char c : word.toCharArray()) {
            int index = c - 'a';
            if (current.children[index] == null) {
                current.children[index] = new TrieNode();
            }
            current = current.children[index];
        }
        if (!current.isEndOfWord) {
            current.isEndOfWord = true;
            current.index = currentIndex++;
            size++; // Tăng biến đếm nếu từ này là từ mới
        }
    }

    // Tìm kiếm từ và trả về chỉ số của từ nếu tìm thấy
    public int get(String word) {
        TrieNode current = root;
        for (char c : word.toCharArray()) {
            int index = c - 'a';
            if (current.children[index] == null) {
                return -1; // Từ không tồn tại
            }
            current = current.children[index];
        }
        return current.isEndOfWord ? current.index : -1;
    }

    // Kiểm tra xem từ có tồn tại hay không
    public boolean contains(String word) {
        TrieNode current = root;
        for (char c : word.toCharArray()) {
            int index = c - 'a';
            if (current.children[index] == null) {
                return false; // Từ không tồn tại trong trie
            }
            current = current.children[index];
        }
        return current.isEndOfWord;
    }

    // Xoá từ khỏi trie
    public boolean delete(String word) {
        if (delete(root, word, 0)) {
            size--; // Giảm biến đếm nếu xoá thành công
            return true;
        }
        return false;
    }

    private boolean delete(TrieNode current, String word, int index) {
        if (index == word.length()) {
            if (!current.isEndOfWord) {
                return false;
            }
            current.isEndOfWord = false;
            return isEmpty(current);
        }

        char c = word.charAt(index);
        int i = c - 'a';
        TrieNode node = current.children[i];
        if (node == null) {
            return false;
        }

        boolean shouldDeleteCurrentNode = delete(node, word, index + 1);

        if (shouldDeleteCurrentNode) {
            current.children[i] = null;
            return isEmpty(current) && !current.isEndOfWord;
        }

        return false;
    }

    // Kiểm tra xem node có rỗng không (tức là không có node con)
    private boolean isEmpty(TrieNode node) {
        for (TrieNode child : node.children) {
            if (child != null) {
                return false;
            }
        }
        return true;
    }

    // Cập nhật từ trong trie (sửa từ)
    public void update(String oldWord, String newWord) {
        delete(oldWord);
        insert(newWord);
    }

    // Hàm clear để xóa hết dữ liệu trong trie
    public void clear() {
        root = new TrieNode(); // Tạo node gốc mới
        currentIndex = 0; // Đặt lại chỉ số từ về 0
        size = 0; // Đặt lại biến đếm về 0
    }

    // Hàm getSize để lấy số lượng từ hiện tại trong trie
    public int getSize() {
        return size;
    }

    public static void main(String[] args) {
        Trie trie = new Trie();

        // Thêm từ
        trie.insert("hello");
        trie.insert("world");
        trie.insert("hell");

        // Lấy kích thước của trie
        System.out.println("Size of trie: " + trie.getSize()); // 3

        // Xóa từ và kiểm tra lại kích thước
        trie.delete("world");
        System.out.println("Size of trie after deleting 'world': " + trie.getSize()); // 2

        // Clear trie và kiểm tra kích thước
        trie.clear();
        System.out.println("Size of trie after clear: " + trie.getSize()); // 0
    }
}
Editor is loading...
Leave a Comment