数据结构实现时所需的成员变量、标准对外接口

1. vector

typedef int Rank;

template <typename T>
class Vector{
protected:
    Rank _size; Rank _capacity; T* _elem;   
};

vector 的本质在于其底部维护的是一个一维数组(某种意义上说,vector 是对一维数组的 adapter 配接器)。在经典的数据结构中,对一维数组进行进一步拓展封装的还包括,binary heap(完全二叉树);一般而言,只要对一维数组进行封装的场合,都会提供另外的两个成员属性,

typedef struct HeapStruct {
    int Capacity;                   // 当前所放体积
    int Size;                       // 容量
    ElementType* Elements;
}* PriorityQueue;

有了 Capacity 以及 Size 这两个成员变量,便可轻易地实现另外两个公共接口,判断容器是否为空或是否为满。

  • insert:⇒ 是否为满;
  • delete:⇒ 是否为空;

2. stack

  • stack@vector

    
    #include "../Vector/Vector.h" 
    
    template <typename T> class Stack: public Vector<T> { 
    public: //size()、empty()以及其它开放接口,均可直接沿用
       void push(T const& e) { insert(size(), e); }  
       T pop() { return remove(size() - 1); } 
       T& top() { return (*this)[size() - 1]; } 
    };

3. 二叉搜索树

  • 查找(search)、插入()、删除;

    template <typename T>
    class BST :public BinTree<T> {
    public:
        virtual BinNodePosi(T)& search(const T& e); 
        virtual BinNodePosi(T) insert(const T& e);
        virtual bool remove(const T& e);
                        // 一般常用作其它特定二叉搜索树的基类,
                        // 需要修改相关(search、insert、remove)接口
    };

4. AVL 树

AVL 树是平衡因子受限的二叉搜索树,对于二叉搜索树的三个核心成员函数,search、insert、remove 函数,仅需要修改其中的 insert 和 remove 函数。

#include "../BST/BST.h"
template <typename T>
class AVL :public BST<T> {
public:
    BinNode<T>* insert(const T& e);
    bool remove(const T& e);
}
原文地址:https://www.cnblogs.com/mtcnn/p/9423711.html