zoukankan      html  css  js  c++  java
  • BinarySearchTree(二叉搜索树)原理及C++代码实现

    BST是一类用途极广的数据结构。它有如下性质:设x是二叉搜索树内的一个结点。如果y是x左子树中的一个结点,那么y.key<=x.key。如果y是x右子树中的一个结点,那么y.key>=x.key。

    BST容易出现不平衡的情况,所以实际运用的时候还是以平衡的二叉搜索树为主,例如RB树,AVL树,treap树甚至skiplist等。

    BST实现较为简单,我们直接来看看代码吧。

    代码如下:(仅供参考)

      1 #include <iostream>
      2 using namespace std;
      3 
      4 struct Node {
      5     int key;
      6     Node * left;
      7     Node * right;
      8     Node * parent;
      9     Node() : key(0), left(nullptr), right(nullptr), parent(nullptr) {}
     10 };
     11 
     12 class BST {
     13     Node * root;
     14 private :
     15     Node * minimum(Node * p);
     16     Node * maximum(Node * p);
     17     //用新结点代替旧结点,只修改结点与其父节点的指向,允许新结点为空
     18     void transplant(Node * old_t, Node * new_t);
     19 public :
     20     BST() : root(nullptr) {}
     21     Node * search(const int k) {return search(root, k);}
     22     Node * search(Node * p, const int k);
     23     const Node * minimum() {return minimum(root);}
     24     const Node * maximum() {return maximum(root);}
     25     const Node * successor(Node * p);
     26     const Node * predecessor(Node * p);
     27     void insert(const int k);
     28     void remove(const int k) {remove(search(k));}
     29     void remove(Node * p);
     30     void inorderWalk() {inorderWalk(root);}
     31     void inorderWalk(Node * p);
     32 };
     33 
     34 Node * BST::search(Node * p, const int k) {
     35     if (p == nullptr || k == p->key)
     36         return p;
     37     if (k < p->key)
     38         return search(p->left, k);
     39     else
     40         return search(p->right, k);
     41 }
     42 
     43 Node * BST::minimum(Node * p) {
     44     if (p == nullptr)
     45         return p;
     46     while (p->left)
     47         p = p->left;
     48     return p;
     49 }
     50 
     51 Node * BST::maximum(Node * p) {
     52     if (p == nullptr)
     53         return p;
     54     while (p->right)
     55         p = p->right;
     56     return p;
     57 }
     58 
     59 const Node * BST::successor(Node * p) {
     60     if (p->right)
     61         return minimum(p->right);
     62     Node * y = p->parent;
     63     while (y != nullptr && y->right == p) {
     64         p = y;
     65         y = y->parent;
     66     }
     67     return y;
     68 }
     69 
     70 const Node * BST::predecessor(Node * p) {
     71     if (p->left)
     72         return maximum(p->left);
     73     Node * y = p->parent;
     74     while (y != nullptr && y->left == p) {
     75         p = y;
     76         y = y->parent;
     77     }
     78     return y;
     79 }
     80 
     81 void BST::insert(const int k) {
     82     Node * p = new Node;
     83     p->key = k;
     84 
     85     Node *x = root, *y = nullptr;
     86     while (x != nullptr) {
     87         y = x;
     88         if (x->key < k)
     89             x = x->right;
     90         else
     91             x = x->left;
     92     }
     93     p->parent = y;
     94     if (y == nullptr)
     95         root = p;
     96     else if (y->key < k)
     97         y->right = p;
     98     else
     99         y->left = p;
    100 }
    101 
    102 void BST::transplant(Node * old_t, Node * new_t) {
    103     if (old_t->parent == nullptr)
    104         root = new_t;
    105     else if (old_t == old_t->parent->left)
    106         old_t->parent->left = new_t;
    107     else
    108         old_t->parent->right = new_t;
    109     if (new_t != nullptr)
    110         new_t->parent = old_t->parent;
    111 }
    112 
    113 void BST::remove(Node * p) {
    114     if (p->left == nullptr)
    115         transplant(p, p->right);
    116     else if (p->right == nullptr)
    117         transplant(p, p->left);
    118     else {
    119         Node * t = minimum(p->right);
    120         if (t->parent != p) {
    121             transplant(t, t->right);
    122             t->right = p->right;
    123             t->right->parent = t;
    124         }
    125         transplant(p, t);
    126         t->left = p->left;
    127         t->left->parent = t;
    128     }
    129     delete p;
    130 }
    131 
    132 void BST::inorderWalk(Node * p) {
    133     if (p) {
    134         inorderWalk(p->left);
    135         cout << p->key << ends;
    136         inorderWalk(p->right);
    137     }
    138 }
  • 相关阅读:
    [20170612]FOR ALL COLUMNS SIZE repeat(11g).txt
    [20170612]FOR ALL COLUMNS SIZE repeat(12c).txt
    [20170611]关于数据块地址的计算.txt
    [20170607]再论Private Strand Flush Not Complete.txt
    [20170606]11G _optimizer_null_aware_antijoin.txt
    42_自定义泛型类的应用
    43_通过反射获得泛型的实际类型参数
    为什么好男孩找不到女朋友
    38_泛型的通配符扩展应用
    36_入门泛型的基本应用
  • 原文地址:https://www.cnblogs.com/yxsrt/p/12201870.html
Copyright © 2011-2022 走看看