Reinvent the wheel to implement a hash table.

Source: Internet
Author: User

First, I want to write it in a sink. But at the end of the article, the system was dizzy. No way. Use C ++ to verify your ideas. The Code is as follows.

 

  1. Chashmap: chashmap ()
  2. {
  3. M_size = 100;
  4. M_count = 0;
  5. M_nodes = (pcnode) malloc (sizeof (cNode) * 100 );
  6. Zeromemory (m_nodes, sizeof (cNode) * 100 );
  7. }
  8. Chashmap ::~ Chashmap ()
  9. {
  10. For (INT I = 0; I <m_size; I ++)
  11. {
  12. Pcnode cur_node = m_nodes + I;
  13. If (cur_node-> left)
  14. _ Del_x (cur_node-> left );
  15. If (cur_node-> right)
  16. _ Del_x (cur_node-> right );
  17. }
  18. Free (m_nodes );
  19. }
  20. Bool chashmap: Set (DWORD key, DWORD Value)
  21. {
  22. If (m_count/m_size >=0.72)
  23. {
  24. // The size of the rebuilt hash table is double that of the original hash table.
  25. DWORD new_size = m_size * 2;
  26. Pcnode new_nodes = (pcnode) malloc (sizeof (cNode) * new_size );
  27. Zeromemory (new_nodes, sizeof (cNode) * new_size );
  28. For (INT I = 0; I <m_size; I ++)
  29. {
  30. Pcnode cur_node = m_nodes + I;
  31. If (cur_node-> key)
  32. {
  33. // Add the current node.
  34. Pcnode add_node = _ getaddress_x (new_nodes, new_size, cur_node-> key );
  35. If (add_node) add_node-> value = cur_node-> value;
  36. // Traverse the child node and delete the old one.
  37. If (cur_node-> right)
  38. _ Setanddel_x (new_nodes, new_size, cur_node-> right );
  39. If (cur_node-> left)
  40. _ Setanddel_x (new_nodes, new_size, cur_node-> left );
  41. }
  42. }
  43. // Delete the old one.
  44. Free (m_nodes );
  45. M_nodes = new_nodes;
  46. M_size = new_size;
  47. }
  48. Pcnode node;
  49. If (node = _ getaddress_x (m_nodes, m_size, key ))
  50. {
  51. Node-> value = value;
  52. M_count ++;
  53. Return true;
  54. }
  55. Return false;
  56. }
  57. DWORD chashmap: Get (DWORD key)
  58. {
  59. // Locate the data first. Compare the key value to find the node. Finally, return the required value.
  60. DWORD Pos = Key % m_size;
  61. Pcnode node = m_nodes + Pos;
  62. While (node ){
  63. If (Key> node-> key)
  64. Node = node-> right;
  65. Else if (Key <node-> key)
  66. Node = node-> left;
  67. Else
  68. Return node-> value;
  69. }
  70. Return NULL;
  71. }
  72. Bool chashmap: exists (DWORD key)
  73. {
  74. DWORD Pos = Key % m_size;
  75. Pcnode node = m_nodes + Pos;
  76. While (node ){
  77. If (Key> node-> key)
  78. Node = node-> right;
  79. Else if (Key <node-> key)
  80. Return true;
  81. Else
  82. Return node-> value;
  83. }
  84. Return false;
  85. }
  86. Bool chashmap: del (DWORD key)
  87. {
  88. DWORD Pos = Key % m_size;
  89. Pcnode par_node = NULL, cur_node, node = m_nodes + Pos;
  90. If (null = node-> key) return false;
  91. While (node ){
  92. If (Key> node-> key ){
  93. Par_node = node;
  94. Node = node-> right;
  95. } Else if (Key <node-> key ){
  96. Par_node = node;
  97. Node = node-> left;
  98. } Else {
  99. Cur_node = node;
  100. Break;
  101. }
  102. }
  103. If (! Node) return false; // no result is returned.
  104. If (! Par_node) // Root Node
  105. {
  106. If (cur_node-> right)
  107. {
  108. Pcnode left_node = cur_node-> left;
  109. * Cur_node = * cur_node-> right;
  110. If (left_node ){
  111. // Mount it to the leftmost.
  112. While (cur_node-> left ){
  113. Cur_node = cur_node-> left;
  114. }
  115. Cur_node-> left = left_node;
  116. }
  117. } Else if (cur_node-> left)
  118. {
  119. // If no right node exists.
  120. * Cur_node = * cur_node-> left;
  121. } Else {
  122. // Nothing
  123. Cur_node-> value = cur_node-> key = 0;
  124. Cur_node-> left = cur_node-> right = NULL;
  125. }
  126. Return true;
  127. }
  128. /* Release cur_node if it is not the root node */
  129. If (cur_node-> right ){
  130. Par_node-> right = cur_node-> right;
  131. // The left node is mounted to the far left of the right.
  132. If (cur_node-> left ){
  133. Node = cur_node;
  134. While (node-> left) {node = node-> left ;}
  135. Node-> left = cur_node-> left;
  136. }
  137. } Else if (cur_node-> left ){
  138. // The right side does not exist.
  139. Par_node-> left = cur_node-> left;
  140. } Else
  141. {
  142. If (par_node-> left = cur_node)
  143. Par_node-> left = NULL;
  144. Else
  145. Par_node-> right = NULL;
  146. }
  147. Free (cur_node );
  148. Return true;
  149. }
  150. // This is a static method. Copy a new node and delete it.
  151. Void chashmap: _ setanddel_x (pcnode arg_nodes, DWORD arg_size, pcnode arg_node)
  152. {
  153. Pcnode add_node = _ getaddress_x (arg_nodes, arg_size, arg_node-> key );
  154. If (add_node) add_node-> value = arg_node-> value;
  155. If (arg_node-> right)
  156. _ Setanddel_x (arg_nodes, arg_size, arg_node-> right );
  157. If (arg_node-> left)
  158. _ Setanddel_x (arg_nodes, arg_size, arg_node-> left );
  159. Free (arg_node );
  160. }
  161. Void chashmap: _ del_x (pcnode arg_node)
  162. {
  163. If (arg_node-> right)
  164. _ Del_x (arg_node );
  165. If (arg_node-> left)
  166. _ Del_x (arg_node );
  167. Free (arg_node );
  168. }
  169. /*
  170. If data with the same key value exists, null is returned.
  171. This method is static.
  172. If it exists, it will give me a space. If not. It allocates a new address.
  173. */
  174. Pcnode chashmap: _ getaddress_x (pcnode arg_node, DWORD arg_size, DWORD key)
  175. {
  176. DWORD Pos = Key % arg_size;
  177. Pcnode t_node, node = arg_node + Pos;
  178. If (node-> key = 0 ){
  179. Node-> key = key;
  180. Return node;
  181. }
  182. While (node)
  183. {
  184. T_node = node;
  185. If (Key> node-> key)
  186. Node = node-> right;
  187. Else if (Key = node-> key)
  188. Return NULL;
  189. Else
  190. Node = node-> left;
  191. }
  192. Node = (pcnode) malloc (sizeof (cNode ));
  193. Node-> key = key; node-> left = node-> right = NULL;
  194. If (Key> t_node-> key)
  195. T_node-> right = node;
  196. Else
  197. T_node-> left = node;
  198. Return node;
  199. }

Header file:

 

  1. Class chashmap
  2. {
  3. Public:
  4. Chashmap ();
  5. Bool set (DWORD key, DWORD Value); // sets data
  6. DWORD size () {return m_size;} // take the capacity size
  7. DWORD count () {return m_count;} // Number of Retrieved Data
  8. DWORD get (DWORD key); // retrieves data
  9. Bool del (DWORD key); // delete data
  10. Bool exists (DWORD key); // whether the data exists
  11. Virtual ~ Chashmap ();
  12. PRIVATE:
  13. Static pcnode _ getaddress_x (pcnode arg_node, DWORD arg_size, DWORD key );
  14. Static void _ setanddel_x (pcnode arg_nodes, DWORD arg_size, pcnode arg_node );
  15. Void _ del_x (pcnode arg_node );
  16. Protected:
  17. DWORD m_size;
  18. DWORD m_count;
  19. Pcnode m_nodes;
  20. };

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.