C + + Search binary tree/Sort binary tree

Source: Internet
Author: User

#pragma  once#include <iostream>using namespace std;template<class K,  class v>struct bstreenode{//two fork tree   node k _key; v _value; bstreenode* _left; bstreenode* _right; Bstreenode (Const k& key,const v& value): _key (Key),  _value (value),  _left ( NULL),  _right (null) {}};template<class k, class v>class bstree{public:typedef  BsTreeNode<K, V> Node; Bstree ():  _root (NULL) {}~bstree () {}bool insert (const k& key, const v&  value)//Insert   non-recursive {if  (_root == null) {_root = new node (key, value); Return true;} node* cur = _root;while  (1) {if  (Cur->_key > key) {if  (cur->_ left) Cur = cur->_left;else{cur->_left = new node (Key, value);return  true;}} else if  (Cur->_key < keY) {if  (cur->_right) Cur = cur->_right;else{cur->_right = new node (Key,  value); return true;}} Above for find   insert position   and insert else if  (Cur->_key == key)//Presence    Insert failed return  false;}} BOOL&NBSP;INSERTR (Const k& key,const v& value)//insert   Recursive {RETURN&NBSP;_INSERTR (_ Root,key,value);} Node* find (Const k& key) {//Find node * cur = _root;while  (cur) {if   (Cur->_key > key) cur = cur->_left;else if  (cur->_key <  key) Cur = cur->_right;elsereturn cur;} Return null;} Bool remover (Const k& key)//delete   recursive {if  (_root == null) return false; Return _remover (_root,key);} Bool remove (K key) {//delete   non-recursive node* cur=_root, *prev=null;while  (cur) {if  (cur- >_key > key) {PREV&NBSP;=&NBSP;CUR;CUR&NBSP;=&NBSP;CUR-&GT;_left;} else if  (Cur->_key < key) {prev = cur;cur = cur->_right;}   above   Find the   node to be deleted else{if  (cur->_left == null)//If the node is left or right empty, delete the section directly//points after the node   Connect to its parent node {if  (prev == null) {_root = cur->_right;} else if  (prev->_left == cur) {prev->_left = cur->_right;} Elseprev->_right = cur->_right;} else if  (Cur->_right == null)//ditto   right empty {if  (prev == null) {_root  = cur->_left;delete cur;} else if  (prev->_right == cur) {prev->_right = cur->_left;} Elseprev->_left = cur->_left;} else//if the left and right are not empty   find the left child leftmost   or   left child most right   node replace//generation Delete {Prev = cur;cur = cur->_right ;while  (cur->_left) {cur = cur->_left;} Prev->_key = cur->_key;prev->_value = cur->_value;prev->_right&nBsp;=cur->_right;} Delete cur;return true;}} Return false;} Bool modification (K key,v value) {//Modify Node* ret = find (Key);if  (ret ==  null) Return false;ret->_value = value;return true;} PRIVATE:BOOL&NBSP;_INSERTR (Node* & root,const k& key,const v& value) Insert   recursive function {if  (root == null) {root = new node (key,value); return true;} if  (Root->_key == key) return false;else if  (Root->_key > key) RETURN&NBSP;_INSERTR (Root->_left,key,value); Else return _insertr (Root->_right,key,value);} Bool _remover (Node* & root,const k& key)//delete   recursive function {if  (root->_key  < key) Return _remover (Root->_right, key);if  (Root->_key>key) return  _remover (Root->_left, key);if  (root->_left == null) {Node* dev = root;root = root->_right;delete dev;return true;} else if  (root->_right == null) {node* dev = root;root = root- >_left;delete dev;return true;} Else{root->_key = root->_right->_key;root->_value = root->_right->_value ; Return _remover (Root->_right,root->_key);}} node* _root;};


C + + Search binary tree/Sort binary tree

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.