1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| #include<iostream> #include<array> #define DataType int using namespace std; struct BST_node { DataType data; BST_node *left; BST_node *right; }; class BST { private: BST_node* root; public: BST(DataType arr[], size_t arr_size); ~BST() { release(root); }; void in_traverse() { traverse(root); } void insert(DataType key) { bi_insert(root, key); } void del(DataType key) { root = bi_delete(root, key); } BST_node* search(DataType key) { return bi_search(root, key); } private: void traverse(BST_node*); void release(BST_node* node); void bi_insert(BST_node*& nood, DataType key); BST_node* bi_delete(BST_node* node, DataType key); BST_node* bi_search(BST_node* node, DataType key); }; void BST::bi_insert(BST_node* &Root, DataType data) { if (Root == NULL) { BST_node* new_node = new BST_node; new_node->data = data; new_node->left = NULL; new_node->right = NULL; Root = new_node; } else { if (Root->data > data) bi_insert(Root->left, data); if(Root->data<data) bi_insert(Root->right, data); } } void BST::release(BST_node* node) { if (node == NULL) return; release(node->left); release(node->right); delete node; return; } BST::BST(DataType arr[], size_t arr_size) { root = NULL; for (int i = 0; i < arr_size; i++) { bi_insert(root, arr[i]); } } void BST::traverse(BST_node* node) { if (node == NULL) return; traverse(node->left); cout << node->data << " "; traverse(node->right); } BST_node* BST::bi_search(BST_node* node, DataType key) { if (node == NULL) return NULL; if (node->data > key) return bi_search(node->left, key); else if (node->data < key) return bi_search(node->right, key); else return node; } BST_node* BST::bi_delete(BST_node* root, DataType key) { BST_node* target = bi_search(root, key); BST_node* copy_root = root; BST_node* temp = NULL; if (root->data == key) { temp = root->left; root = root->right; while (root->left != NULL) root = root->left; root->left = temp; return copy_root->right; } else { BST_node* temp1 = NULL; while ((root->left==NULL || root->left->data != key) &&(root->right==NULL || root->right->data != key)) key < root->data ? root = root->left : root = root->right; key < root->data ? temp = root->left : temp = root->right; temp1 = temp->right; if (temp1 == NULL) { if (temp->data > root->data) root->right = temp->left; else root->left = temp->left; return copy_root; } else { while (temp1->left != NULL) temp1 = temp1->left; temp1->left = temp->left; if (temp->data < root->data) { root->left = temp->right; delete temp; return copy_root; } else { root->right = temp->right; delete temp; return copy_root; } } } } int main() { array<int, 10>arr{ 12,32,43,23,45,15,76,48,98,56 }; BST bi_ser_tre(arr.data(), arr.size()); bi_ser_tre.in_traverse(); cout << endl; cout << "插入9:" << endl; bi_ser_tre.insert(9); bi_ser_tre.in_traverse(); cout << endl; cout << "删除76:" << endl; bi_ser_tre.del(76); bi_ser_tre.in_traverse(); return 0; }
|