Binary search Tree Properties: Each node has a search basis for the key code (key), all nodes of the key code is 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.
BSTree.h #pragma once template<class k,class v> struct Bstreenode {bstreenode (const k& key,const v& V
Alue): _key (Key), _value (value), _left (null), _right (null) {} K _key;
V _value;
bstreenode<k,v>* _left;
bstreenode<k,v>* _right;
};
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 (_root = = NULL) {_root = new Node (key,value);
} node* cur = _root;
node* parent = NULL;
while (cur) {if (Cur->_key < key) {parent = cur;
Cur = cur->_right;
} else if (Cur->_key > key) {parent = cur;
Cur = cur->_left;
} else return false;
} cur = new Node (key,value);
if (Parent->_key < key) {parent->_right = cur;
} else {parent->_left = cur;
} return true;
} node* Find (const k& key) {node* cur = _root; while (cur) {if (Cur->_key < key) cur = cur->_right;
else if (Cur->_key > key) cur = cur->_left;
else {cout<<cur->_key<< ":" <<cur->_value<<endl;
return cur;
}} return NULL;
} bool Remove (const k& key) {if (_root = = NULL) {return false;
} node* cur = _root;
node* parent = NULL;
Locate the node to be deleted while (cur) {if (Cur->_key > key) {parent = cur;
Cur = cur->_left;
} else if (Cur->_key < key) {parent = cur;
Cur = cur->_right;
} else {break;
}} node* del = NULL;
1. The left child or right child to delete the node is empty if (Cur->_left = = null) {del = cur;
if (Parent->_left = = cur) {parent->_left = cur->_right;
} else {parent->_right = cur->_right;
} Delete del;
}//Delete node right child is empty else if (cur->_right = = NULL) {del = cur;
if (Parent->_left = = cur) {parent->_left = cur->_left; } else {parent->_right = cur->_left;
} Delete del;
}/////Find the node as the root node of the left (largest) right child instead of it, and then delete the else//To delete the node's left child is not empty {parent = cur;
Find the right (smallest) leftmost child that is the root node of the node instead of it, and then delete node* Subleft = cur->_right;
while (subleft->_left) {parent = Subleft;
Subleft = subleft->_left;
} Cur->_key = subleft->_key;
Cur->_value = subleft->_value;
if (Parent->_left = = subleft) Parent->_left = subleft->_right;
else Parent->_right = subleft->_right;
Delete Subleft;
} return false;
}//recursive insertion of bool Insertr (const k& key,const v& value) {return _insertr (_root,key,value);
}//Recursively delete bool Remover (const k& key) {return _remover (_root,key);
}//Recursive lookup node* findr (const k& key) {return _findr (_root,key);
} void Inorder () {_inorder (_root);
cout<<endl;
} protected:bool _insertr (node*& root,const k& key,const v& value) {if (root = = NULL) { root = new Node (key,value);
return true;
} if (Root->_key > key) {return _insertr (root->_left,key,value);
} else if (Root->_key < key) {return _insertr (root->_right,key,value);
} else {return false;
}}//recursively delete a node 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;
if (Root->_left = = NULL) {root = root->_right;
} else if (root->_right = = NULL) {root = root->_left;
} else {node* subleft = root->_right;
while (subleft->_left) {subleft = subleft->_left;
} swap (Root->_key,subleft->_key);
Swap (Root->_value,subleft->_value);
Return _remover (Root->_right,key);
} Delete del;
} return true; } node* _findr (Node*& root,const k& key) {if (_root = = null) {return null;
} if (Root->_key > key) {_findr (Root->_left,key);
} else if (Root->_key < key) {_findr (Root->_right,key);
} else {cout<<root->_key<< ":" <<root->_value<<endl;
return root;
} return NULL;
} void _inorder (node* root) {if (root = = NULL) {return;
} _inorder (Root->_left);
cout<<root->_key<< "";
_inorder (Root->_right);
} protected:node* _root;
};
Test iterations void Testtree () {bstree<int,int> BST;
int a[] = {5,3,7,1,4,6,8,0,2,9}; for (size_t i = 0; i < sizeof (a)/sizeof (a[0]); i++) {BST.
Insert (A[i],i); } BST.
Inorder (); bstreenode<int,int>* ret = BST.
Find (6); Bst.
Remove (9); Bst.
Inorder (); Bst.
Remove (7); Bst.
Inorder (); Bst.
Remove (5); Bst.
Inorder (); Bst.
Remove (3); Bst.
Inorder ();
}//Test recursive void Testtreer () {bstree<int,int> bst1; int a1[] = {5,3,7,1,4,6,8,0,2,9}; for (size_t i = 0; i < sizeof (A1)/sizeof (A1[0]); i++) {bst1.
INSERTR (A1[i],i); } bst1.
Inorder (); bstreenode<int,int>* Ret1 = Bst1.
Findr (6); Bst1.
Remover (9); Bst1.
Inorder (); Bst1.
Remover (5); Bst1.
Inorder (); Bst1.
Remover (3); Bst1.
Inorder (); Bst1.
Remover (7); Bst1.
Inorder (); Bst1.
Remover (8); Bst1.
Inorder (); Bst1.
Remover (2); Bst1.
Inorder (); Bst1.
Remover (6); Bst1.
Inorder (); }
Test.cpp
#include <iostream>
using namespace std;
#include "BSTree.h"
int main ()
{
//testtree ();
Testtreer ();
GetChar ();
return 0;
}