三向单词查找树(c++版)

本文介绍了一种使用 C++ 实现的 Ternary Search Tree (TST) 数据结构,该结构支持字符串键的高效插入与查找操作。通过示例代码展示了节点内部的 put 和 find 方法的具体实现细节,并提供了一个简单的测试案例来验证 TST 的正确性和功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TST.h

#pragma once
#include <memory>
#include <string>

class TST
{
private:
    class Node
    {
    private:
        std::unique_ptr<int> c;
        bool end;
        std::unique_ptr<Node> left;
        std::unique_ptr<Node> mid;
        std::unique_ptr<Node> right;
    private:
        void put(const std::string& s, const int& pos)
        {
            if (pos == s.length())
                return;
            if (c == nullptr)
            {
                c = std::move(std::unique_ptr<int>(new int(s[pos])));
                if (pos == s.length() - 1)
                {
                    end = true;
                    return;
                }
                if (mid == nullptr)
                    mid = std::move(std::unique_ptr<Node>(new Node));
                mid->put(s, pos + 1);
                return;
            }
            int temp = *c;
            if (s[pos] < temp)
            {
                if (left == nullptr)
                    left = std::move(std::unique_ptr<Node>(new Node));
                left->put(s, pos);
            }
            else if (s[pos] > temp)
            {
                if (right == nullptr)
                    right = std::move(std::unique_ptr<Node>(new Node));
                right->put(s, pos);
            }
            else
            {
                if (mid == nullptr)
                    mid = std::move(std::unique_ptr<Node>(new Node));
                mid->put(s, pos);
            }
        }
        bool find(const std::string& s, const int& pos)
        {
            if (pos >= s.length())
                return false;
            if (c == nullptr)
                return false;
            if (end == true && pos == s.length() - 1 && *c == s[pos])
                return true;
            else if (end != true && pos == s.length() - 1 && *c == s[pos] && mid != nullptr)
                return mid->find(s, pos);
            else
            {
                int temp = *c;
                if (s[pos] < temp && left != nullptr)
                    return left->find(s, pos);
                else if (s[pos] > temp && right != nullptr)
                    return right->find(s, pos);
                else if (s[pos] == temp && mid != nullptr)
                    return mid->find(s, pos + 1);
            }
            return false;
        }
    public:
        Node()
            :c(nullptr),left(nullptr),mid(nullptr),right(nullptr),end(false)
        {

        }
        void put(const std::string& s)
        {
            put(s, 0);
        }
        bool find(const std::string& s)
        {
            return find(s, 0);
        }
    };
private:
    std::unique_ptr<Node> root;
public:
    TST():root(new Node)
    {

    }
public:
    void put(const std::string& s)
    {
        root->put(s);
    }
    bool find(const std::string& s)
    {
        return root->find(s);
    }
};

main.cpp

#include <iostream>
#include "TST.h"

using namespace std;

int main()
{
    TST t;
    t.put("marco");
    t.put("wh");
    t.put("marcom");
    t.put("misst");
    t.put("网红");
    t.put("帅哥");
    t.put("帅把");
    cout << boolalpha << t.find("帅哥") << endl;
    cout << t.find("辣妹") << endl;
    cout << t.find("网红") << endl;
    cout << t.find("marco") << endl;
    system("pause");
    return 0;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值