1 //与上一次实现不同,这里使用了大量的指针引用, 2 //因此节省了一个指向父节点的指针 3 #include<iostream> 4 #include<vector> 5 #include<ctime> 6 #include<cstdlib> 7 8 using namespace std; 9 10 template<class T> 11 class BinarySearchTree 12 { 13 private: 14 struct Node 15 { 16 T data; 17 Node *left; 18 Node *right; 19 Node(T val) 20 { 21 data = val; 22 left = NULL; 23 right = NULL; 24 } 25 26 private: 27 Node() 28 { 29 } 30 }; 31 Node *root; 32 int size; 33 34 public: 35 BinarySearchTree() 36 { 37 root = NULL; 38 size = 0; 39 } 40 ~BinarySearchTree() 41 { 42 clear(root); 43 root = NULL; 44 size = 0; 45 } 46 Node*& findMin(Node *&node) const 47 { 48 if(node->left == NULL) 49 return node; 50 else 51 return findMin(node->left); 52 } 53 T max(Node *node) const 54 { 55 if(node->right == NULL) 56 return node->data; 57 else 58 return max(node->right); 59 } 60 61 Node *& insert(Node *& node,T val) 62 { 63 if(node == NULL) 64 { 65 node = new Node(val); 66 size++; 67 return node; 68 } 69 if(val > node->data) 70 return insert(node->right,val); 71 else if(val < node->data) 72 return insert(node->left,val); 73 else 74 return node; 75 } 76 77 bool contain(Node *& node,T val) const 78 { 79 if(node == NULL) 80 return false; 81 82 if(val > node->data) 83 return contain(node->right,val); 84 else if(val < node->data) 85 return contain(node->left,val); 86 else 87 return true; 88 } 89 90 void remove(Node* &node,T val) 91 { 92 if(node == NULL) 93 { 94 cout<<"remove() error!\n"; 95 return; 96 } 97 if(val > node->data) 98 remove(node->right,val); 99 else if(val < node->data) 100 remove(node->left,val); 101 else if(val == node->data) 102 { 103 if(node->left == NULL && node->right == NULL) 104 { 105 delete node; 106 node = NULL; 107 } 108 else if(node->left == NULL && node->right != NULL) 109 { 110 Node *p = node; 111 node = node->right; 112 delete p; 113 size--; 114 } 115 else if(node->left != NULL && node->right == NULL) 116 { 117 Node *p = node; 118 node = node->left; 119 delete p; 120 size--; 121 } 122 else if(node->left != NULL && node->right != NULL) 123 { 124 Node* &p = findMin(node->right); 125 node->data = p->data; 126 p = p->right; 127 delete p; 128 size--; 129 } 130 } 131 } 132 void clear(Node* &node) 133 { 134 if(node->left != NULL) 135 clear(node->left); 136 if(node->right != NULL) 137 clear(node->right); 138 139 delete node; 140 node = NULL; 141 } 142 void print(Node *node) 143 { 144 if(node == NULL) 145 return; 146 cout<<node->data<< " "; 147 if(node->left != NULL) 148 print(node->left); 149 if(node->right != NULL) 150 print(node->right); 151 } 152 void insert(T val) 153 { 154 insert(root,val); 155 } 156 void remove(T val) 157 { 158 remove(root,val); 159 } 160 void print() 161 { 162 print(root); 163 cout<<"\n"; 164 } 165 }; 166 167 int main() 168 { 169 BinarySearchTree<int> tree; 170 tree.insert(10); 171 tree.insert(1); 172 tree.insert(11); 173 tree.insert(9); 174 tree.insert(8); 175 tree.print(); 176 cout<<"\n\n"; 177 tree.remove(9); 178 tree.print(); 179 180 return 0; 181 }
时间: 2024-11-10 03:42:29