"Binary tree" binary search tree

Source: Internet
Author: User
Tags delete key

Binary search tree:

1. Each node has a key code (key) as a search basis, the key code is different.

2. All key codes of the left subtree are smaller than the key code of the root node.

3. All key codes of the right subtree are greater than the key code of the root node.

4. The left and right sub-trees are two-fork search trees.


Delete key: Left is empty, right is empty, neither left is empty

1) left empty: cur right tree chain to parent node

2) Right blank: cur left tree chain to parent node

3) Not empty: Find the right tree leftmost node or left tree the right node, will find the node and cur exchange after the deletion of it.


Binary search tree increment, delete, check (non-recursive and recursive) program code is as follows:

#pragma  once#include<string>////Note: Consider the boundary problem _root , the left and right sub-tree problem (non-recursive, recursive), remember to sentence empty processing template<class k, class v>struct bstnode{bstnode<k, v>* _left;   //left dial hand number BSTNode<K,  v>* _right;  //Right sub-tree k _key; v _value;//Constructor Bstnode (const k& key, const v& value): _left (NULL),  _ Right (NULL),  _key (key),  _value (value) {}};template<class k, class v>class  Bstree{typedef bstnode<k, v> node;public:bstree (): _root (NULL) {}public://bool insert ( Const k& key, const v& value);//bool remove (Const K& key) ;//node* find (Const k& key);//bool insert_r (const k& key, const  v& value);//bool remove_r (Const k& key, const v& value); /node* find_r (Const k& key); Bool insert (Const&nbsP K& key, const v& value) {if (_root == null) _root = new node (Key, value); Node* cur = _root;while (cur) {if (Key < cur->_key)    //key small, insert left subtree { if (cur->_left == null) Cur->_left = new node (key, value); elsecur =  cur->_left;} Else if (Key > cur->_key)    //key Large, insert right sub-tree {if (cur->_right ==  NULL) Cur->_right = new node (key, value); elsecur = cur->_right;} else                        //key key codes differ return false;} Return true;} Delete//////////////////boundary condition judgment (no node, one node)////find the key value and record the parent. Delete key, in three cases (left is empty, right is empty, left is not empty)////right and left nodes are present, find the leftmost node of the tree or the right node of the left tree,////will find the node and cur exchange and then delete the found node Bool remove (const  k& key)  {//no node if (_root == NULL) return false;//a node  _rootif (_root->_left == null&&_root->_right ==  null) {if (Key == _root->_key) {delete _root;_root = null;return true;} Elsereturn false;} node* cur = _root; Node* parent = null;while (cur) {   //Find key value, record Parentif (key < cur-> _key)   {parent = cur;cur = cur->_left;} Else if (Key > cur->_key) {parent = cur;cur = cur->_right;} Delete key, in three cases (left empty, right empty, left or no empty) else  {node*del=cur;if (cur->_left == null)     //link the right subtree of the cur to the parent node {//Note that the parent may be empty if (Parent == null)   {_root = cur->_right;} Else{if (Cur = parent->_left) parent->_left = cur->_right;elseparent->_right  = cur->_right;}} Else if (cur->_right == null)   //links the left subtree of the cur to the parent node {if (parent == null) {_root = cur->_left;} Else{if (Cur = parent->_left) parent->_left = cur->_left;elseparent->_right  = cur->_left;}} Both left and right nodes exist, find the left-most node of the tree, or the right-most node of the left tree////will find the node with cur swap and then delete the found node else{parent = cur;//the leftmost node of the right tree firstleftnode*  firstleft = cur->_right;while (firstleft->_left) {Parent = firstleft;firstleft  = firstleft->_left;} Swap (Firstleft->_key, cur->_key), swap (firstleft->_value, cur->_value);d el =  firstleft;//because the left and right nodes are present, so the parent is not empty//firstleft , and there may be an if (firstleft == parent->_left) node Parent->_left = firstleft->_right;elseparent->_right = firstleft->_right;}  /* find firstleft instead of Del*/delete del;return true;}} While endreturn false;} Find Node* find (Const k& key) {if (_root == null) return null; Node* cur = _root;while (cur) {if (KEY&NBSP;&LT;&NBSp;cur->_key)    //Find in left subtree     {cur = cur->_left;     }else if (Key>cur->_key)   //in the right subtree find {cur = cur->_right;} Else{return cur;}} Return null;} Recursive implementation additions and deletions check Bool insert_r (const k& key, const v& value) { Return _insert_r (_root, key, value);} Bool remove_r (Const k& key) {return _remove_r (_root, key);} Node* find_r (Const k& key) {return _find_r (_root, key);} Middle order Traversal Void inorder () {return _inorder (_root);} Bool empty () {if (_root == null) {return true;} Return false;} Protected:void _inorder (node* root) {if (root == null) Return;_inorder (root->_left);//cout  << root->_key <<  " "  << endl;cout <<  "Key:"  << root->_key <<  " ,value: " << root->_value<< endl;_inorder (Root->_right);} Note: The root parameter should be a reference &   each recursive root is the last Root->_left or Root->_rightbool _insert_r (node*&  root, const k& key, const v& value) {if (Root == NULL) { Root = new node (key, value); return true;} if (Key < root->_key) {return _insert_r (root->_left, key, value);} Else if (Key>root->_key) {return _insert_r (root->_right, key, value);} Else{return false;}} Find Node* _find_r (Node*& root, const k& key) {if (root == null) return  null;if (Key < root->_key) {return _find_r (root->_left, key);} Else if (Key>root->_key) {return _find_r (root->_right, key);} Else{return root;}} Delete Bool _remove_r (Node*& root, const k& key) {if (Root&nbsP;== null) return false;if (key < root->_key) {Return _remove_r (Root->_left,  key);} Else if (Key > root->_key) {return _remove_r (root->_right, key);} Delete key////to refer to root as a parameter, root equivalent to the previous layer of root->_left or root->_rightelse   {node*del =  Root;if (root->_left == null) {root = root->_right;delete del;return true;} Else if (root->_right == null) {root = root->_left;delete del;return  true;} Left and right nodes are not empty else{//leftmost node Node* firstleft = root->_right;while (firstleft->_left) { Firstleft = firstleft->_left;} Swap (Firstleft->_key, root->_key); swap (firstleft->_value, root->_value);//_Remove_R ( Firstleft, key);//error, Firstleft is a temporary variable cannot be a recursive parameter _remove_r (Root->_right, key);}} Delete Key endreturn false;} protected:node* _root;};

