• 算法-搜索(5)m路搜索树



    动态m路搜索树即系统运行时可以动态调整保持较高搜索效率的最多m路的搜索树。
    以3路搜索树为例说明其关键码排序关系:
     
    const int MaxValue=9999;   
    template <class T>
    struct MtreeNode:public Mtree{
        int n;   //关键码个数  
        MtreeNode<T> *parent;
        T key[m+1];      //key[m]为监视哨兼单元,key[0]未使用
        MtreeNode<T> *ptr[m+1];  //子树结点指针数组,ptr[m]在插入溢出时使用
        int *recptr[m+1];  //每个索引项中指向数据区相应记录起始地址的指针
    };
    
    template <class T>
    struct Triple{     //搜索结果三元组定义
        MtreeNode<T> *r;
        int i;   //结点中关键码序号
        int tag;  //tag为0表示搜索成功,为1表示搜索失败
    };
    
    template <class T>
    class Mtree{
    protected:
        MtreeNode<T> *root;
        const int m;  //最大子树个数,等于树的度
    public:
        Triple<T> Search(const T&x);
    };
    
    template <class T>
    Triple<T> Mtree<T>::Search(const T& x){
        Triple result;   //记录搜索结果三元组
        GetNode(root);   //从磁盘上读取位于根root的结点
        MtreeNode<T> *p=root,*q=NULL;   //p是扫描指针,q用来记录p的父结点指针
        int i=0;
        while(p!=NULL){
            p->key[(p->n)+1]=MaxValue;
            while(p->key[i+1]<x) i++; //在结点内顺序搜索
            if(p->key[i+1]==x){
                result.r=p;result.i=i+1;result.tag=0;  //返回结果
                return result;
            }
            q=p;p=p->ptr[i];  //本结点无x,q记录当前结点,p下降到相应子树
            GetNode(p);       //从磁盘读取p结点
        }
        result.r=q;
        result.i=i+1;
        result.tag=1;
        return result;  //搜索失败,返回插入位置
    }
  • 相关阅读:
    .NET生成CSV文件
    Codeforces Round #552 (Div. 3) A题
    位运算介绍
    Codeforces Round #552 (Div. 3) F题
    POJ—1321(棋盘问题)
    Codeforces Round #552 (Div. 3) C题
    Codeforces Round #553 (Div. 2) A题
    Codeforces Round #553 (Div. 2) C题
    Codeforces Round #553 (Div. 2) B题
    Codeforces Round #552 (Div. 3) D题
  • 原文地址:https://www.cnblogs.com/yangyuliufeng/p/9237632.html
Copyright © 2020-2023  润新知