约定:

        多次插入同一个key的节点,其value值取最后一次插入的值。

递归实现: 

 std;
 3 
 4 class BST {
 5 private:
 6     struct BSTNode {
 7         int key, value;
 8         BSTNode * left, *right;
 9 
10         BSTNode (int _key, int _value, BSTNode * _left = nullptr, BSTNode * _right = nullptr) :
11             left(_left), right(_right) {
12             key = _key;
13             value = _value;
14         }
15     };
16 
17     BSTNode * search(int key, BSTNode * root) {
18         if (root==NULL) return NULL;
19         if (root->key==key) return root;
20         else if (root->key > key) return search(root->left, key);
21         else return search(root->right, key);
22     }
23       
24     BSTNode * insert(BSTNode * root, int key, int value) {
25         if (root== NULL) 
26             return new BSTNode (key, value);
27 
28         if (root->key == key) 
29             root->value = value;
30         else if (root->key < key) 
31             root->right = insert(root->right, key, value);
32         else 
33             root->left = insert(root->left, key, value);
34 
35             return root;
36     }
37 
38     BSTNode * removeAt(int key, BSTNode * root) {
39          if (root == nullptr)
40             return nullptr;
41 
42          if (key < root->key)
43             root->left = removeAt(key, root->left);
44          else if (root->key < key)
45             root->right = removeAt(key, root->right);
46          else {
47             BSTNode * tmp;
48             if (root->left && root->right) {
49                 tmp = findMax(root->left);
50                 root->key = tmp->key;
51                 root->value = tmp->value;
52                 root->left = removeAt(tmp->key, root->left);
53             }
54             else {
55                 tmp = root;
56                 if (root->left == nullptr)
57                      root = root->right;
58                 else if (root->right == nullptr)
59                      root = root->left;
60 
61                 delete tmp;
62              }
63         }
64         return root;
65     }
66 
67     BSTNode * findMax(BSTNode * root) {
68         if (root != nullptr) {
69             while (root->right != nullptr)
70                 root = root->right;
71         }
72 
73         return root;
74     }
75 
76 public:
77     BSTNode * search(int key) {
78         return search(key, Root);
79     }
80 
81     void remove(int key) {
82         Root = removeAt(key, Root);
84 } 85 86 void insert( int key, int value){ 87 Root = insert(Root, key, value); 88 } 89 };

非递归实现:

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 class BST{
  5 private:
  6     struct BSTNode {
  7         int key, value;
  8         BSTNode * left, *right;
  9 
 10         BSTNode (int _key, int _value, BSTNode * _left = nullptr, BSTNode * _right = nullptr) :
 11             left(_left), right(_right) {
 12             key = _key;
 13             value = _value;
 14         }
 15     };
 16 
 17     BSTNode *Root = nullptr, *ht = nullptr;
 18 
 19     BSTNode * search(int key, BSTNode * root) {
 20         ht = nullptr;
 21         while (root != nullptr) {
 22             if (root->key < key) {
 23                 ht = root;
 24                 root = root->right;
 25             }
 26             else if (key < root->key) {
 27                 ht = root;
 28                 root = root->left;
 29             }
 30             else
 31                 return root;
 32         }
 33 
 34         return nullptr;
 35     }
 36 
 37     void removeAt(int key, BSTNode * root) {
 38         BSTNode * node = search(key);
 39         if (node != nullptr) {
 40             if (node->left == nullptr) {
 41                 if (node == Root)
 42                     Root = node->right;
 43                 else
 44                     (ht->key < node->key ? ht->right : ht->left) = node->right;
 45                 delete node;
 46             }
 47             else if (node->right == nullptr) {
 48                 if (node == Root)
 49                     Root = node->left;
 50                 else
 51                     (ht->key < node->key ? ht->right : ht->left) = node->left;
 52                 delete node;
 53             }
 54             else {
 55                 BSTNode * p = node, *succ;
 56                 succ = findMax(node->left, p);
 57                 node->key = succ->key; node->value = succ->value;
 58                 (p == node ? p->left : p->right) = succ->left;
 59                 delete succ;
 60             }
 61         }
 62     }
 63 
 64     BSTNode * findMax(BSTNode * root, BSTNode * & ht) {
 65         if (root != nullptr) {
 66             while (root->right != nullptr) {
 67                 ht = root;
 68                 root = root->right;
 69             }
 70         }
 71 
 72         return root;
 73     }
 74 
 75 public:
 76     BSTNode * search(int key) {
 77         return search(key, Root);
 78     }
 79 
 80     void insert(int key, int value) {
 81         BSTNode * tmp = search(key);
 82         if (tmp == nullptr) {//关键码不存在
 83             BSTNode * node = new BSTNode (key, value);
 84             if (ht == nullptr) {//根节点
 85                 Root = node;
 86             }
 87             else {
 88                 if (ht->key < key)
 89                     ht->right = node;
 90                 else
 91                     ht->left = node;
 92             }
 93         }
 94         else
 95             tmp->value = value;
 96     }
 97 
 98     void remove(int key){
 99         removeAt(key,Root);
100     }
101 };

 

相关文章: