{"id":837,"date":"2012-03-18T14:55:29","date_gmt":"2012-03-18T14:55:29","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=837"},"modified":"2012-03-18T14:55:29","modified_gmt":"2012-03-18T14:55:29","slug":"a-quick-and-dirty-bst-implementation","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2012\/03\/a-quick-and-dirty-bst-implementation\/","title":{"rendered":"A quick and dirty BST implementation"},"content":{"rendered":"<p>Another quick and dirty algorithm implementation in C++. This time a Binary search tree (no balancing):<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\r\n#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\n\r\nusing namespace std;\r\n\r\nclass Node {\r\n\r\npublic:\r\n\r\n  int value;\r\n\r\n  Node *less;\r\n  Node *more;\r\n};\r\n\r\nclass BinarySearchTree {\r\n\r\npublic:\r\n\r\n  BinarySearchTree() : root(NULL) {}\r\n\r\n  void find_node(int value,Node **findnode,Node **parent) {\r\n\r\n    *findnode = NULL;\r\n    *parent   = NULL;\r\n\r\n    if(root == NULL) {\r\n      return;\r\n    }\r\n\r\n    Node *current = root;\r\n    for(;;) {\r\n      if(current-&gt;value == value) {*findnode = current; return;}\r\n\r\n      if(value &lt; current-&gt;value) {\r\n        if(current-&gt;less != NULL) {*parent = current; current = current-&gt;less;}\r\n        else {\r\n          return;\r\n        }\r\n      } else {\r\n        if(current-&gt;more != NULL) {*parent = current; current = current-&gt;more;}\r\n        else {\r\n          return;\r\n        }\r\n      }\r\n    }\r\n  }\r\n \r\n  Node *getr(Node *c,Node **parent) {\r\n    *parent = c;\r\n    for(;c-&gt;more != NULL;) {\r\n      *parent = c;\r\n      c = c-&gt;more;\r\n    }\r\n    return c;\r\n  }\r\n\r\n  void remove(int value) {\r\n  \r\n    Node *delnode;\r\n    Node *parent;\r\n\r\n    find_node(value,&amp;delnode,&amp;parent);\r\n\r\n    if(delnode == NULL) return;\r\n\r\n    \/\/ leaf\r\n    if((delnode-&gt;less == NULL) &amp;&amp; (delnode-&gt;more == NULL)) {\r\n      if(parent-&gt;less == delnode) {parent-&gt;less = NULL;}\r\n      if(parent-&gt;more == delnode) {parent-&gt;more = NULL;}\r\n      delete delnode;\r\n      return;\r\n    }\r\n\r\n    \/\/ only one child\r\n    if((delnode-&gt;less == NULL) || (delnode-&gt;more == NULL)) {\r\n      if(delnode-&gt;less != NULL) {\r\n        if(parent-&gt;less == delnode) {parent-&gt;less = delnode-&gt;less;}\r\n        if(parent-&gt;more == delnode) {parent-&gt;more = delnode-&gt;less;}\r\n        delete delnode; \r\n        return;\r\n      }\r\n\r\n      if(delnode-&gt;more != NULL) {\r\n        if(parent-&gt;less == delnode) {parent-&gt;less = delnode-&gt;more;}\r\n        if(parent-&gt;more == delnode) {parent-&gt;more = delnode-&gt;more;}\r\n        delete delnode;\r\n        return;\r\n      }\r\n    }\r\n\r\n    \/\/ two children\r\n    Node *i = getr(delnode-&gt;less,&amp;parent);\r\n\r\n    delnode-&gt;value = i-&gt;value;\r\n    if(parent == i)       { delnode-&gt;less = NULL;}\r\n    if(parent-&gt;less == i) { parent-&gt;less = NULL; }\r\n                     else { parent-&gt;more = NULL; }\r\n    delete i;\r\n  }\r\n\r\n  void insert(int value) {\r\n\r\n    if(root == NULL) {\r\n      Node *n = new Node();\r\n      n-&gt;value = value;\r\n      n-&gt;less = NULL;\r\n      n-&gt;more = NULL;\r\n      root = n;\r\n\r\n      return;\r\n    }\r\n\r\n    Node *current = root;\r\n    for(;;) {\r\n      if(value &lt; current-&gt;value) {\r\n        if(current-&gt;less != NULL) current = current-&gt;less;\r\n        else {\r\n          Node *n = new Node();\r\n          n-&gt;value = value;\r\n          n-&gt;less = NULL;\r\n          n-&gt;more = NULL;\r\n          current-&gt;less = n;\r\n          break;\r\n        }\r\n      } else {\r\n        if(current-&gt;more != NULL) current = current-&gt;more;\r\n        else {\r\n          Node *n = new Node();\r\n          n-&gt;value = value;\r\n          n-&gt;less = NULL;\r\n          n-&gt;more = NULL;\r\n          current-&gt;more = n;\r\n          break;\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  bool exists(int value) {\r\n    if(root == NULL) {\r\n      return false;\r\n    }\r\n\r\n    Node *current = root;\r\n    for(;;) {\r\n      if(current-&gt;value == value) return true;\r\n\r\n      if(value &lt; current-&gt;value) {\r\n        if(current-&gt;less != NULL) current = current-&gt;less;\r\n        else {\r\n          return false;\r\n        }\r\n      } else {\r\n        if(current-&gt;more != NULL) current = current-&gt;more;\r\n        else {\r\n          return false;\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  Node *root;\r\n};\r\n\r\nint main() {\r\n\r\n  BinarySearchTree s;\r\n\r\n  vector&lt;int&gt; testdata;\r\n\r\n  for(size_t n=0;n&lt;10;n++) { testdata.push_back(rand()%20); }\r\n\r\n  for(size_t n=0;n&lt;10;n++) { s.insert(testdata&#x5B;n]); }\r\n\r\n  cout &lt;&lt; &quot;testdata: &quot; &lt;&lt; endl;\r\n  for(size_t n=0;n&lt;10;n++) { cout &lt;&lt; testdata&#x5B;n] &lt;&lt; endl; }\r\n\r\n  cout &lt;&lt; &quot;existance check: &quot; &lt;&lt; endl;\r\n  for(size_t n=0;n&lt;10;n++) { if(s.exists(testdata&#x5B;n])) cout &lt;&lt; &quot;existance: &quot; &lt;&lt; testdata&#x5B;n] &lt;&lt; &quot; true&quot; &lt;&lt; endl; else cout &lt;&lt; &quot;existance: &quot; &lt;&lt; testdata&#x5B;n] &lt;&lt; &quot; false&quot; &lt;&lt; endl;}\r\n  for(size_t n=0;n&lt;10;n++) { if(s.exists(testdata&#x5B;n]+20)) cout &lt;&lt; &quot;existance: &quot; &lt;&lt; testdata&#x5B;n]+20 &lt;&lt; &quot; true&quot; &lt;&lt; endl; else cout &lt;&lt; &quot;existance: &quot; &lt;&lt; testdata&#x5B;n]+20 &lt;&lt; &quot; false&quot; &lt;&lt; endl;}\r\n\r\n  cout &lt;&lt; &quot;remove: &quot; &lt;&lt; testdata&#x5B;5] &lt;&lt; endl;\r\n  s.remove(testdata&#x5B;5]);\r\n  if(s.exists(testdata&#x5B;5])) cout &lt;&lt; testdata&#x5B;5] &lt;&lt; &quot; still exists&quot; &lt;&lt; endl; else cout &lt;&lt; testdata&#x5B;5] &lt;&lt; &quot; no longer exists&quot; &lt;&lt; endl;\r\n\r\n  cout &lt;&lt; &quot;remove: &quot; &lt;&lt; testdata&#x5B;3] &lt;&lt; endl;\r\n  s.remove(testdata&#x5B;3]);\r\n  if(s.exists(testdata&#x5B;3])) cout &lt;&lt; testdata&#x5B;3] &lt;&lt; &quot; still exists&quot; &lt;&lt; endl; else cout &lt;&lt; testdata&#x5B;3] &lt;&lt; &quot; no longer exists&quot; &lt;&lt; endl;\r\n\r\n  cout &lt;&lt; &quot;remove: &quot; &lt;&lt; testdata&#x5B;0] &lt;&lt; endl;\r\n  s.remove(testdata&#x5B;0]);\r\n  if(s.exists(testdata&#x5B;0])) cout &lt;&lt; testdata&#x5B;0] &lt;&lt; &quot; still exists&quot; &lt;&lt; endl; else cout &lt;&lt; testdata&#x5B;0] &lt;&lt; &quot; no longer exists&quot; &lt;&lt; endl;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Another quick and dirty algorithm implementation in C++. This time a Binary search tree (no balancing): #include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; class Node { public: int value; Node *less; Node *more; }; class BinarySearchTree { public: BinarySearchTree() : root(NULL) {} void find_node(int value,Node **findnode,Node **parent) { *findnode = NULL; *parent = NULL; if(root [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-837","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-dv","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=837"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/837\/revisions"}],"predecessor-version":[{"id":838,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/837\/revisions\/838"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}