# "Data structure" search binary tree (recursive and non-recursive) implementation, including: Add Insert, remove, find

Source: Internet
Author: User

Search binary tree, is a special structure of two fork tree.

Characteristics:

(1) Each node has a key code, and the key code is not duplicated.

(2) The key code of each node on the left dial hand tree is smaller than the key code of the root node.

(3) The key code of each node on the right subtree is greater than the key code of the root node.

(4) The left and right sub-trees are search binary trees.

Below, for the convenience of everyone to understand, I have an example to draw a search binary tree, as follows:

Code See below:

`#define _crt_secure_no_warnings 1#include<iostream>using namespace std;//Search the node structure of the binary tree Template<class K,class V >struct searchbinarynode{searchbinarynode* _left;//left dial hand tree searchbinarynode* _right;//right subtree K _key;//each node is (_key,_value) Form V _value;//The constructor of the node that searches the binary tree searchbinarynode (const k& key, const v& value): _left (null), _right (null), _key (key), _value (value) {}};template<class k,class v>class searchbinarytree{typedef searchbinarynode<k, V> Node; public://Search Binary Tree Constructor searchbinarytree (): _root (NULL) {}//Inserts a node into the search binary tree bool Insert (const k& key, const v& value) {/ /Search binary tree without node, directly hang the node if (_root = = NULL) {_root = new node (key, value); return true;} Traverse the search binary tree, to insert a node smaller than each root node, go to the left subtree, otherwise large to the right subtree walk node* cur = _root; node* prev = null;//save node, easy to insert node chain on top while (cur) {if (Key < Cur->_key) {prev = Cur;cur = Cur->_left;} else if (key > Cur->_key) {prev = Cur;cur = Cur->_right;} Else{return false;//Search binary tree requires no duplicate key code}}//at this time do not know the chain in the Zuozi or right subtree, sub-condition if (Prev->_key > key) {prev->_left = new Node (Key, VALue);} else if (Prev->_key < key) {prev->_right = new Node (key, value);} return true;} Searching for a node of the binary tree bool Find (const k& key,const v& value) {if (_root = = NULL) {return false;} node* cur = _root;while (cur) {if (Key < Cur->_key) {cur = cur->_left;} else if (key > Cur->_key) {cur = cur->_right;} Else{return true;}} return true;} Delete a node of the search binary tree bool Remove (const k& Key,const v& value) {//No nodes: returns if (_root = = NULL) {return false;} One node: directly delete else if (_root->_left = = NULL && _root->_right = = null) {if (_root->_key = = key) {Delete _root;_r Oot = Null;return true;} Elsereturn false;} Multiple nodes else{node* cur = _root; node* parent = null;while (cur) {//go to the left subtree for if (Cur->_key > key) {parent = Cur;cur = Cur->_left;} Go to the right subtree and look for else if (Cur->_key < key) {parent = Cur;cur = Cur->_right;} Locate the node and determine how to remove the node else{//to delete the node without the left subtree if (cur->_left = = NULL) {///problem: This time to deal with a special case, if you delete only the root node parent:5, and its right subtree parent- >_right:9, the parent is empty,//accessing a member of the parent causes the program to crash//resolve: When the parent is empty, theParent->right root _root, remove the parent node if (parent = = NULL) {_root = Cur->_right;delete cur;cur = Null;return true;} Two cases://(1) Delete cur on the right subtree of parent, cur->_right chain on Parent->_right if (parent->_key<cur->_key) {parent- >_right = Cur->_right;} (2) Otherwise, cur->_right chain in Parent->_leftelse if (parent->_key>cur->_key) {parent->_left = Cur->_ Right;} Delete Cur;cur = Null;return true;}  The node to be removed has no right subtree else if (cur->_right = = null) {//Cur->_left is null if (parent = = NULL) {_root = Cur->_left;delete Cur;cur = Null;return true;} Two cases://(1) Delete cur on the right subtree of parent, cur->_left chain on Parent->_right if (Parent->_key < Cur->_key) {parent- >_right = Cur->_left;} (2) Otherwise, cur->_left chain in Parent->_leftelse if (Parent->_key > Cur->_key) {parent->_left = Cur->_left;} Delete release Delete cur;cur = Null;return true;} The left and right subtree are not empty, in two cases else{//firstleft is to remove the cur of the node of the tree (the code goes to this step, stating that the inevitable is not empty, then certainly not empty) node* Firstleft = cur->_right;// Go to Firstleft's left subtree, find the leftmost node//(1) left empty, swap, put the chain in the firstleft of a string of nodes (Only right subtree) chain on cur->_right if (firstleft->_left = = NULL) {swap (Cur->_key, firstleft->_key); Swap (Cur->_value , firstleft->_value); cur->_right = Firstleft->_right;delete Firstleft;firstleft = NULL;return true;} Left empty, go straight to its left subtree, find, swap else{node* tmp = firstleft;//tmp to the left node while (tmp->_left) {firstleft = Tmp;tmp = Tmp->_left;} Swap (Cur->_key, Tmp->_key), swap (Cur->_value, tmp->_value), or//link a string of nodes in TMP (only the right subtree) in Firstleft->_ Right on firstleft->_left = Tmp->_right;delete tmp;tmp = Null;return true;}}}}} Call _insert_rvoid insert_r (const k& key, const v& value) {_insert_r (_root, key, value);} Call _find_rnode* find_r (const k& key, const v& value) {node* ret = _find_r (_root, key, value); return ret;} Call _remove_rvoid remove_r (const k& key, const v& value) {_remove_r (_root,key, value);} Call _inordervoid inorder () {_inorder (_root); cout << Endl;} protected://in-sequence traversal printing: Easy to see if the search binary tree is constructed correctly void _inorder (node* root) {if (root = NULL) {return;} _inorder (Root-> _left); cout << root->_key << ""; _inorder (root->_right);} A recursive way of inserting a node into a search binary tree * * Note: recursive notation, the creation of nodes to be placed on the _root, without reference, the equivalent of _root on a copy of the temporary form parametric root built a node, the _root itself does not function */void _insert _r (node*& root, const k& key, const v& value) {if (root = NULL) {root = new Node (key, value); return;} if (Root->_key < key)//go to the right subtree recursively {_insert_r (Root->_right, key, value);} else if (Root->_key > key)//go to the left subtree recursively {_insert_r (Root->_left, key, value);} else//Search binary tree requires no duplicate key code return;} Search for a recursive syntax for searching a node of a binary tree node* _find_r (node* root, const k& key, const v& value) {if (root = null) {return null;} if (Root->_key < key)//go to the right subtree recursively {return _find_r (Root->_right, key, value);} else if (Root->_key > key)//go to the left subtree recursively {return _find_r (Root->_left, key, value);} else return root;//Find, return node pointer}//Delete Search recursive syntax for a node of a binary tree void _remove_r (node*& root, const k& key, const v& value) {// NULL node, returns if (Root = NULL) return;//A node: If this node is deleted. Otherwise does not handle if (Root->_left = = NULL && Root->_right = = null){if (Root->_key = = key) {Delete root;root = Null;return;} Elsereturn;} Recursively find if (Root->_key > key) {_remove_r (Root->_left, key, value) on the left subtree;} Recursively find the Else if (Root->_key < key) {_remove_r (Root->_right, key, value) on the right subtree;} The node is found, and the left and right subtree of the node is not empty, determine how to remove the node else{node* del = null;//is empty, at this time do not need to be the same as the non-recursive writing in two cases (parent->_right whether) respectively to deal with, This is the advantage of recursion. Parent->_right is empty, the previous layer is handed back is root->_left, that is, the root at this time, the chain on the Root->_left//parent->_right not empty, the previous layer of the return of the root- >_right, that is, root at this time, the chain on the root->_left//So, when the parent->_right is empty or not empty, are the previous layer recursion, root->_left or root->_right , you can use the root at this time to indicate if (root->_right = = null) {del = Root;root = Root->_left;delete Del;del = null;} Ibid., Parent->_left is empty, the upper hand returns Root->_right, that is, root at this time, the chain on the Root->_right//parent->_right not empty, the upper hand return root- >_left, is the root at this time, the chain on the root->_right//and in both cases, can be resolved with recursion, do not have to deal with the else if (root->_left = = NULL) {del = Root;root = root- >_right;delete Del;del = NULL;} The left and right sub-trees are not empty, if not recursive, there are two cases to deal with. To delete the root node first find the right subtree node firstleft,//and then go to Firstleft Zuozi, goFind the leftmost node, at which point the left subtree may be empty or not empty. Borrowing the advantages of recursion, whether it is the upper left or right sub-tree passed over,//is the last layer of recursion, that is, the root at this time. dispose of it together. So, these two situations together, can else{node* firstleft = root->_right; node* tmp = firstleft;//Find the left node while (tmp->_left) {firstleft = Tmp;tmp = Tmp->_left;} Find the leftmost node, save, swap, delete swap (Tmp->_key, Root->_key), swap (Tmp->_value, root->_value);d el = Root;root = tmp-> _right;delete Del;del = NULL;}}} Protected:node* _root;}; void Testsearchbinarytree () {typedef searchbinarynode<int, int> Node; Searchbinarytree<int, int> SBT;SBT. Insert (5, 1); SBT. Insert (3, 1); SBT. Insert (2, 1); SBT. Insert (6, 1); SBT. Insert (0, 1); SBT. Insert (9, 1); SBT. Insert (7, 1); SBT. Insert (8, 1); SBT. Insert (4, 1); SBT. Insert (1, 1); SBT. Inorder (); SBT. Find (3,1);//delete the complete test case: The sequence is inserted in order to remove the sequential deletion, and all deleted, all deletions are basically covered. SBt. Remove (1, 1); SBT. Remove (3, 1); SBT. Remove (4, 1); SBT. Remove (7, 1); SBT. Remove (6, 1); SBT. Remove (0, 1); SBT. Remove (8, 1); SBT. Remove (2, 1); SBT. Remove (5, 1); SBT. Remove (9, 1); SBT. Inorder (); SBT. Insert_r (5, 1); SBT. Insert_r (3, 1); SBT. Insert_r (2, 1); SBT. Insert_r(6, 1); SBT. Insert_r (0, 1); SBT. Insert_r (9, 1); SBT. Insert_r (7, 1); SBT. Insert_r (8, 1); SBT. Insert_r (4, 1); SBT. Insert_r (1, 1); SBT. Inorder (); node* ret = SBT. Find_r (3, 1), if (ret = = NULL) {cout << "not exist!" << Endl;} Elsecout << ret->_key << ENDL;SBT. Remove_r (1, 1); SBT. Remove_r (3, 1); SBT. Remove_r (4, 1); SBT. Remove_r (7, 1); SBT. Remove_r (6, 1); SBT. Remove_r (0, 1); SBT. Remove_r (8, 1); SBT. Remove_r (2, 1); SBT. Remove_r (5, 1); SBT. Remove_r (9, 1); SBT. Inorder ();} int main () {testsearchbinarytree (); System ("pause"); return 0;}`

"Data structure" search binary tree (recursive and non-recursive) implementation, including: Add Insert, remove, find

Related Keywords:

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.