two fork Find Tree (English:binarysearch tree), also known as the binary searching trees, ordered binary tree (English: Ordered binary tree), sort binary trees (English: sorted binary), Refers to an empty tree or a two-fork tree with the following properties:
The left subtree of any node is not empty, then the value of all nodes on the left subtree is less than the value of its root node;
The right subtree of any node is not empty, then the value of all nodes on the right subtree is greater than the value of its root node;
The left and right subtrees of any node are also two-fork search tree;
There are no nodes with key values equal.
#pragma oncetemplate<class K, class V>struct BSTreeNode{K _key; v _value; bstreenode<k, v>* _left; bstreenode<k, v>* _right; Bstreenode (Const k& key, const v& value): _key (Key), _value (value), _left (NULL), _right (NULL) {}};template<class k, class v>class bstree{typedef bstreenode<k, v> node;public:bstree (): _root (NULL) {}bool insert (const k& key, const v& value) {if (null == _root)//If the empty tree {_root = new node (key, value); return true;} node* parent = null; node* cur = _root;//determining the location of the insertion node while (cur) {if (key < cur->_key) {Parent = cur;cur = cur->_left;} else if (Key > cur->_key) {parent = cur;cur = cur->_right;} else//already exists key{return false;}} Insert Node if (Key > parent->_key) Parent->_right = new node (key, value); elseparent- >_left = new node (Key, value);} Insert recursive notation Bool insertr (const k& key, const v& value) {return _ INSERTR (_root, key, value);} BOOL&NBSP;_INSERTR (Node*& root, const k& key, const v& value) { if (null == root) {root = new node (key, value); return true;} if (Key > root->_key) return _insertr (root->_right, key, value); else if (Key < root->_key) return _insertr (Root->_left, key, value); Elsereturn false;} Node* find (Const k& key) {node* cur = _root;while (cur) {if (key > cur->_key) cur = cur->_right;else if (key < cur->_key ) Cur = cur->_left;elseReturn cur;} Return null;} Find recursive notation Node* findr (const k& key) {Return _findr (_root, key);} Node* _findr (Node* root, const k& key) {if (null == root) return NULL;if (Key > root->_key) Return _findr (Root->_right, key);else if (Key < root->_key) Return _findr (root->_left, key); elsereturn root;} Bool remove (Const k& key) {node* parent = null; node* cur = _root;//determine where to delete the node while (cur) {if (key > cur->_key) {Parent = cur;cur = cur->_right;} else if (Key < cur->_key) {parent = cur;cur = cur->_left;} Else{break;}} if (null == cur)//without the node {return false;} node* del;if (null == cur->_left)//delete node left child is empty {del = cur;//deleted node is root node if ( Null == parent){_root = _root->_right;} else{if (Cur == parent->_left) parent->_left = cur->_right;elseparent->_ Right = cur->_right;}} else if (null == cur->_right)//Delete node right child is empty {del = cur;if (NULL == parent) {_root = _root->_left;} else{if (Cur == parent->_left) parent->_left = cur->_right;elseparent->_ Right = cur->_left;}} else//Delete the left and right child of the node is not empty, find the leftmost node of the tree in place of the node to delete {parent = cur; node* leftmost = cur->_right;while (Leftmost->_left) {parent = leftmost; Leftmost = leftmost->_left;} Del = leftmost;cur->_key = leftmost->_key;cur->_value = leftmost->_ value;if (Leftmost == parent->_left) parent->_left = leftmost->_right; Elseparent->_right = leftmost->_right;} Return true;} Remove recursive notation Bool remover (cOnst k& key) {return _remover (_root, key);} Bool _remover (Node*& root, const k& key) {if (NULL == root) return false;if (Key > root->_key) {return _remover (root->_right, key);} else if (Key < root->_key) {return _remover (root->_left, key);} else{node* del = root;if (null == root->_left) {root = root->_ Right;} else if (null == root->_right) {root = root->_left;} else{node* leftmost = root->_right;while (Leftmost->_left) {leftmost = Leftmost->_left;} Swap (Root->_key, leftmost->_key); swap (root->_value, leftmost->_value); return _ Remover (root->_right, key);} Delete del;} Return true;} Middle sequence traversal recursive notation Void inorder () {_inorder (_root);} Void _inorder (Node* root) {if (null == root) Return;_inorder (Root->_left);cout<<root->_key<< " "; _inorder (root->_right);} protected:node* _root;}; Void test () {bstree<int, int> t;int a[] = {5, 3, 4, 1, 7, 8, 2, 6, 0, 9};for (size_t i = 0; i < sizeof (a)/sizeof (a[0]); ++i) {t.insertr (a[i], i);} Cout<<t.findr (8)->_key<<endl;cout<<t.findr (5)->_key<<endl;cout<<t.findr (9) ->_key<<endl;t.remover (8); T.remover (7); T.remover (9); T.remover (6); T.remover (5); T.remover (3); T.RemoveR ( 1); T.remover (4); t.remover (0); T.remover (2); T.inorder ();}
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/83/9F/wKiom1d4sPXxpt87AADKmXua-BM400.png "title=" Bstree.png "alt=" Wkiom1d4spxxpt87aadkmxua-bm400.png "/>
This article is from the "zgw285763054" blog, make sure to keep this source http://zgw285763054.blog.51cto.com/11591804/1795294
C + + implementation Search binary tree