Binary search tree (to be continued)

Source: Internet
Author: User

Stick to pure code first, and then pits

Template<class k, class v>struct bstnode{ bstnode (Const K &key,  const v &value)   :_key (key)   , _value (value)   , _ Left (NULL)   , _right (NULL)  {} k _key; v _value; bstnode<k,  V> *_left; BSTNode<K, V> *_right;}; Template<class k, class v>class bstree{ typedef bstnode<k, v>  node;public: bstree ()   :_root (NULL)  {} ~bstree ()  {  _destory (_ root);  } bstree (Const bstree<k, v> &tree)  {  _root =  _creathstree (_root, tree._root);  } bstree operator= (const bstree<k, v > &tree)  {  _destory (_root);   _root = _creathstree (_root,  Tree._root);   return *this; }public: bool insert (Const k&key, const v&value)  {  if  (_root ==  NULL)   {   _root = new node (key, value);    return  true;  }  Node *parent = NULL;  Node *cur =  _root;  while  (cur)   {   if  (cur->_key < key)    {    parent = cur;    cur = cur- >_right;   }   else if  (Cur->_key > key)     {    parent = cur;    cur = cur->_ left;   }   else   {    return false;    }  }  if  (Key > parent->_key)   {    parent->_rigHt = new node (Key, value);  }  else  {    Parent->_left = new node (key, value);   }  return true; }  bool insert_r (Const k &key, const v &value)  {   Return _insert_r (_root, key, value);  } node *find_r (Const K &key)  {  return _find_r (_root, key);  } node *find (const K & Key)  {  Node *cur = _root;  while  (Cur != null)    {   if  (Key == cur->_key)    {     return cur;   }   else if  (Key < cur->_key)     {    cur = cur->_left;   }   else  if  (key >&Nbsp;cur->_key)    {    cur = cur->_right;    }  }  return null; } /*  Delete to consider two cases:   1, leaf node (left  and   right  are  null)   2, non-leaf nodes (left  and  right  have a not  null)   3, non-leaf nodes ( left  and  right are not  null)  */ bool remove (Const k &key)  {   node *prev = null;  node *cur = _root;  //find the current node and its parent.   while  (cur)   {   if  (key == cur->_key)     {    break;   }   else if  (key <  cur->_key)    {    prev = cur;    cur  = cur->_left;   }   else if  (key > cur-> _key) &NBSP;&NBSP;&NBsp {    prev = cur;    cur = cur->_right;    }  }  //Case 1 (leaf knot)   if  (cur->_left == null  && cur->_right == null)   {   if  (prev !=  NULL)   //is not just one node left:   {    if  (prev->_left ==  cur)     {     prev->_left = NULL;     }    else    {     prev->_ right = null;    }   }   delete cur;   }  //Situation 2 (left  or  right  one not  null)   else if  (cur->_ Right == null)   {   if  (prev == null)  //processing the root node     {    _root = cur->_left;   }   else   {     if  (prev->_left == cur)     {      prev->_left = cur->_left;    }    else     {     prev->_right = cur->_left;    }     delete cur;   }  }  else if  (cur- >_left == null)   {   if  (prev == null)  //processing of the root node    {    _root = cur->_right;   }    else   {    if  (prev->_left == cur)      {     prev->_left = cur->_right;    }     else    {     prev->_right = cur->_right;     }   }   delete cur;  }  //Condition 3 (left  or  riht  are not  null)   else  {   Node *prev = cur;    Node *FirstLeft = cur->_right;   while  (firstleft-> _left)    {    prev = firstleft;    firstleft  = FirstLeft->_left;   }   Node *del = FirstLeft;    cur->_key = del->_key;   cur->_value = del->_ value;   if  (Prev->_left == firstleft)    {     prev->_left = FirstLeft->_right;   }   else    {    prev->_right = firstleft->_right;   }   delete del;   }  return true; } bool remove_r (Const k &key)  {   return _remove_r (_root, key);  } void inorder ()  {  InOrder_ R (_root);  }protected: node* _creathstree (node *cur, node *_cur)  {   if  (_cur == null)   {   return NULL;  }   else  {   cur = new node (_cur->_key, _cur->_value);   }  cur->_left = _creathstree (Cur->_left, _cur->_left);   cur->_right = _creathstree (cur->_right, _cur->_right);  return  Cur; } void _destory (Node *root)  {  if  (root == null)    {   returN;  }  _destory (Root->_left);   _destory (root->_right);   delete  root; } bool _remove_r (Node *&root, const k &key)  {   if  (Root == null)   {   return false;  }   if  (Root->_key > key)   {   return _remove_r ( Root->_left, key);  }  else if  (Root->_key < key)    {   return _remove_r (Root->_right, key);  }  else   //  Equal   {   Node *del = root;   if  ( Root->_left == null)    {    root = root->_right;    //here the root is quoted,                  //could be the last floor.root->_left  or  root->_right   }   else if  (root->_ Right == null)    {    root = root->_left;    }   else   {    node *firstleft =  root->_right;    while  (Firstleft->_left)     {      FirstLeft = FirstLeft->_left;    }     std::swap (Root->_key, firstleft->_key);     std::swap (Root->_value,  firstleft->_value);     return _remove_r (Root->_right, key);    }   delete del;  } } bool _insert_r (Node *& Root, const k &key, const v &value)  {  if  (root  == null)   {    root = new node (key, value);    return true;  }   else if  (Key < root->_key)   {   _insert_r (root- >_left, key, value);  }  else if  (Key > root->_key)   {   _insert_r (Root->_right, key, value);  }   Return false; } node * _find_r (Node *root, const k &key)  {  if  (root == null)   {   return NULL;   }  if  (Key == root->_key)   {   return root;   }  if  (Key < root->_key)   {   return  _find_r (Root->_left, key);  }  else if  (key > root->_key ) &NBSP;&NBSP;{&NBSP;&NBSP;&NBSp;return _find_r (Root->_right, key);   } } void inorder_r (Node * Root)  {  if  (root == null)   {   return;  }   inorder_r (root->_left);  cout <<  "key: "  <<  root->_key <<  ", value: "  << root->_value <<  Endl;  inorder_r (root->_right); }protected: node *_root;};

Binary search tree (to be continued)

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.