二叉树的非递归在上篇博客中已经写过了,现在来看递归形式。
#include<iostream>
#include<assert.h>
using namespace std;
template<class k, class v>
struct BSTreeNode
{
BSTreeNode(const k&key, const v&value)
:_pLeft(NULL)
, _pRight(NULL)
, _key(key)//要查找的值
, _value(value)//数组的下标
{}
BSTreeNode<k, v>*_pLeft;
BSTreeNode<k, v>*_pRight;
k _key;
v _value;
};
template<class k, class v>
class BSTree
{
public:
typedef BSTreeNode<k, v>Node;
typedef BSTreeNode<k, v>* pNode;
BSTree()
:_pRoot(NULL)
{}
void _CopyBinaryTee(pNode *pRoot)//创建二叉搜索树
{
pRoot = new Node(pRoot->_key, pRoot->_value);
_CopyBinaryTee(&((*pRoot)->_pLeft));
_CopyBinaryTee(&((*pRoot)->_pRight));
}
BSTree<k, v>& operator = (const BSTree<k, v>& bst)
{
if (this != &bt)
{
_DestoryBinaryTree(_pRoot);
_pRoot = _CopyBinaryTree(bt._pRoot);
}
return *this;
}
void _DestoryBinaryTree(pNode pRoot)//销毁空间
{
if (pRoot)
{
_DestoryBinaryTree(pRoot->_pLeft);
_DestoryBinaryTree(pRoot->_pRight);
delete pRoot;
pRoot = NULL;
}
}
bool insertNor(const k&key, const v&value)
{
return _insertNor(_pRoot, key,value);
}
bool _insertNor(pNode &pRoot, const k&key,const v&value)
{
if (pRoot == NULL)
{
pRoot = new Node(key, value);
return true;
}
if (key < pRoot->_key)
{
return _insertNor(pRoot->_pLeft, key, value);
}
else if (key > pRoot->_key)
{
return _insertNor(pRoot->_pRight, key, value);
}
else
return false;
}
pNode FindNor(const k&key)
{
return _Find(_pRoot, key);
}
pNode _Find(pNode pRoot,const k&key)//查找函数
{
if (pRoot == NULL)
{
return NULL;
}
if (key == pRoot->_key)
{
return pRoot;
}
else if (pRoot->_key > key)
{
return _Find(pRoot->_pLeft, key);
}
else
{
return _Find(pRoot->_pRight, key);
}
}
k MinData()//找寻最左边小的数
{
assert(_pRoot);
pNode pcur = _pRoot;
while (pcur->_pLeft)
{
pcur = pcur->_pLeft;
}
return pcur->_key;
}
k maxData()//找寻最右边大的数
{
assert(_pRoot);
pNode pcur = _pRoot;
while (pcur->_pRight)
{
pcur = pcur->_pRight;
}
return pcur->_key;
}
void Inorder()
{
return _Inorder(_pRoot);
}
void _Inorder(pNode pRoot)//中序
{
if (pRoot)
{
_Inorder(pRoot->_pLeft);
cout << "<" << pRoot->_key << "," << pRoot->_value << ">" << endl;
_Inorder(pRoot->_pRight);
}
}
bool removeNor(const k&key)
{
return _removeNor(_pRoot, key);
}
bool _removeNor(pNode &pRoot, const k&key)//删除
{
if (pRoot == NULL)
{
return false;
}
if (pRoot->_key > key)
{
_removeNor(pRoot->_pLeft, key);
}
else if (pRoot->_key < key)
{
_removeNor(pRoot->_pRight, key);
}
else
{
if (pRoot->_pLeft == NULL)//只有右子树
{
pRoot = pRoot->_pRight;
delete pRoot;
}
else if (pRoot->_pRight == NULL)//只有左子树
{
pRoot = pRoot->_pLeft;
delete pRoot;
}
else //左右子树存在
{
pNode parent = pRoot;
pNode pDel = pRoot->_pRight;//找到其右子树中序下的第一个节点
while (pDel->_pLeft)//找到其左子树
{
parent = pDel;
pDel = pDel->_pLeft;
}
pRoot->_key = pDel->_key;
pRoot->_value = pDel->_value;
if (parent->_pLeft == pDel)
parent->_pLeft = pDel->_pRight;
else
parent->_pRight = pDel->_pRight;
}
// delete pDel;
///* pDel = NULL;*/
}
return true;
}
//注:若要删除的节点无孩子节点,可以归结到第二三种情况中。
~BSTree()
{
_DestoryBinaryTree(_pRoot);
}
private:
pNode _pRoot;
};
#include<string>
int main()
{
int arr[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 };
BSTree<int, int>bst;
for (int i = 0; i <10; i++)
{
bst.insertNor(arr[i],i);
}
bst.Inorder();
cout << bst.FindNor(3)->_value << endl;
cout << endl;
bst.removeNor(7);
bst.Inorder();
cout << bst.MinData() << endl;
cout << bst.maxData() << endl;
BSTree<string, string>bs;
bs.insertNor("xiaodu", "小杜");
cout << bs.FindNor("xiaodu")->_value << endl;
system("pause");
return 0;
}
结果为: