Binary search Tree

Source: Internet
Author: User

    1. Binary search Tree

      Binary search tree is also called binary lookup tree, binary sort tree. It has the following properties:

      1> Each node has a key code to search for, and it's not the same

Key code of 2> left node is small key code of root node

The key code of the right node of 3> is greater than the key code of root node.

4> each subtree satisfies the binary search tree

With these properties, the Middle sequence traversal of the two-fork search tree is orderly.

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/84/97/wKioL1eVs2rxiV8iAAAZo1hBCus902.png-wh_500x0-wm_3 -wmp_4-s_30005305.png "title=" 1-100 Digui. PNG "alt=" Wkiol1evs2rxiv8iaaazo1hbcus902.png-wh_50 "/>

2. Construction of a two-fork search tree

Each node has a key value and value, and the key value is used to differentiate the node, and the key value for each node is different. Each node is

There are pointers to left and right.

Template<class K, Class v>struct Bstreenode{bstreenode (const k&key, const V&value): _left (NULL), _right ( NULL), _key (key), _value (value) {}bstreenode<k, v>* _left; Bstreenode<k, v>* _right; K _key; V _value;}; Template<class K, class V>class bstree{typedef bstreenode<k, v> node;public:bstree (): _root (NULL) {}~BSTree        () {if (_root) {clear ();} }protected:node* _root;};

3. Inserting a two-fork search tree

If the insertion is an empty tree, then the new root node comes out, then inserts the position according to the key value to determine the inserted bit

, each insertion of a value to be judged from the root node, to insert a key value is smaller than the root node key value, then go to the left tree, than the root nodes of the

If the key value is large, go to the right tree. returns true until the correct position is found for insertion. Returns False if the value of the root node is equal.

1> Non-recursive insertion

BOOL Insert (const k& key, const v&value)

{//Follow Zolliangugen right greater than root, each subtree satisfies

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 (Parent->_key > key)

{

Parent->_left = new Node (key, value);

}

Else

Parent->_right = new Node (key, value);

return true;

}

2> recursive insertion

//recursive insertion, note that the root parameter received by _INSERTR is a reference type,

BOOL Insertr (const K&key, const V&value)

{

return_insertr (_root, key, value);

}

Protected

BOOL _insertr (node* &root, const k&key, const V&value)

{

if (root = NULL)//At this point the root must be the correct location to insert the key value, because root is referenced by the

{

root = new Node (key, value);

return true;

}

if (Root->_key > key)

{

_INSERTR (Root->_left, key, value);

}

else if (Root->_key < key)

{

_INSERTR (Root->_right, key, value);

}


Else

{

return false;

}


}

4. Finding a two-fork search tree

The worst case finding of a binary tree is O (n), and the average time complexity is O (log2 (n)).

node* Find (const K&key)//Find the words to return the node {assert (_root); node* cur = _root;while (cur) {if (Cur->_key > key) {cur = cur->_left;} else if (Cur->_key < key) {cur = cur->_right;} Else{return cur;}} return NULL;}

5. Deletion of two-fork search tree

The deletion of binary search tree can be divided into three situations:

1> the left tree of the node to be deleted is empty, the right tree can be empty, the node may be the root node, or it may be another node.


Delete a normal left tree as an empty node. Let the node's father node point to the right node of the node and delete the node.

Delete the 8 node of this tree

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/84/98/wKioL1eVtvqjNfsXAAA1mTAV2Bg616.png-wh_500x0-wm_3 -wmp_4-s_566171765.png "title=" 1-100 Digui. PNG "width=" height= "186" border= "0" hspace= "0" vspace= "0" style= "width:300px;height:186px;" alt= " Wkiol1evtvqjnfsxaaa1mtav2bg616.png-wh_50 "/>

2> the right tree of the node to be deleted is empty, the left tree can be empty and not empty, the node may be the root node, or it may be another node.

The deleted method is the same as the right node. This is not an example.

3> the node to be deleted is not empty around.

First find the leftmost node in the right tree of the node, then swap it, then delete the leftmost node. Because the left-most node of the right tree of the node is greater than

the nodes of the left tree of the node are large, smaller than the node in the right tree of the node, and satisfy the binary search tree, so it is exchanged with the deleted node .

 650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/84/98/wKioL1eVuluBGzoXAAA7wrZdQmk224.png-wh_ 500x0-wm_3-wmp_4-s_1955173314.png "title=" 1-100 Digui. PNG "alt=" wkiol1evulubgzoxaaa7wrzdqmk224.png-wh_50 "/>

1> non-recursive deletion bool remove (const k&key) {if  (_root == null) return false; node* cur = _root; node* parent = null;//find the node to be deleted while  (cur) {if  (cur->_key < key) {Parent  = cur;cur = cur->_right;} else if  (Cur->_key>key) {parent = cur;cur = cur->_left;} Else{break;}} if  (cur == null) {return false;} node* del;//1.cur->left==nullif  (cur->_left == null) {del = cur;//deleted is the root node if   (parent == null) {_root = cur->_right;} else{if  (parent->_left == cur) {parent->_left = cur->_right;} Else{parent->_right = cur->_right;}}} 2.cur->right=nullelse if  (Cur->_right == null) {del = cur;if  (parent  == null) {_root = cur->_left;} else{if  (parent->_left == cur) {parent->_left = cur->_left;                  }else{parent->_right = cur->_left;}}} else //is not empty {parent = cur;//Find the left-most node of the right tree to replace, so the binary search tree is still satisfied node* firstleftn = cur- >_right;while  (firstleftn->_left) {parent= firstleftn;firstleftn = firstleftn->_ Left;} del = firstleftn;cur->_key = firstleftn->_key;cur->_value = firstleftn- >_value;if  (parent->_left == firstleftn) {parent->_left = firstleftn->_ Right;} Else{parent->_right = firstleftn->_right;}} Delete del;return true;}  2> recursive deletion of Bool remover (Const k&key) {  return _remover (_root, key);} Bool _remover (Node*& root, const k&key) {if  (root == null) {return  false;} if  (root->_key < key) {_removER (Root->_right, key);} else if  (Root->_key>key) {_remover (root->_left, key);} Else{node *del = root;//1. The node to be deleted is left empty if  (root->_left == null) {root =  Root->_right;} Right is empty else if  (root->_right == null) {root = root->_left;} Left and right not empty else{node*firstleftn = root->_right;while  (firstleftn->_left) {firstLeftN =  firstleftn->_left;} Swap (Root->_key, firstleftn->_key); swap (Root->_value, firstleftn->_value);      return _remover (Root->_right,key);} Delete del;    return true;}}







This article is from the "Learning Journey" blog, please make sure to keep this source http://10541571.blog.51cto.com/10531571/1829668

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.