AVL tree 实现 dictionary

#include <iostream>
using namespace std;
 
 
struct AVLTreeNode
{
    string m_key;                  // 关键字
    int m_height;   // 高度
    int cnt;//个数
    AVLTreeNode *m_leftChild; // 左孩子
    AVLTreeNode *m_rightNode; // 右孩子
    AVLTreeNode(string value){
        m_key=value;
        m_leftChild=NULL;
        m_rightNode=NULL;
        cnt=1;
    }
};
 
 
class AVLTree
{
public:
    AVLTreeNode *m_root;
    string maximun;
    string minimun;
    AVLTree() : m_root(NULL) {}
    int height() { return height(m_root); }
    void insert(string key) { insert(m_root, key); }
    // 将键值为key的结点插入到AVL中
 
 
    int height(AVLTreeNode *node)
    {
        return node != NULL ? node->m_height : 0;
    }
 
    AVLTreeNode* insert(AVLTreeNode *&node,string key){
        if (key>maximun) maximun=key;
        if(key<minimun) minimun=key;
        if (node==NULL){
            node=new AVLTreeNode(key);
        }
        else if (key < node->m_key) // key插入node的左子树的情况
        {
            node->m_leftChild = insert(node->m_leftChild, key);
            // 插入节点后,如果AVL树失衡,需要进行相应调节
            if (height(node->m_leftChild) - height(node->m_rightNode) == 2)
            {
                if (key < node->m_leftChild->m_key)
                    node = leftLeftRotation(node);
                else
                    node = leftRightRotation(node);
            }
        }
        else if (key >node->m_key) // key插入node的右子树的情况
        {
            node->m_rightNode = insert(node->m_rightNode, key);
            // 插入节点后,如果AVL树失衡,需要进行相应调节
            if (height(node->m_rightNode) - height(node->m_leftChild) == 2)
            {
                if (key > node->m_rightNode->m_key)
                    node = rightRightRotation(node);
                else
                    node = rightLeftRotation(node);
            }
        }
        else node->cnt++;
        node->m_height = max(height(node->m_leftChild), height(node->m_rightNode)) + 1;
        return node;
    }
 
 
    /* LL:左子树的左边失去平衡(左单旋转)
     *       k2              k1
     *      /  \            /  \
     *     k1   z   ===>   x    k2
     *    /  \                 /  \
     *   x    y               y    z
     */
 
    AVLTreeNode* leftLeftRotation(AVLTreeNode *&k2){
        AVLTreeNode *k1 = k2->m_leftChild;
        k2->m_leftChild = k1->m_rightNode;
        k1->m_rightNode = k2;
        k2->m_height = std::max(height(k2->m_leftChild), height(k2->m_rightNode)) + 1;
        k1->m_height = std::max(height(k1->m_leftChild), k2->m_height) + 1;
        return k1;
    }
 
    /* RR:右子树的右边失去平衡(右单旋转)
     *       k1              k2
     *      /  \            /  \
     *     x   k2   ===>   k1   z
     *        /  \        /  \
     *       y    z      x    y
     */
    AVLTreeNode *rightRightRotation(AVLTreeNode *&k1)
    {
        AVLTreeNode *k2 = k1->m_rightNode;
        k1->m_rightNode = k2->m_leftChild;
        k2->m_leftChild = k1;
 
        k1->m_height = std::max(height(k1->m_leftChild), height(k1->m_rightNode)) + 1;
        k2->m_height = std::max(height(k2->m_rightNode), k1->m_height) + 1;
        return k2;
    }
 
    /* LR:左子树的右边失去平衡(左双旋转)
     *       k3               k3               k2
     *      /  \     RR      /  \     LL      /  \
     *     k1   D   ===>   k2    D   ===>   k1    k3
     *    /  \            /  \             /  \  /  \
     *   A    K2         k1   C           A    B C   D
     *       /  \       /  \
     *      B    C     A    B
     */
    AVLTreeNode *leftRightRotation(AVLTreeNode *&k3)
    {
        k3->m_leftChild = rightRightRotation(k3->m_leftChild);
        return leftLeftRotation(k3);
    }
 
    /* RL:右子树的左边失去平衡(右双旋转)
     *     k1               k1                K2
     *    /  \      LL     /  \      RR      /  \
     *   A    k3   ===>   A    k2   ===>   k1    K3
     *       /  \             /  \        /  \  /  \
     *      k2   D           B    k3     A    B C   D
     *     /  \                  /   \
     *    B    D                C     D
     */
    AVLTreeNode *rightLeftRotation(AVLTreeNode *&k1)
    {
        k1->m_rightNode = leftLeftRotation(k1->m_rightNode);
        return rightRightRotation(k1);
    }
     void inOrder() { inOrder(m_root); }
     void inOrder(AVLTreeNode *node)
         {
             if (node == NULL) {
                 return;
             }
             inOrder(node->m_leftChild);
             cout << node->m_key << " ";
             inOrder(node->m_rightNode);
         }
     int search(AVLTreeNode *N,string s){
         if(s<minimun||s>maximun) return 0;
         int count=0;
         if (N->m_key>=s&&N!=NULL){
             if(N->m_key.find(s)==0) count=count+N->cnt;
             if(N->m_leftChild!=NULL) count=count+search(N->m_leftChild,s);
             if(N->m_rightNode!=NULL&&N->m_key.find(s)==0)count=count+search(N->m_rightNode,s);
         }
         if (N->m_key<s&&N!=NULL){
             if(N->m_rightNode!=NULL)count=count+search(N->m_rightNode,s);
         }
         return count;
     }
};
int main()
{
    ios_base::sync_with_stdio(false);
    AVLTree tree;
    int num,inq;
    string input;
    string s;
    cin>>num;
    for(int i=0;i<num;i++){
 
 
        cin>>input;
        tree.insert(input);
    }
 
    cin>>inq;
    for (int i=0;i<inq;i++){
 
 
        cin>>s;
        cout<<tree.search(tree.m_root,s)<<endl;
    }
 
}

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值