为了认真学习一遍数据结构,将大部分常用(少部分不常用)的数据结构都写一遍(顺便提高代码能力)。
附:http://visualgo.net/ 这是一个很好的数据结构(算法)的动态演示网站,极具创新性。非常有助于理解和实现数据结构(算法)
1 #ifndef _KIRAI_LIST 2 #define _KIRAI_LIST 3 4 #include <cstdlib> 5 namespace kirai { 6 template <class type> 7 struct listnode { 8 typedef listnode<type>* np; 9 type _data; 10 np pre; 11 np next; 12 listnode<type>() { pre = NULL; next = NULL; } 13 }; 14 15 template <class type> 16 class list { 17 typedef listnode<type>* np; 18 typedef listnode<type> nt; 19 20 public: 21 list<type>() { head = NULL; tail = NULL; _size = 0; } 22 ~list<type>() { clear(); } 23 24 public: 25 bool push_back(type); 26 bool push_front(type); 27 type pop_back(); 28 type pop_front(); 29 void clear(); 30 bool empty() const; 31 int size() const; 32 type front() const; 33 type back() const; 34 bool insert(type, int); 35 bool remove(int); 36 type data(int); 37 38 public: 39 type &operator[] (const int&) const; 40 41 protected: 42 np head; 43 np tail; 44 45 private: 46 int _size; 47 void _init(type); 48 }; 49 50 //operator overload 51 template <class type> 52 type &list<type>::operator[] (const int &num) const { 53 if (num >= _size || num < 0) { 54 exit(EXIT_FAILURE); 55 } 56 np cur = head; 57 for (int i = 0; i < num; i++) { 58 cur = cur->next; 59 } 60 return cur->_data; 61 } 62 63 //functions 64 template <class type> 65 type list<type>::front() const { 66 return head->_data; 67 } 68 69 template <class type> 70 type list<type>::back() const { 71 return tail->_data; 72 } 73 74 template <class type> 75 bool list<type>::empty() const { 76 return _size == 0 ? true : false; 77 } 78 79 template <class type> 80 int list<type>::size() const { 81 return _size; 82 } 83 84 template <class type> 85 void list<type>::_init(type val) { 86 if (head != NULL) { 87 clear(); 88 } 89 head = new nt; 90 head->_data = val; 91 tail = head; 92 _size++; 93 } 94 95 template <class type> 96 bool list<type>::push_back(type val) { 97 if (empty()) { 98 _init(val); 99 return true; 100 } 101 np tmp = new nt; 102 if (tmp == NULL) { 103 return false; 104 } 105 tmp->_data = val; 106 tmp->pre = tail; 107 tail->next = tmp; 108 tail = tmp; 109 tail->next = NULL; 110 _size++; 111 return true; 112 } 113 114 template <class type> 115 bool list<type>::push_front(type val) { 116 if (empty()) { 117 _init(val); 118 return true; 119 } 120 np tmp = new nt; 121 if (tmp == NULL) { 122 return false; 123 } 124 tmp->_data = val; 125 tmp->next = head; 126 head = tmp; 127 _size++; 128 return true; 129 } 130 131 template <class type> 132 type list<type>::pop_back() { 133 if (empty()) { 134 exit(EXIT_FAILURE); 135 } 136 type tmp = tail->_data; 137 tail = tail->pre; 138 delete tail->next; 139 _size--; 140 return tmp; 141 } 142 143 template<class type> 144 type list<type>::pop_front() { 145 if (empty()) { 146 exit(EXIT_FAILURE); 147 } 148 type tmp = head->_data; 149 head = head->next; 150 delete head->pre; 151 _size--; 152 return tmp; 153 } 154 155 template <class type> 156 void list<type>::clear() { 157 if (_size == 0) { 158 return; 159 } 160 np cur = tail; 161 np tmp = tail; 162 while (cur->next != NULL) { 163 cur = cur->pre; 164 delete tmp; 165 tmp = cur; 166 } 167 delete cur->pre; 168 _size = 0; 169 } 170 171 template <class type> 172 bool list<type>::insert(type val, int pos) { 173 if (pos >= _size || pos < 0) { 174 return false; 175 } 176 np cur = head; 177 for (int i = 0; i != pos; i++) { 178 cur = cur->next; 179 } 180 np tmp = new nt; 181 tmp->pre = cur; 182 tmp->next = cur->next; 183 tmp->_data = val; 184 185 cur->next->pre = tmp; 186 cur->next = tmp; 187 _size++; 188 return true; 189 } 190 191 template <class type> 192 bool list<type>::remove(int pos) { 193 np cur = head; 194 if (pos >= _size || pos < 0) { 195 return false; 196 } 197 if (_size == 1) { 198 clear(); 199 return true; 200 } 201 if (pos == 0) { 202 pop_front(); 203 return true; 204 } 205 if (pos == _size - 1) { 206 pop_back(); 207 return true; 208 } 209 for (int i = 0; i != pos; i++) { 210 cur = cur->next; 211 } 212 cur->pre->next = cur->next; 213 cur->next->pre = cur->pre; 214 cur->pre = NULL; 215 cur->next = NULL; 216 delete cur; 217 _size--; 218 return true; 219 } 220 221 template <class type> 222 type list<type>::data(int pos) { 223 np cur = &head; 224 for (int i = 0; i < pos; i++){ 225 cur = cur->next; 226 } 227 return cur->_data; 228 } 229 } 230 #endif
1 #ifndef _KIRAI_QUEUE 2 #define _KIRAI_QUEUE 3 4 #include <cstdlib> 5 6 namespace kirai { 7 template <class type> 8 struct queuenode { 9 typedef queuenode<type>* np; 10 type data; 11 np next; 12 queuenode<type>() { next = NULL; } 13 }; 14 15 template <class type> 16 class queue { 17 typedef queuenode<type>* np; 18 typedef queuenode<type> nt; 19 20 public: 21 queue<type>() { head = NULL; tail = NULL; _size = 0; } 22 ~queue<type>() { clear(); } 23 24 public: 25 bool push_back(type); 26 type pop_front(); 27 void clear(); 28 bool empty() const; 29 int size() const; 30 type front() const; 31 type back() const; 32 33 protected: 34 np head; 35 np tail; 36 37 private: 38 int _size; 39 void _init(type); 40 void _clear(np); 41 }; 42 43 template <class type> 44 void queue<type>::_clear(np cur) { 45 if (cur == NULL) { 46 return; 47 } 48 _clear(cur->next); 49 delete cur; 50 } 51 52 template <class type> 53 type queue<type>::front() const { 54 return head->data; 55 } 56 57 template <class type> 58 type queue<type>::back() const { 59 return tail->data; 60 } 61 62 template <class type> 63 bool queue<type>::empty() const { 64 return _size == 0 ? true : false; 65 } 66 67 template <class type> 68 int queue<type>::size() const { 69 return _size; 70 } 71 72 template <class type> 73 void queue<type>::_init(type val) { 74 if (head != NULL) { 75 clear(); 76 } 77 head = new nt; 78 head->data = val; 79 tail = head; 80 _size++; 81 } 82 83 template <class type> 84 bool queue<type>::push_back(type val) { 85 if (_size == 0) { 86 _init(val); 87 return true; 88 } 89 np tmp = new nt; 90 if (tmp == NULL) { 91 return false; 92 } 93 tmp->data = val; 94 while (tail->next != NULL) { 95 tail = tail->next; 96 } 97 tail->next = tmp; 98 tail = tail->next; 99 _size++; 100 return true; 101 } 102 103 template<class type> 104 type queue<type>::pop_front() { 105 if (empty()) { 106 exit(EXIT_FAILURE); 107 } 108 np cur = head; 109 type tmp = cur->data; 110 head = head->next; 111 delete cur; 112 _size--; 113 return tmp; 114 } 115 116 template<class type> 117 void queue<type>::clear() { 118 _clear(head); 119 head = NULL; 120 _size = 0; 121 } 122 } 123 #endif
1 #ifndef _KIRAI_STACK 2 #define _KIRAI_STACK 3 4 #include <cstdlib> 5 6 namespace kirai { 7 template <class type> 8 struct stacknode { 9 typedef stacknode<type>* np; 10 type data; 11 np pre; 12 stacknode<type>() { pre = NULL; } 13 }; 14 15 template <class type> 16 class stack { 17 typedef stacknode<type>* np; 18 typedef stacknode<type> nt; 19 20 public: 21 stack<type>() { _top = NULL; _size = 0; } 22 ~stack<type>() { clear(); } 23 24 public: 25 bool push(type); 26 type pop(); 27 void clear(); 28 bool empty() const; 29 int size() const; 30 type top() const; 31 32 protected: 33 np _top; 34 int _size; 35 36 private: 37 void _init(type); 38 void _clear(np); 39 }; 40 41 template <class type> 42 type stack<type>::top() const { 43 return _top->data; 44 } 45 template <class type> 46 bool stack<type>::empty() const { 47 return _size == 0 ? true : false; 48 } 49 50 template <class type> 51 void stack<type>::_clear(np cur) { 52 if (cur == NULL) { 53 return; 54 } 55 _clear(cur->pre); 56 delete cur; 57 } 58 59 template <class type> 60 int stack<type>::size() const { 61 return _size; 62 } 63 64 template <class type> 65 void stack<type>::_init(type val) { 66 if (_top != NULL) { 67 clear(); 68 } 69 _top = new nt; 70 _top->data = val; 71 _size++; 72 } 73 74 template <class type> 75 bool stack<type>::push(type val) { 76 if (_size == 0) { 77 _init(val); 78 return true; 79 } 80 np tmp = new nt; 81 if (tmp == NULL) { 82 return false; 83 } 84 tmp->data = val; 85 tmp->pre = _top; 86 _top = tmp; 87 _size++; 88 return true; 89 } 90 91 template <class type> 92 type stack<type>::pop() { 93 if (empty()) { 94 exit(EXIT_FAILURE); 95 } 96 np cur = _top; 97 type tmp = cur->data; 98 _top = _top->pre; 99 delete cur; 100 _size--; 101 return tmp; 102 } 103 104 template<class type> 105 void stack<type>::clear() { 106 _clear(_top); 107 _top = NULL; 108 _size = 0; 109 } 110 } 111 #endif
1 #ifndef _KIRAI_DEQUE 2 #define _KIRAI_DEQUE 3 4 #include <cstdlib> 5 6 namespace kirai { 7 template <class type> 8 struct node { 9 typedef node<type>* np; 10 type data; 11 np pre; 12 np next; 13 int pos; 14 node<type>() { pre = NULL; next = NULL; } 15 }; 16 17 template <class type> 18 class deque { 19 typedef node<type>* np; 20 typedef node<type> nt; 21 22 public: 23 deque<type>() { head = NULL; tail = NULL; _size = 0; } 24 ~deque<type>() { clear(); } 25 26 public: 27 bool push_back(type); 28 bool push_front(type); 29 type pop_back(); 30 type pop_front(); 31 void clear(); 32 bool empty() const; 33 int size() const; 34 type front() const; 35 type back() const; 36 protected: 37 np head; 38 np tail; 39 40 private: 41 int _size; 42 void _init(type); 43 }; 44 45 template <class type> 46 type deque<type>::front() const { 47 return head->data; 48 } 49 50 template <class type> 51 type deque<type>::back() const { 52 return tail->data; 53 } 54 55 template <class type> 56 bool deque<type>::empty() const { 57 return _size == 0 ? true : false; 58 } 59 60 template <class type> 61 int deque<type>::size() const { 62 return _size; 63 } 64 65 template <class type> 66 void deque<type>::_init(type val) { 67 if (head != NULL) { 68 clear(); 69 } 70 head = new nt; 71 head->data = val; 72 tail = head; 73 _size++; 74 } 75 76 template <class type> 77 bool deque<type>::push_back(type val) { 78 if (_size == 0) { 79 _init(val); 80 return true; 81 } 82 np tmp = new nt; 83 if (tmp == NULL) { 84 return false; 85 } 86 tmp->data = val; 87 tmp->pre = tail; 88 tail->next = tmp; 89 tail = tmp; 90 _size++; 91 return true; 92 } 93 94 template <class type> 95 bool deque<type>::push_front(type val) { 96 if (empty()) { 97 _init(val); 98 return true; 99 } 100 np tmp = new nt; 101 if (tmp == NULL) { 102 return false; 103 } 104 tmp->data = val; 105 tmp->next = head; 106 head = tmp; 107 _size++; 108 return true; 109 } 110 111 template <class type> 112 type deque<type>::pop_back() { 113 if (empty()) { 114 exit(EXIT_FAILURE); 115 } 116 np cur = tail; 117 type tmp = cur->data; 118 tail = tail->pre; 119 delete cur; 120 _size--; 121 return tmp; 122 } 123 124 template<class type> 125 type deque<type>::pop_front() { 126 if (empty()) { 127 exit(EXIT_FAILURE); 128 } 129 np cur = &head; 130 type tmp = cur->data; 131 head = head->next; 132 delete cur; 133 _size--; 134 return tmp; 135 } 136 137 template <class type> 138 void deque<type>::clear() { 139 if (_size == 0) { 140 return; 141 } 142 np cur = tail; 143 np tmp = tail; 144 while (cur->next != NULL) { 145 cur = cur->pre; 146 delete tmp; 147 tmp = cur; 148 } 149 delete cur->pre; 150 _size = 0; 151 } 152 } 153 #endif
1 typedef unsigned long long ull; 2 const int B = 100007; 3 // const int mod = (1<<30); 4 ull quickmul(int x, int n) { 5 ull ans = 1; 6 ull t = x; 7 while(n) { 8 if(n & 1) { 9 ans = (ans * t); 10 } 11 t = t * t; 12 n >>= 1; 13 } 14 return ans; 15 } 16 17 //a是否在b中出现 18 bool contain(string a, string b) { 19 if(a.length() > b.length()) { 20 return false; 21 } 22 ull t = quickmul(B, a.length()); 23 ull ah = 0, bh = 0; 24 for(int i = 0; i < a.length(); i++) { 25 ah = ah * B + a[i]; 26 } 27 for(int i = 0; i < a.length(); i++) { 28 bh = bh * B + b[i]; 29 } 30 //b不断右移,更新hash值并判断 31 for(int i = 0; i + a.length() <= b.length(); i++) { 32 if(ah == bh) { 33 // i值为位置 34 return true; 35 } 36 if(i + a.length() < b.length()) { 37 bh = bh * B + b[i+a.length()] - b[i] * t; 38 cout << "ah = " << ah << " bh = " << bh << endl; 39 } 40 } 41 return false; 42 }
1 #ifndef _KIRAI_BST 2 #define _KIRAI_BST 3 #include "queue" 4 5 namespace kirai { 6 template<class type> 7 struct bstnode { 8 typedef bstnode<type>* np; 9 type _data; 10 np pre; 11 np left; 12 np right; 13 bstnode<type>() { pre = NULL; left = NULL; right = NULL; } 14 bstnode<type>(const int& x) : _data(x) { bstnode<type>(); } 15 }; 16 17 template<class type> 18 class bst { 19 typedef bstnode<type>* np; 20 typedef bstnode<type> nt; 21 22 public: 23 bst() { _root = NULL; } 24 ~bst() = default; 25 bool empty() const { return _root == NULL; } 26 void insert(const type&); 27 bool search(const type& x) const { return _search(_root, x) ? true : false; } 28 bool remove(const type&); 29 type min() const { return _min(_root)->_data; } 30 type max() const { return _max(_root)->_data; } 31 void bfs(void(*)(type)); 32 33 public: 34 void preorder(void(*visit)(type)) { _preorder(_root, visit); }; 35 void inorder(void(*visit)(type)) { _inorder(_root, visit); }; 36 void postorder(void(*visit)(type)) { _postorder(_root, visit); }; 37 38 protected: 39 bstnode<type>* _search(np, const type&); 40 bstnode<type>* _min(np) const; 41 bstnode<type>* _max(np) const; 42 void _insert(np, const type&); 43 void _inorder(np, void(*)(type)); 44 void _postorder(np, void(*)(type)); 45 void _preorder(np, void(*)(type)); 46 type _remove(np); 47 type __remove(np); 48 49 protected: 50 static bool _isroot(np cur) { return (cur->pre == NULL); } 51 static bool _isleaf(np cur) { return (cur->left == NULL && cur->right == NULL); } 52 53 private: 54 np _root; 55 }; 56 57 template<class type> 58 void bst<type>::bfs(void(*visit)(type data)) { 59 kirai::queue<np> q; 60 q.push_back(_root); 61 while (!q.empty()) { 62 np tmp = q.front(); 63 visit(q.front()->_data); 64 if(q.front()->left) q.push_back(tmp->left); 65 if(q.front()->right) q.push_back(tmp->right); 66 q.pop_front(); 67 } 68 } 69 70 template <class type> 71 bstnode<type>* bst<type>::_min(np cur) const { 72 if (empty()) { 73 return NULL; 74 } 75 while (cur->left) { 76 cur = cur->left; 77 } 78 return cur; 79 } 80 81 template <class type> 82 bstnode<type>* bst<type>::_max(np cur) const { 83 if (empty()) { 84 return NULL; 85 } 86 while (cur->right) { 87 cur = cur->right; 88 } 89 return cur; 90 } 91 92 template <class type> 93 bstnode<type>* bst<type>::_search(np cur, const type& x) { 94 if (cur == NULL) { 95 return NULL; 96 } 97 if (cur->_data == x) { 98 return cur; 99 } 100 else if (x >= cur->_data) { 101 return _search(cur->right, x); 102 } 103 else { 104 return _search(cur->left, x); 105 } 106 } 107 108 template <class type> 109 bool bst<type>::remove(const type& x) { 110 np cur = _search(_root, x); 111 if (cur == NULL) { 112 return false; 113 } 114 _remove(cur); 115 return true; 116 } 117 118 template <class type> 119 type bst<type>::_remove(np cur) { 120 np tmp; 121 type x; 122 if(_isleaf(cur)) { 123 return __remove(cur); 124 } 125 else { 126 /*if (cur->left != NULL) { 127 tmp = _max(cur->left); 128 } 129 else { 130 tmp = _min(cur->right); 131 }*/ 132 tmp = cur->left ? _max(cur->left) : _min(cur->right); 133 x = cur->_data; 134 cur->_data = _remove(tmp); 135 return x; 136 } 137 } 138 139 template <class type> 140 type bst<type>::__remove(np cur) { 141 type x = cur->_data; 142 np pre = cur->pre; 143 if (!_isroot(cur)) { 144 if (pre->left == cur) { 145 pre->left = NULL; 146 } 147 else { 148 pre->right = NULL; 149 } 150 } 151 delete cur; 152 return x; 153 } 154 155 template <class type> 156 void bst<type>::insert(const type& x) { 157 if (empty()) { 158 np tmp = new nt; 159 _root = tmp; 160 _root->_data = x; 161 return; 162 } 163 _insert(_root, x); 164 return; 165 } 166 167 template <class type> 168 void bst<type>::_insert(np cur, const type& x) { 169 if (x >= cur->_data) { 170 if (cur->right == NULL) { 171 np tmp = new nt; 172 cur->right = tmp; 173 cur->right->pre = cur; 174 tmp->_data = x; 175 return; 176 } 177 else { 178 _insert(cur->right, x); 179 } 180 } 181 else { 182 if (cur->left == NULL) { 183 np tmp = new nt; 184 cur->left = tmp; 185 cur->left->pre = cur; 186 tmp->_data = x; 187 return; 188 } 189 else { 190 _insert(cur->left, x); 191 } 192 } 193 } 194 195 template <class type> 196 void bst<type>::_preorder(np cur, void(*visit)(type data)) { 197 if (cur != NULL) { 198 (*visit)(cur->_data); 199 _preorder(cur->left, visit); 200 _preorder(cur->right, visit); 201 } 202 } 203 204 template <class type> 205 void bst<type>::_inorder(np cur, void(*visit)(type data)) { 206 if (cur != NULL) { 207 _inorder(cur->left, visit); 208 (*visit)(cur->_data); 209 _inorder(cur->right, visit); 210 } 211 } 212 213 template <class type> 214 void bst<type>::_postorder(np cur, void(*visit)(type data)) { 215 if (cur != NULL) { 216 _postorder(cur->left, visit); 217 _postorder(cur->right, visit); 218 (*visit)(cur->_data); 219 } 220 } 221 } 222 223 #endif
1 #ifndef _KIRAI_BST 2 #define _KIRAI_BST 3 #include "queue" 4 5 namespace kirai { 6 template<class type> 7 struct bstnode { 8 typedef bstnode<type>* np; 9 type _data; 10 int height; 11 np left; 12 np right; 13 bstnode<type>() { height = 0; left = NULL; right = NULL; } 14 bstnode<type>(const int& x) : _data(x) { bstnode<type>(); } 15 }; 16 17 template<class type> 18 class bst { 19 typedef bstnode<type>* np; 20 typedef bstnode<type> nt; 21 22 public: 23 bst() { _root = NULL; } 24 ~bst() = default; 25 26 //Interfaces 27 public: 28 bool empty() const { return _root == NULL; } 29 void insert(const type&); 30 bool search(const type& x) const { return _search(_root, x) ? true : false; } 31 bool remove(const type&); 32 type min() const { return _min(_root)->_data; } 33 type max() const { return _max(_root)->_data; } 34 void bfs(void(*)(type)); 35 36 public: 37 int height() { return empty() ? -1 : _root->height; } 38 39 public: 40 void preorder(void(*visit)(type)) { _preorder(_root, visit); }; 41 void inorder(void(*visit)(type)) { _inorder(_root, visit); }; 42 void postorder(void(*visit)(type)) { _postorder(_root, visit); }; 43 void clear() { _clear(_root); _root = NULL; }; 44 45 protected: 46 int _height(np cur) { return _empty(cur) ? -1 : cur->height; } 47 48 protected: 49 bstnode<type>* _search(np, const type&); 50 bstnode<type>* _min(np) const; 51 bstnode<type>* _max(np) const; 52 void _clear(np); 53 void _setnull(np, np); 54 int _setheight(np); 55 void _insert(np, const type&); 56 void _inorder(np, void(*)(type)); 57 void _postorder(np, void(*)(type)); 58 void _preorder(np, void(*)(type)); 59 type _remove(np); 60 type __remove(np); 61 bool _empty(np cur) const { return cur == NULL; } 62 63 protected: 64 bool _isroot(np cur) { return (cur == _root); } 65 static bool _isleaf(np cur) { return (cur->left == NULL && cur->right == NULL); } 66 67 private: 68 np _root; 69 }; 70 71 template<class type> 72 void bst<type>::_clear(np cur) { 73 if (cur->left) _clear(cur->left); 74 if (cur->right) _clear(cur->right); 75 _remove(cur); 76 } 77 78 template<class type> 79 int bst<type>::_setheight(np cur) { 80 if (!cur) return -1; 81 if (_isleaf(cur)) return cur->height = 0; 82 int a = _setheight(cur->left); 83 int b = _setheight(cur->right); 84 return cur->height = (a > b ? a : b) + 1; 85 } 86 87 template<class type> 88 void bst<type>::bfs(void(*visit)(type data)) { 89 if (empty()) return; 90 kirai::queue<np> q; 91 q.push_back(_root); 92 while (!q.empty()) { 93 np tmp = q.front(); 94 visit(q.front()->_data); 95 //printf("%d ", tmp->height); 96 if (q.front()->left) q.push_back(tmp->left); 97 if (q.front()->right) q.push_back(tmp->right); 98 q.pop_front(); 99 } 100 } 101 102 template <class type> 103 void bst<type>::_setnull(np cur, np aim) { 104 if (cur->left == aim) { 105 cur->left = NULL; 106 return; 107 } 108 if (cur->right == aim) { 109 cur->right = NULL; 110 return; 111 } 112 if (aim->_data > cur->_data) _setnull(cur->right, aim); 113 if (aim->_data < cur->_data) _setnull(cur->left, aim); 114 } 115 116 template <class type> 117 bstnode<type>* bst<type>::_min(np cur) const { 118 if (empty()) { 119 return NULL; 120 } 121 while (cur->left) { 122 cur = cur->left; 123 } 124 return cur; 125 } 126 127 template <class type> 128 bstnode<type>* bst<type>::_max(np cur) const { 129 if (empty()) { 130 return NULL; 131 } 132 while (cur->right) { 133 cur = cur->right; 134 } 135 return cur; 136 } 137 138 template <class type> 139 bstnode<type>* bst<type>::_search(np cur, const type& x) { 140 if (cur == NULL) { 141 return NULL; 142 } 143 if (cur->_data == x) { 144 return cur; 145 } 146 else if (x > cur->_data) { 147 return _search(cur->right, x); 148 } 149 else { 150 return _search(cur->left, x); 151 } 152 } 153 154 template <class type> 155 bool bst<type>::remove(const type& x) { 156 np cur = _search(_root, x); 157 if (cur == NULL) { 158 return false; 159 } 160 _remove(cur); 161 _setheight(_root); 162 return true; 163 } 164 165 template <class type> 166 type bst<type>::_remove(np cur) { 167 np tmp; 168 type x; 169 if (_isleaf(cur)) { 170 x = cur->_data; 171 if (!_isroot(cur)) { 172 _setnull(_root, cur); 173 delete(cur); 174 cur = NULL; 175 } 176 return x; 177 } 178 else { 179 tmp = cur->left ? _max(cur->left) : _min(cur->right); 180 x = cur->_data; 181 cur->_data = _remove(tmp); 182 return x; 183 } 184 } 185 186 template <class type> 187 void bst<type>::insert(const type& x) { 188 if (empty()) { 189 np tmp = new nt(); 190 _root = tmp; 191 _root->_data = x; 192 _root->height = 0; 193 return; 194 } 195 _insert(_root, x); 196 } 197 198 template <class type> 199 void bst<type>::_insert(np cur, const type& x) { 200 if (x > cur->_data) { 201 if (cur->right == NULL) { 202 np tmp = new nt(); 203 cur->right = tmp; 204 tmp->_data = x; 205 } 206 else { 207 _insert(cur->right, x); 208 } 209 } 210 if (x < cur->_data) { 211 if (cur->left == NULL) { 212 np tmp = new nt(); 213 cur->left = tmp; 214 cur->height++; 215 tmp->_data = x; 216 } 217 else { 218 _insert(cur->left, x); 219 } 220 } 221 cur->height = ( 222 _height(cur->left) > _height(cur->right) ? 223 _height(cur->left) : _height(cur->right) 224 ) + 1; 225 } 226 227 template <class type> 228 void bst<type>::_preorder(np cur, void(*visit)(type data)) { 229 if (cur != NULL) { 230 (*visit)(cur->_data); 231 _preorder(cur->left, visit); 232 _preorder(cur->right, visit); 233 } 234 } 235 236 template <class type> 237 void bst<type>::_inorder(np cur, void(*visit)(type data)) { 238 if (cur != NULL) { 239 _inorder(cur->left, visit); 240 (*visit)(cur->_data); 241 _inorder(cur->right, visit); 242 } 243 } 244 245 template <class type> 246 void bst<type>::_postorder(np cur, void(*visit)(type data)) { 247 if (cur != NULL) { 248 _postorder(cur->left, visit); 249 _postorder(cur->right, visit); 250 (*visit)(cur->_data); 251 } 252 } 253 } 254 255 #endif
1 #ifndef _KIRAI_AVL 2 #define _KIRAI_AVL 3 #include "queue" 4 5 namespace kirai { 6 template<class type> 7 struct avlnode { 8 typedef avlnode<type>* np; 9 type _data; 10 int height; 11 np left; 12 np right; 13 avlnode<type>() { height = 0; left = NULL; right = NULL; } 14 avlnode<type>(const int& x) : _data(x) { avlnode<type>(); } 15 }; 16 17 template<class type> 18 class avl { 19 typedef avlnode<type>* np; 20 typedef avlnode<type> nt; 21 22 public: 23 avl() { _root = NULL; } 24 ~avl() = default; 25 26 //Interfaces 27 public: 28 bool empty() const { return _root == NULL; } 29 void insert(const type&); 30 bool search(const type& x) const { return _search(_root, x) ? true : false; } 31 bool remove(const type&); 32 type min() const { return _min(_root)->_data; } 33 type max() const { return _max(_root)->_data; } 34 void bfs(void(*)(type)); 35 36 public: 37 int height() { return empty() ? -1 : _root->height; } 38 39 public: 40 void preorder(void(*visit)(type)) { _preorder(_root, visit); }; 41 void inorder(void(*visit)(type)) { _inorder(_root, visit); }; 42 void postorder(void(*visit)(type)) { _postorder(_root, visit); }; 43 void clear() { _clear(_root); _root = NULL; }; 44 45 protected: 46 int _height(np cur) { return _empty(cur) ? -1 : cur->height; } 47 48 protected: 49 avlnode<type>* _search(np, const type&); 50 avlnode<type>* _min(np) const; 51 avlnode<type>* _max(np) const; 52 void _clear(np); 53 void _setnull(np, np); 54 int _setheight(np); 55 avlnode<type>* _insert(np, const type&); 56 void _inorder(np, void(*)(type)); 57 void _postorder(np, void(*)(type)); 58 void _preorder(np, void(*)(type)); 59 type _remove(np); 60 type __remove(np); 61 bool _empty(np cur) const { return cur == NULL; } 62 63 protected: 64 bool _isroot(np cur) { return (cur == _root); } 65 static bool _isleaf(np cur) { return (cur->left == NULL && cur->right == NULL); } 66 avlnode<type>* _left_rotate(np); 67 avlnode<type>* _right_rotate(np); 68 avlnode<type>* _double_left_rotate(np); 69 avlnode<type>* _double_right_rotate(np); 70 71 private: 72 np _root; 73 }; 74 75 template<class type> 76 avlnode<type>* avl<type>::_left_rotate(np cur) { 77 np tmp; 78 tmp = cur->left; 79 tmp->right = cur; 80 cur->height = ( 81 _height(cur->left) > _height(cur->right) ? 82 _height(cur->left) : _height(cur->right) 83 ) + 1; 84 85 tmp->height = ( 86 _height(tmp->left) > _height(cur->right) ? 87 _height(tmp->left) : _height(cur->right) 88 ) + 1; 89 return tmp; 90 91 } 92 93 template<class type> 94 avlnode<type>* avl<type>::_right_rotate(np cur) { 95 np tmp; 96 tmp = cur->right; 97 tmp->left = cur; 98 cur->height = ( 99 _height(cur->left) > _height(cur->right) ? 100 _height(cur->left) : _height(cur->right) 101 ) + 1; 102 103 tmp->height = ( 104 _height(tmp->right) > _height(cur->left) ? 105 _height(tmp->right) : _height(cur->left) 106 ) + 1; 107 return tmp; 108 } 109 110 template<class type> 111 avlnode<type>* avl<type>::_double_left_rotate(np cur) { 112 cur->left = _right_rotate(cur->left); 113 return _left_rotate(cur); 114 } 115 116 template<class type> 117 avlnode<type>* avl<type>::_double_right_rotate(np cur) { 118 cur->right = _left_rotate(cur->right); 119 return _right_rotate(cur); 120 } 121 122 template<class type> 123 void avl<type>::_clear(np cur) { 124 if (cur->left) _clear(cur->left); 125 if (cur->right) _clear(cur->right); 126 _remove(cur); 127 } 128 129 template<class type> 130 int avl<type>::_setheight(np cur) { 131 if (!cur) return -1; 132 if (_isleaf(cur)) return cur->height = 0; 133 int a = _setheight(cur->left); 134 int b = _setheight(cur->right); 135 return cur->height = (a > b ? a : b) + 1; 136 } 137 138 template<class type> 139 void avl<type>::bfs(void(*visit)(type data)) { 140 if (empty()) return; 141 kirai::queue<np> q; 142 q.push_back(_root); 143 while (!q.empty()) { 144 np tmp = q.front(); 145 visit(q.front()->_data); 146 printf("%d ", tmp->height); 147 if (q.front()->left) q.push_back(tmp->left); 148 if (q.front()->right) q.push_back(tmp->right); 149 q.pop_front(); 150 } 151 } 152 153 template <class type> 154 void avl<type>::_setnull(np cur, np aim) { 155 if (cur->left == aim) { 156 cur->left = NULL; 157 return; 158 } 159 if (cur->right == aim) { 160 cur->right = NULL; 161 return; 162 } 163 if (aim->_data > cur->_data) _setnull(cur->right, aim); 164 if (aim->_data < cur->_data) _setnull(cur->left, aim); 165 } 166 167 template <class type> 168 avlnode<type>* avl<type>::_min(np cur) const { 169 if (empty()) { 170 return NULL; 171 } 172 while (cur->left) { 173 cur = cur->left; 174 } 175 return cur; 176 } 177 178 template <class type> 179 avlnode<type>* avl<type>::_max(np cur) const { 180 if (empty()) { 181 return NULL; 182 } 183 while (cur->right) { 184 cur = cur->right; 185 } 186 return cur; 187 } 188 189 template <class type> 190 avlnode<type>* avl<type>::_search(np cur, const type& x) { 191 if (cur == NULL) { 192 return NULL; 193 } 194 if (cur->_data == x) { 195 return cur; 196 } 197 else if (x > cur->_data) { 198 return _search(cur->right, x); 199 } 200 else { 201 return _search(cur->left, x); 202 } 203 } 204 205 template <class type> 206 bool avl<type>::remove(const type& x) { 207 np cur = _search(_root, x); 208 if (cur == NULL) { 209 return false; 210 } 211 _remove(cur); 212 _setheight(_root); 213 return true; 214 } 215 216 template <class type> 217 type avl<type>::_remove(np cur) { 218 np tmp; 219 type x; 220 if (_isleaf(cur)) { 221 x = cur->_data; 222 if (!_isroot(cur)) { 223 _setnull(_root, cur); 224 delete(cur); 225 cur = NULL; 226 } 227 return x; 228 } 229 else { 230 tmp = cur->left ? _max(cur->left) : _min(cur->right); 231 x = cur->_data; 232 cur->_data = _remove(tmp); 233 return x; 234 } 235 } 236 237 template <class type> 238 void avl<type>::insert(const type& x) { 239 if (empty()) { 240 np tmp = new nt(); 241 _root = tmp; 242 _root->_data = x; 243 _root->height = 0; 244 return; 245 } 246 _root = _insert(_root, x); 247 } 248 249 template <class type> 250 avlnode<type>* avl<type>::_insert(np cur, const type& x) { 251 if (x > cur->_data) { 252 if (cur->right == NULL) { 253 np tmp = new nt(); 254 cur->right = tmp; 255 tmp->_data = x; 256 } 257 else { 258 cur->right = _insert(cur->right, x); 259 if (_height(cur->right) - _height(cur->left) == 2) { 260 if (x > cur->right->_data) { 261 cur = _right_rotate(cur); 262 } 263 else { 264 cur = _double_right_rotate(cur); 265 } 266 } 267 } 268 } 269 if (x < cur->_data) { 270 if (cur->left == NULL) { 271 np tmp = new nt(); 272 cur->left = tmp; 273 cur->height++; 274 tmp->_data = x; 275 } 276 else { 277 cur->left = _insert(cur->left, x); 278 if (_height(cur->left) - _height(cur->right) == 2) { 279 if (x < cur->left->_data) { 280 cur = _left_rotate(cur); 281 } 282 else { 283 cur = _double_left_rotate(cur); 284 } 285 } 286 } 287 } 288 cur->height = ( 289 _height(cur->left) > _height(cur->right) ? 290 _height(cur->left) : _height(cur->right) 291 ) + 1; 292 return cur; 293 if (cur == NULL) { 294 np tmp = new nt(); 295 tmp->_data = x; 296 cur = tmp; 297 cur->left = NULL; 298 cur->right = NULL; 299 } 300 else if (x < cur->_data) { 301 cur->left = _insert(cur->left, x); 302 if (_height(cur->left) - _height(cur->right) == 2) { 303 if (x < cur->left->_data) { 304 cur = _left_rotate(cur); 305 } 306 else { 307 cur = _double_left_rotate(cur); 308 } 309 } 310 } 311 else if (x > cur->_data) { 312 cur->right = _insert(cur->right, x); 313 if (_height(cur->right) - _height(cur->left) == 2) { 314 if (x > cur->right->_data) { 315 cur = _right_rotate(cur); 316 } 317 else { 318 cur = _double_right_rotate(cur); 319 } 320 } 321 } 322 323 cur->height = ( 324 _height(cur->left) > _height(cur->right) ? 325 _height(cur->left) : _height(cur->right) 326 ) + 1; 327 return cur; 328 } 329 330 template <class type> 331 void avl<type>::_preorder(np cur, void(*visit)(type data)) { 332 if (cur != NULL) { 333 (*visit)(cur->_data); 334 _preorder(cur->left, visit); 335 _preorder(cur->right, visit); 336 } 337 } 338 339 template <class type> 340 void avl<type>::_inorder(np cur, void(*visit)(type data)) { 341 if (cur != NULL) { 342 _inorder(cur->left, visit); 343 (*visit)(cur->_data); 344 _inorder(cur->right, visit); 345 } 346 } 347 348 template <class type> 349 void avl<type>::_postorder(np cur, void(*visit)(type data)) { 350 if (cur != NULL) { 351 _postorder(cur->left, visit); 352 _postorder(cur->right, visit); 353 (*visit)(cur->_data); 354 } 355 } 356 } 357 358 #endif
1 int pre[maxn]; 2 int N, d; 3 4 int find(int x) { 5 return x == pre[x] ? x : pre[x] = find(pre[x]); 6 } 7 8 void unite(int x, int y) { 9 x = find(x); 10 y = find(y); 11 if(x != y) { 12 pre[y] = x; 13 } 14 } 15 inline void init() { 16 for(int i = 0; i < maxn; i++) { 17 pre[i] = i; 18 } 19 }
1 #ifndef _KIRAI_PRIORITY_QUEUE 2 #define _KIRAI_PRIORITY_QUEUE 3 4 namespace kirai { 5 template<class type> 6 class priority_queue { 7 public: 8 priority_queue(); 9 ~priority_queue(); 10 11 public: 12 int size() const; 13 void set_heap_size(int x); 14 void clear(); 15 bool push(type); 16 type top() const; 17 bool pop(); 18 bool empty(); 19 20 private: 21 const int max_size = 100010; 22 static int heap_size; 23 int _size; 24 type *_data; 25 }; 26 27 template<class type> 28 int priority_queue<type>::heap_size = 10000; 29 30 template<class type> 31 priority_queue<type>::priority_queue() { 32 _size = 0; 33 _data = new type[heap_size]; 34 memset(_data, 0, sizeof(_data)); 35 _data[0] = -0x7f7f7f7f; 36 } 37 38 template<class type> 39 priority_queue<type>::~priority_queue() { 40 delete _data; 41 _data = NULL; 42 } 43 44 template<class type> 45 int priority_queue<type>::size() const { return _size; } 46 47 template<class type> 48 void priority_queue<type>::clear() { 49 _size = 0; 50 memset(_data, 0, sizeof(_data)); 51 } 52 53 template<class type> 54 bool priority_queue<type>::empty() { return _size == 0; } 55 56 template<class type> 57 bool priority_queue<type>::push(type x) { 58 if (_size > heap_size) { 59 return false; 60 } 61 int pos = ++_size; 62 for (; _data[pos >> 1] > x; pos >>= 1) { 63 _data[pos] = _data[pos >> 1]; 64 } 65 _data[pos] = x; 66 return true; 67 68 } 69 70 template<class type> 71 type priority_queue<type>::top() const { 72 return !_size ? _data[0] : _data[1]; 73 } 74 75 template<class type> 76 bool priority_queue<type>::pop() { 77 if (_size == 0) { 78 return false; 79 } 80 int pos, child = 1; 81 type last = _data[_size--]; 82 for (pos = 1; (pos << 1) <= _size; pos=child) { 83 child = pos << 1; 84 if (child != _size && _data[child + 1] < _data[child]) { 85 ++child; 86 } 87 if (last > _data[child]) { 88 _data[pos] = _data[child]; 89 } 90 else { 91 break; 92 } 93 } 94 _data[pos] = last; 95 return true; 96 } 97 98 } 99 100 101 #endif
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 #define __KIRAI_DEBUG 21 #ifdef __KIRAI_DEBUG 22 #define pr(x) \ 23 cout << #x << " = " << x << endl 24 #endif 25 26 using namespace std; 27 28 29 namespace kirai_sort { 30 template <class _type, bool(*_cmp)(_type a, _type b)> 31 class bubble { 32 public: 33 bubble() = default; 34 ~bubble() = default; 35 static void sort(_type* _tmp, size_t _begin, size_t _end) { 36 for(size_t i = _begin; i != _end; i++) { 37 for(size_t j = i; j != _end; j++) { 38 if(_cmp(*(_tmp + j), *(_tmp + i))) { 39 _swap(*(_tmp + i), *(_tmp + j)); 40 } 41 } 42 } 43 } 44 protected: 45 static inline void _swap(_type& _x, _type& _y) { 46 _type _t = _x; 47 _x = _y; 48 _y = _t; 49 } 50 }; 51 52 template <class _type, bool(*_cmp)(_type a, _type b)> 53 class select : public bubble<_type, _cmp> { 54 public: 55 static void sort(_type* _tmp, size_t _begin, size_t _end) { 56 for(size_t i = _begin; i != _end; i++) { 57 size_t _t = i; 58 for(size_t j = i + 1; j != _end; j++) { 59 if(_cmp(*(_tmp + j), *(_tmp + _t))) { 60 _t = j; 61 } 62 } 63 bubble<_type, _cmp>::_swap(*(_tmp + i), *(_tmp + _t)); 64 } 65 } 66 }; 67 68 template<class _type, bool(*_cmp)(_type a, _type b)> 69 class insert { 70 public: 71 insert() = default; 72 ~insert() = default; 73 static void sort(_type* _tmp, size_t _begin, size_t _end) { 74 for(size_t i = _begin; i != _end; i++) { 75 size_t j; 76 _type _t = *(_tmp + i); 77 for(j = i; j > 0 && *(_tmp + j - 1) > _t; j--) { 78 *(_tmp + j) = *(_tmp + j - 1); 79 } 80 *(_tmp + j) = _t; 81 } 82 } 83 }; 84 85 template<class _type, bool(*_cmp)(_type a, _type b)> 86 class shell { 87 public: 88 shell() = default; 89 ~shell() = default; 90 static void sort(_type* _tmp, size_t _begin, size_t _end) { 91 size_t _size = _end - _begin; 92 for(size_t gap = _size >> 1; gap > 0; gap >>= 1) { 93 for(size_t i = _begin; i < _begin + gap; i++) { 94 for(size_t j = i + gap; j < _end; j += gap) { 95 if(_cmp(*(_tmp + j), *(_tmp + j - gap))) { 96 _type _t = *(_tmp + j); 97 size_t k = j - gap; 98 while(k >= 0 && *(_tmp + k) > _t) { 99 *(_tmp + k + gap) = *(_tmp + k); 100 k -= gap; 101 } 102 *(_tmp + k + gap) = _t; 103 } 104 } 105 } 106 } 107 } 108 }; 109 110 template<class _type, bool(*_cmp)(_type a, _type b)> 111 class merge { 112 public: 113 merge() = default; 114 ~merge() = default; 115 static void sort(_type* _tmp, size_t _begin, size_t _end) { 116 _merge(_tmp, _begin, _end, _begin, _end - 1); 117 } 118 private: 119 static void _merge(_type* _tmp, 120 size_t _begin, size_t _end, size_t p, size_t q) { 121 if(p < q) { 122 int m = (p + q) >> 1; 123 _merge(_tmp, _begin, _end, p, m); 124 _merge(_tmp, _begin, _end, m + 1, q); 125 _sort(_tmp, _begin, _end, p, m, q); 126 } 127 } 128 129 static void _sort(_type* _tmp, 130 size_t _begin, size_t _end, size_t p, size_t m, size_t q) { 131 static size_t _len = (_end - _begin) >> 1; 132 _type ll[_len], rr[_len]; 133 size_t n1 = m - p + 1; 134 size_t n2 = q - m; 135 size_t i = 0, j = 0; 136 for(size_t ii = 0; ii < n1; ii++) ll[ii] = _tmp[p+ii]; 137 for(size_t ii = 0; ii < n2; ii++) rr[ii] = _tmp[m+ii+1]; 138 while(i < n1 && j < n2) { 139 if(_cmp(ll[i], rr[j])) _tmp[p++] = ll[i++]; 140 else _tmp[p++] = rr[j++]; 141 } 142 while(i < n1) _tmp[p++] = ll[i++]; 143 while(j < n2) _tmp[p++] = rr[j++]; 144 } 145 }; 146 147 template<class _type, bool(*_cmp)(_type a, _type b)> 148 class heap { 149 public: 150 heap() = default; 151 ~heap() = default; 152 static void sort(_type* _tmp, size_t _begin, size_t _end) { 153 _type hp[_end-_begin+1]; 154 _build(hp, _tmp, _begin, _end); 155 _sort(hp, _tmp, _begin, _end); 156 } 157 private: 158 static void _build(_type* hp, _type* _tmp, size_t _begin, size_t _end) { 159 memset(hp, 0, sizeof(hp)); 160 hp[0] = (_type)(-0x7f7f7f); 161 size_t pos = 0; 162 for(size_t i = _begin; i != _end; i++) { 163 _push(*(_tmp + i), hp, pos); 164 } 165 } 166 static void _sort(_type* hp, _type* _tmp, size_t _begin, size_t _end) { 167 size_t pos = _end - _begin; 168 for(size_t i = _begin; i != _end; i++) { 169 *(_tmp + i) = _pop(hp, pos); 170 } 171 } 172 private: 173 static void _push(_type _t ,_type* hp, size_t& pos) { 174 size_t i = ++pos; 175 for( ;_cmp(_t, hp[i>>1]); i >>= 1) { 176 hp[i] = hp[i>>1]; 177 } 178 hp[i] = _t; 179 } 180 181 static _type _pop(_type* hp, size_t& pos) { 182 size_t child = 1; 183 size_t i = 1; 184 _type ans = hp[1]; 185 _type last = hp[pos--]; 186 for( ; (i << 1) <= pos; i = child) { 187 child = i << 1; 188 if(child != pos && hp[child] > hp[child+1]) { 189 ++child; 190 } 191 if(last > hp[child]) { 192 hp[i] = hp[child]; 193 } 194 else { 195 break; 196 } 197 } 198 hp[i] = last; 199 return ans; 200 } 201 }; 202 203 template<class _type, bool(*_cmp)(_type a, _type b)> 204 class quick : public bubble<_type, _cmp> { 205 public: 206 static void sort( _type* _tmp, size_t _begin, size_t _end) { 207 _sort(_tmp, _begin, _end); 208 } 209 private: 210 static void _sort(_type* _tmp, size_t ll, size_t rr) { 211 if(ll >= rr) { 212 return; 213 } 214 size_t piv = _part(_tmp, ll, rr - 1); 215 _sort(_tmp, ll, piv - 1); 216 _sort(_tmp, piv + 1, rr); 217 218 } 219 static size_t _part(_type* _tmp, size_t ll, size_t rr) { 220 size_t _x = *(_tmp + rr); 221 size_t _y = ll - 1; 222 for(size_t i = ll; i < rr; i++) { 223 if(*(_tmp + i) <= _x) { 224 _y++; 225 bubble<_type, _cmp>::_swap(*(_tmp + _y), *(_tmp + i)); 226 } 227 } 228 bubble<_type, _cmp>::_swap(*(_tmp + _y + 1), *(_tmp + rr)); 229 return _y + 1; 230 } 231 232 inline static size_t _random_piv(size_t _size) { 233 //add your random function here. 234 } 235 }; 236 } 237 238 239 bool cmp(int x, int y) { 240 return x < y; 241 } 242 243 int main() { 244 int arr[8] = {4,1,6,8,6,51,213,1}; 245 kirai_sort::heap<int, cmp>::sort(arr, 0, 8); 246 for(int i = 0; i < 8; i++) { 247 printf("%d ", arr[i]); 248 } 249 return EXIT_SUCCESS; 250 }