zoukankan      html  css  js  c++  java
  • BST完全实现

      1 #include <iostream>
      2 #include<string>
      3 #include<binaryNode.hpp>
      4 using namespace std;
      5 template<typename T>
      6 class BST {
      7 public:
      8     BST() { sz = hi = 0; top = NULL; }//初始化
      9     void insert(const T& data);
     10     void insert(const initializer_list<T>& data_list) { for (auto i : data_list)insert(i); }
     11     void remove(const T& data);
     12     void remove(binaryNode<T>*node);
     13     constexpr binaryNode<T>* root() const { return top; }
     14     void inorder_traverse  (const binaryNode<T>* node/*=root()*/)   //中序遍历了解结构,这里不能用默认参数root()
     15     {
     16         if (!node)return;
     17         inorder_traverse(node->lson);
     18         cout << node->data << " ";
     19         inorder_traverse(node->rson);
     20     }
     21     /*
     22 问:一个类中,为什么不能将数据成员做为成员函数的默认参数?
     23 答:标准规定这么一个限制条件是有其理由的,
     24 非静态成员如果在成员函数被调用前没有被初始化,
     25 此时编译器无法确认函数参数的默认值是多少。
     26 而标准这样做就把错误的发现提前到编译期。
     27  */
     28    binaryNode<T>* floor(const T& data);//小于等于data的元素
     29    binaryNode<T>* ceil (const T& data);//大于等于data的元素
     30    binaryNode<T>* max(binaryNode<T>*node) const{  while (node->rson)node = node->rson; return node; }//一个节点当做个子树看,最大的元素
     31    binaryNode<T>* min(binaryNode<T>*node) const{  while (node->lson)node = node->lson; return node; }//一个节点当做个子树看,最小的元素
     32     constexpr int size() const{ return sz; }
     33     constexpr int height() const { return hi; }
     34     binaryNode<T>* find(const T&data) 
     35     {
     36         if (auto node = find_it(data))return node; else { cout << "we can't find it!"; abort(); }
     37     }
     38     //通过find_it(data)获得准确的节点在哪,如果为空,程序截止
     39     ~BST() 
     40     {
     41         traverse(top);
     42     }//遍历删除元素
     43 private:
     44     void traverse(binaryNode<T>* node) { if (node) { traverse(node->lson); traverse(node->rson); delete node; } }
     45     binaryNode<T>* find_it(const T& data);
     46     binaryNode<T>* search(const T& data);
     47     void swap(binaryNode<T>* left, binaryNode<T>* right) { auto t = left->data; left->data = right->data; right->data = t; }//只交换元素不交换指针
     48     constexpr bool is_exist(const binaryNode<T>* node) const { return node->lson || node->rson; }//判断双节点是否存在不存在返回false;
     49     int checksz(const binaryNode<T>*node) const
     50     {
     51         if (!node)return 0;
     52         return checksz(node->lson) + checksz(node->rson) + 1;
     53     }//遍历检查各个子树的size
     54     int checkhi(const binaryNode<T>* node) const
     55     {
     56         if(!node)return 0;
     57         return  std::max(checkhi(node->lson),checkhi(node->rson)) + 1;
     58     }//遍历检查各个子树的height
     59 
     60     void remove_leaf(binaryNode<T>* node)
     61     {
     62         if (node != top) {
     63             if (node->father->lson == node)
     64                 node->father->lson = NULL;
     65             else
     66                 node->father->rson = NULL;
     67         }
     68         else top = NULL;
     69         delete node;
     70     }//删除叶子节点,如果是top,则置为空树。
     71     void remove_one(binaryNode<T>* node)
     72     {
     73         if (node != top) {
     74             if (!node->lson)
     75             {
     76                 if (node->father->lson == node)
     77                 {
     78                     node->father->lson = node->rson;
     79                     node->rson->father = node->father;
     80                 }
     81                 else
     82                 {
     83                     node->father->rson = node->rson;
     84                     node->rson->father = node->father;
     85                 }
     86             }
     87             else
     88             {
     89                 if (node->father->lson == node)
     90                 {
     91                     node->father->lson = node->lson;
     92                     node->lson->father = node->father;
     93                 }
     94                 else
     95                 {
     96                     node->father->rson = node->lson;
     97                     node->lson->father = node->father;
     98                 }
     99             }
    100         }
    101         else {
    102             if (!node->lson) top = node->rson;
    103             else top = node->lson;
    104             
    105         }
    106         delete node;
    107     }//删除一个节点,该节点只有一个子树,该节点的父亲指向节点之子
    108     void remove_both(binaryNode<T>* node)
    109     {
    110         binaryNode<T>* temp = min(node->rson);
    111         BST<T>::swap(temp, node);
    112         remove(temp);
    113     }//不写交换而要考虑很多情况,所以为了方便。只转移数据
    114 
    115     int sz;
    116     int hi;
    117     binaryNode<T>*  top;
    118 };
    119 template<typename T> binaryNode<T>* BST<T>::find_it(const T& data)
    120 {
    121     binaryNode<T>* temp = root();
    122     while (is_exist(temp)||temp->data==data)
    123     {
    124         if (temp->data == data)return temp;
    125         else if (data > temp->data) {
    126             if (temp->rson)
    127                 temp = temp->rson;
    128             else break;
    129         }
    130         else {
    131             if (temp->lson)
    132                 temp = temp->lson;
    133             else break;
    134         }
    135     }
    136     return nullptr;
    137 }
    138 
    139 
    140 template<typename T> binaryNode<T>* BST<T>::floor(const T& data)
    141 {
    142     binaryNode<T>* close = top;
    143     while (is_exist(close))
    144     {
    145         if (data == close->data)return close;
    146         if (data > close->data)
    147         {
    148             if (close->rson)
    149                 close = close->rson;
    150             else break;
    151         }
    152         else
    153         {
    154             if (close->lson)
    155                 close = close->lson;
    156             else break;
    157         }
    158     }
    159     while (data<close->data)
    160     { binaryNode<T>* temp = close;
    161     close = close->father;
    162     if (temp != close->lson)
    163         return close;
    164     }
    165     return close;
    166 }
    167 
    168 
    169 template<typename T> binaryNode<T>* BST<T>::ceil(const T& data)
    170 {
    171     binaryNode<T>* close = top;
    172     while (is_exist(close))
    173     {
    174         if (data == close->data)return close;
    175         if (data > close->data)
    176         {
    177             if (close->rson)
    178                 close = close->rson;
    179             else break;
    180         }
    181         else
    182         {
    183             if (close->lson)
    184                 close = close->lson;
    185             else break;
    186         }
    187     }
    188     while (data > close->data)
    189     {
    190         binaryNode<T>* temp = close;
    191         close = close->father;
    192         if (temp != close->rson)
    193             return close;
    194     }
    195     return close;
    196 }
    197 
    198 template<typename T> void BST<T>::insert(const T& data) {//利用BST的性质找到该插入的地方插入
    199     if (!top)top = new binaryNode<T>(data);
    200     else
    201     {   binaryNode<T>* temp = search(data);
    202     if(!temp||temp->data==data) 
    203     {
    204         cout << "you have inserted same data in BST!" << endl; abort();
    205     }
    206     else{
    207             if (data > temp->data) 
    208             {
    209                 temp->rson = new binaryNode<T>(data);
    210                 temp->rson->father = temp;
    211             }
    212             else
    213             {
    214                 temp->lson = new binaryNode<T>(data);
    215                 temp->lson->father = temp;
    216             }
    217 
    218     }
    219     
    220     }
    221     sz = checksz(top);
    222     hi = checkhi(top);
    223 }
    224 
    225 template<typename T> void BST<T>::remove(const T& data) {
    226     if (!top) { cout << "top is not exist!" << endl; abort(); }
    227     binaryNode<T>* temp = find_it(data);
    228     if (!temp) { cout << "we can't find it that you want to remove!" << endl; abort(); }
    229     remove(temp);
    230     sz = checksz(top);
    231     hi = checkhi(top);
    232 }
    233 template<typename T> void BST<T>::remove(binaryNode<T>*node) {
    234     if (is_exist(node))
    235     {
    236         if (node->lson && node->rson)
    237             remove_both(node);//最棘手的两个
    238         else
    239             remove_one(node);//处理单个子树节点
    240     }
    241     else
    242         remove_leaf(node);//表示是个孤单的叶子结点
    243 }
    244 
    245 template<typename T> binaryNode<T>* BST<T>::search(const T& data) {
    246     if (top->data == data)return top;
    247     binaryNode<T>* temp = root();
    248     while (is_exist(temp)) {
    249         if (temp->data == data)
    250             return nullptr;
    251         if (data > temp->data)
    252             if (temp->rson)
    253                 temp = temp->rson;
    254             else break;
    255         else if (data < temp->data)
    256             if (temp->lson)
    257                 temp = temp->lson;
    258             else break;
    259 }
    260     return temp;
    261 }
    262 
    263 int main()
    264 {
    265     BST<char>test;
    266     test.insert({ 's','a','f','k','d','p','c','g'});
    267     test.insert('z');
    268     cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;
    269     test.inorder_traverse(test.root());
    270     cout << endl;
    271     test.remove('k');
    272     cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;
    273     test.inorder_traverse(test.root());
    274     cout << endl;
    275     test.remove('a');
    276     cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;
    277     test.inorder_traverse(test.root());
    278     cout << endl;
    279     test.remove('d');
    280     cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;
    281     test.inorder_traverse(test.root());
    282     cout << endl;
    283     test.remove('p');
    284     cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;
    285     test.inorder_traverse(test.root());
    286     cout << endl;
    287     test.remove('s');
    288     cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;
    289     test.inorder_traverse(test.root());
    290     cout << endl;
    291     test.remove('f');
    292     cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;
    293     test.inorder_traverse(test.root());
    294     cout << endl;
    295     test.remove('g');
    296     cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;
    297     test.inorder_traverse(test.root());
    298     cout << endl;
    299     test.remove('c');
    300     cout << " BST高度: " << test.height() << " BST元素个数: " << test.size() << endl;
    301     test.inorder_traverse(test.root());
    302     cout << endl;
    303 
    304 }

    binaryNode.hpp

     1 #pragma once
     2 #pragma once
     3 template<class T>struct binaryNode {
     4     binaryNode(T data, binaryNode<T>* father = NULL, binaryNode<T>* lson = NULL, binaryNode<T>* rson = NULL) :
     5         father(father), lson(lson), rson(rson), data(data) {}
     6     binaryNode<T>* father;
     7     binaryNode<T>* lson;
     8     binaryNode<T>* rson;
     9     T data;
    10     //auto insertls(T data) {
    11     //    return lson = new binaryNode<T>(data, this);
    12     //}
    13     //auto insertrs(T data) {
    14     //    return rson = new binaryNode<T>(data, this);
    15     //}
    16     //auto remove() {
    17     //    delete rson;
    18     //    delete lson;
    19     //    delete this;
    20     //}
    21     /*bool operator<(const binaryNode<T>* node) { return node->data < data; }
    22     bool operator>(const binaryNode<T>* node) { return !(node < this); }
    23     bool operator==(const binaryNode<T>* node) { return data == node->data; }*/
    24 
    25 };

    https://github.com/siuwhat/algorithm/tree/master/BST

    所望隔山海
  • 相关阅读:
    [HNOI2002]彩票
    贼有意思[最长上升公共子序列](SAC大佬测试题)
    空间漫游(SAC大佬的测试)
    计蒜客NOIP模拟赛D2T3 数三角形
    计蒜客NOIP模拟赛D2T2 直线的交点
    计蒜客NOIP模拟赛(3)D2T1 小区划分
    计蒜客NOIP模拟赛(3)D1T3 任性的国王
    UpdateAfterEvent
    长链剖分学习笔记
    P4292 [WC2010]重建计划
  • 原文地址:https://www.cnblogs.com/otakus/p/bst.html
Copyright © 2011-2022 走看看