Test code:

Void testbstree () {Bstree<int, int> bst;bst. Insert (5, 1); BST. Insert (3, 1); BST. Insert (4, 1); BST. Insert (1, 1); BST. Insert (7, 1); BST. Insert (8, 1); BST. Insert (2, 1); BST. Insert (6, 1); BST. Insert (0, 1); BST. Insert (9, 1); BST. Inorder ();cout <<  "? "  << bst. Find (1)->_key<< endl;cout <<  "? "  << bst. Find (8)->_key<< endl;cout <<  "? "  << bst. Find (&LT;&LT;&NBSP;ENDL;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. Remove (0), if (BST. Empty ()) cout <<  "This binary search tree is null" &NBSP;&LT;&LT;&NBSP;ENDL;BST. Inorder ();} Void testbstree_r ()   //recursive {bstree<int, int> bst;bst. Insert_r (5, 1); BST. Insert_r (3, 1); BST. Insert_r (4, 1); BST. Insert_r (1, 1); BST. Insert_r (7, 1); BST. Insert_R (8, 1); BST. Insert_r (2, 1); BST. Insert_r (6, 1); BST. Insert_r (0, 1); BST. Insert_r (9, 1); BST. Inorder ();cout <<  "? "  << bst. Find (1)->_key << endl;cout <<  "? "  << bst. Find (8)->_key << endl;cout <<  "? "  << bst. Find (&NBSP;&LT;&LT;&NBSP;ENDL;/*BST). Remove_r (1); BST. Remove_r (2); BST. Remove_r (3); BST. Remove_r (4); */bst. Remove_r (5); BST. Remove_r (6); BST. Remove_r (7); BST. Remove_r (8); BST. Remove_r (9); BST. Remove_r (0); if (BST. Empty ()) cout <<  "This binary search tree is null" &NBSP;&LT;&LT;&NBSP;ENDL;BST. Inorder ();} Void testbstree_string ()   // int string{bstree<int, string> bst;bst. Insert (5,  "Zhou"), BST. Insert (3,  "Sun"), BST. Insert (4,  "Li"), BST. Insert (1,  "Zhao"), BST. Insert (7,  "Zheng"), BST. Insert (8,  "Wang"), BST. Insert (2,  "Qian"), BST. Insert (6,  "WU"), BST. Insert (0,  "bAijiaxing "); BST. Insert (9,  "Feng"), BST. Inorder ();cout <<  "? "  << bst. Find (1)->_key << endl;cout <<  "? "  << bst. Find (8)->_key << endl;cout <<  "? "  << bst. Find (&NBSP;&LT;&LT;&NBSP;ENDL;//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), if (BST. Empty ()) cout <<  "This binary search tree is null" &NBSP;&LT;&LT;&NBSP;ENDL;BST. Inorder ();}


This article is from the "Na-dimensional Snow" blog, please be sure to keep this source http://1536262434.blog.51cto.com/10731069/1790713

"Binary tree" binary search 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.