⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btree_.h

📁 this is a Implementation of a B* tree,not yet completed, because I need help for this task.
💻 H
字号:

// btree.h

#ifndef BTREE_H
#define BTREE_H

#define keyType void * 
#define ObjIDType long


#include "CBTreePage.h"


#define DEFAULT_BTREE_ORDER 3


class BTree
{

public:
       //typedef ObjectInfo iterator;
       typedef CBTreePage ::lpfnForEach2    lpfnForEach2;
       typedef CBTreePage ::lpfnForEach3    lpfnForEach3;
       typedef CBTreePage ::lpfnFirstThat2  lpfnFirstThat2;
       typedef CBTreePage ::lpfnFirstThat3  lpfnFirstThat3;
//       typedef CBTreePage ::ObjectInfo      ObjectInfo;
public:
       BTree(int order = DEFAULT_BTREE_ORDER, bool unique = true);
       ~BTree();
       bool            Insert ( keyType key, const int ObjID);
       bool            Remove (const keyType key, const int ObjID);
       ObjIDType       Search (const keyType key);
       long            size()  { return m_NumKeys; }
       long            height(){ return m_Height;      }
       long            GetOrder(){ return m_Order;     }

       void            Print (ostream &os);
       void            ForEach( lpfnForEach2 lpfn, void *pExtra1 );
       void            ForEach( lpfnForEach3 lpfn, void *pExtra1, void *pExtra2);
       ObjectInfo*     FirstThat( lpfnFirstThat2 lpfn, void *pExtra1 );
       ObjectInfo*     FirstThat( lpfnFirstThat3 lpfn, void *pExtra1, void *pExtra2);
       //typedef               ObjectInfo iterator;

protected:
       CBTreePage          m_Root;
       int                     m_Height;  // height of tree
       int                     m_Order;   // order of tree
       long            m_NumKeys; // number of keys
       bool            m_Unique;  // Accept the elements only once ?
};


#include <iostream>


const int MaxHeight = 5;
//template <typename keyType, typename ObjIDType>
BTree/*<keyType, ObjIDType>*/::BTree(int order, bool unique)
                               : m_Unique(unique),
                                 m_Order(order),
                                 m_Root(2 * order  + 1, m_Unique),
                                 m_NumKeys(0)
{
       m_Root.SetMaxKeysForChilds(order);
       m_Height = 1;
}

//template <typename keyType, typename ObjIDType>
BTree/*<keyType, ObjIDType>*/::~BTree()
{
}

bool BTree::Insert(keyType key, const int ObjID)
{
       bt_ErrorCode error = m_Root.Insert(key, ObjID);
       if( error == bt_duplicate )
               return false;
       m_NumKeys++;
       if( error == bt_overflow )
       {
               m_Root.SplitRoot();
               m_Height++;
       }
       return true;
}

//template <typename keyType, typename ObjIDType>
bool BTree/*<keyType, ObjIDType>*/::Remove (const keyType key, const int ObjID)
{
       bt_ErrorCode error = m_Root.Remove(key, ObjID);
       if( error == bt_duplicate || error == bt_nofound )
               return false;
       m_NumKeys--;

       if( error == bt_rootmerged )
               m_Height--;
       return true;
}

//template <typename keyType, typename ObjIDType>
ObjIDType BTree/*<keyType, ObjIDType>*/::Search (const keyType key)
{
       ObjIDType ObjID = -1;
       m_Root.Search(key, ObjID);
       return ObjID;
}


//template <typename keyType, typename ObjIDType>
void BTree/*<keyType, ObjIDType>*/::ForEach(lpfnForEach2 lpfn, void *pExtra1)
{
       m_Root.ForEach(lpfn, 0, pExtra1);
}

//template <typename keyType, typename ObjIDType>
void BTree/*<keyType, ObjIDType>*/::ForEach(lpfnForEach3 lpfn, void *pExtra1, void *pExtra2)
{
       m_Root.ForEach(lpfn, 0, pExtra1, pExtra2);
}

ObjectInfo *BTree::FirstThat(lpfnFirstThat2 lpfn, void *pExtra1)
{
       return m_Root.FirstThat(lpfn, 0, pExtra1);
}

//template <typename keyType, typename ObjIDType>
ObjectInfo *BTree::FirstThat(lpfnFirstThat3 lpfn, void *pExtra1, void *pExtra2)
{
       return m_Root.FirstThat(lpfn, 0, pExtra1, pExtra2);
}


void BTree/*<keyType, ObjIDType>*/::Print (ostream &os)
{
       m_Root.Print(os);
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -