I. Two the nature of the search tree:
Each node has a key code that is used as the basis for the search, and the key codes for all nodes are different.
The key code (key) for all nodes on the left dial hand tree is small for the root node's key code (key).
The key code (key) for all nodes on the right subtree is greater than the root node's key code (key).
The left and right sub-trees are two-fork search trees.
Two. Consider the example
int a [] = {5,3,4,1,7,8,2,6,0,9};
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/84/3E/wKiom1eJsnbz6BZUAABTgFepaIM539.png "title=" 222. PNG "alt=" Wkiom1ejsnbz6bzuaabtgfepaim539.png "/>
Three. Code implementation
#include <iostream>using namespace std;template<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) {}~bstree () {}bool insert (const k& key, const v& value) {node* cur = _root; node* parent = cur;if (_root == null) {_root = new node (key, value); return true;} while (cur) {if (key > cur->_key) {parent = cur;cur = cur->_ Right;} else if (Key < cur->_key) {parent = cur;cur = cur->_left;} ELsereturn false;} if (Key > parent->_key) {parent->_right = new node (key, value);} Else{parent->_left = new node (Key, value);} Return true;} Node* find (Const k& key) {if (_root == null) return null; node* cur = _root;while (cur) {if (key > cur->_key) cur = cur->_right;else if (Key < cur->_key) Cur = cur->_left;elsereturn cur; }return null;} Bool remove (Const k& key) {if (_root == null) return false; node* cur = _root; node* parent = null; node* del = null;//findwhile (cur) {if (key > cur->_key) {parent = cur;cur = cur->_right;} else if (Key < cur->_key) {parent = cur;cur = cur->_left;} deleteelse{node* d el;//The left subtree of the node to be deleted is empty if (cur->_left == null) {del = cur;//rootif (parent == null) _root = cur->_right;else{if (parent->_left == cur) parent->_left = cur->_right;elseparent->_right = cur->_right;}} The right subtree of the node to be deleted is an empty else if (cur->_right == null) {del = cur;if (parent == null) _root = cur->_left;else{if (parent->_left = cur) parent->_left = cur->_left;elseparent->_right = cur->_left;} Return true;} The left and right subtrees of the node to be deleted are not empty else{parent = cur;//find the leftmost node of the right subtree or find the Zuozi node node* tmp = cur->_right;// TMP cannot be referenced while (tmp->_left) {parent = tmp;tmp = tmp->_left;} cur->_key = tmp->_key;cur->_value = tmp->_value;del = tmp;// Connect the TMP subtree to the parent of TMP if (parent->_left == tmp) parent->_left = Tmp->_right;elseparent->_right = tmp->_right;} Delete del;return true;}} Return false;} Void inorder () {_inorder (_root); Cout << endl;} Void _inorder (Node*& root) {if (root == null) Return;_inorder (root->_left); cout << root->_key << " "; _inorder (root->_right);} protected:node* _root;}; Int main () {int a[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 }; bstree<int, int> bst;for (Int i = 0; i < sizeof (a) / sizeof (A[0]); i++) {BST. Insert (a[i], i);} Bst. Inorder ();//bstreenode<int,int>* node = bst. Find (5); //cout << node->_key << endl;//bst. Find (+); Cout << bst. Remove (&NBSP;<<&NBSP;ENDL;BST). Inorder (); Cout << bst. Remove (5) <<&Nbsp;endl;bst. Inorder (); BST. Remove (7); BST. Inorder (); BST. Remove (8); BST. Inorder (); BST. Remove (9); BST. Inorder (); BST. Remove (0); BST. Remove (1); BST. Remove (2); BST. Remove (3); BST. Remove (4); BST. Remove (5); BST. Remove (6); BST. Remove (7); BST. Remove (8); BST. Remove (9); BST. Inorder (); System ("pause"); return 0;}
Four. Two-fork search tree defects
Can become a single-linked list, and lose the ability to exclude half of nodes each time it searches. The solution is to get the tree balanced.
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1826944
Binary search Tree