您的位置:首页 > 其它

二叉查找树实现类——二叉链表

2012-03-26 21:44 288 查看
今晚头还是晕晕的,就随便写个数据结构来提神。

就选了二叉查找树吧,老规矩,直接参考了Wiki上面的内容。

二叉查找树的特性:

二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树

若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
它的左、右子树也分别为二叉排序树。

二叉查找树的讨论:

二叉排序树的查找过程和次优二叉树类似,通常采取二叉链表作为二叉排序树的存储结构。中序遍历二叉排序树可得到一个关键字的有序序列,一个无序序列可以通过构造一棵二叉排序树变成一个有序序列,构造树的过程即为对无序序列进行排序的过程。每次插入的新的结点都是二叉排序树上新的叶子结点,在进行插入操作时,不必移动其它结点,只需改动某个结点的指针,由空变为非空即可。搜索,插入,删除的复杂度等于树高,期望O(logn),最坏O(n)(数列有序,树退化成线性表).

虽然二叉排序树的最坏效率是O(n),但它支持动态查询,且有很多改进版的二叉排序树可以使树高为O(logn),如SBT,AVL,红黑树等.故不失为一种好的动态排序方法.

代码如下:

#include <iostream>

using namespace std;

template <class NodeType>
class BSTreeNode
{
public:
NodeType value;
BSTreeNode* lChild, rChild;

BSTreeNode();
};

template <class NodeType>
BSTreeNode<NodeType>::BSTreeNode()
{
lChild = NULL;
rChild = NULL;
}

template <class NodeType>
class BinarySearchTree
{
public:
BinarySearchTree();
BinarySearchTree(BSTreeNode<NodeType> *node);
~BinarySearchTree();

void CreatBinaryTree(BSTreeNode<NodeType> *node);
void DestroyTree(BSTreeNode<NodeType> *node);
void InsertValue(NodeType value);							//NodeType必须重载了==及>操作符
void DeleteNode(BSTreeNode<NodeType> *node);				//NodeType必须重载了=操作符
BSTreeNode<NodeType>* FindValue(NodeType value);			//NodeType必须重载了=,==及>操作符

private:
BSTreeNode<NodeType> *root;
void CopyTree(BSTreeNode<NodeType> *position, BSTreeNode<NodeType> *node);
void InsertValue(BSTreeNode<NodeType> *node, NodeType value);
BSTreeNode<NodeType>* FindValueFrom(BSTreeNode<NodeType> *node, NodeType value);
};

template <class NodeType>
BinarySearchTree<NodeType>::BinarySearchTree()
{
root = NULL;
}

template <class NodeType>
BinarySearchTree<NodeType>::BinarySearchTree(BSTreeNode<NodeType> *node)
{
CreatBinaryTree(node);
}

template <class NodeType>
BinarySearchTree<NodeType>::~BinarySearchTree()
{
DestroyTree(root);
}

template <class NodeType>
void BinarySearchTree<NodeType>::CreatBinaryTree(BSTreeNode<NodeType> *node)
{
CopyTree(root, node);
}

template <class NodeType>
void BinarySearchTree<NodeType>::CopyTree(BSTreeNode<NodeType> *position, BSTreeNode<NodeType> *node)
{
if (NULL == node)
{
position = NULL;
return ;
}

position = new BSTreeNode<NodeType>;
position->value = node->value;

CopyTree(position->lChild, node->lChild);
CopyTree(position->rChild, node->rChild);
}

template <class NodeType>
void BinarySearchTree<NodeType>::DestroyTree(BSTreeNode<NodeType> *node)
{
if (NULL == node)
{
return ;
}

DestroyTree(node->lChild);
DestroyTree(node->rChild);

delete node;
}

template <class NodeType>
void BinarySearchTree<NodeType>::InsertValue(NodeType value)
{
InsertValue(root, value);
}

template <class NodeType>
void BinarySearchTree<NodeType>::InsertValue(BSTreeNode<NodeType> *node, NodeType value)
{
if (NULL == node)
{
node = new BSTreeNode<NodeType>;
node->value = value;
return ;
}

if (node->value == value)
{
return ;
}
else if ( node->value > value)
{
InsertValue(node->lChild, value);
}
else
{
InsertValue(node->rChild, value);
}
}

template <class NodeType>
BSTreeNode<NodeType>* BinarySearchTree<NodeType>::FindValueFrom(BSTreeNode<NodeType> *node, NodeType value)
{
if (NULL == node)
{
return NULL;
}

if (node->value == value)
{
return node;
}
else if (node->value > value)
{
return FindValueFrom(node->lChild, value);
}
else
{
return FindValueFrom(node->rChild, value);
}
}

template <class NodeType>
BSTreeNode<NodeType>* BinarySearchTree<NodeType>::FindValue(NodeType value)
{
return FindValueFrom(root,value);
}

template <class NodeType>
void BinarySearchTree<NodeType>::DeleteNode(BSTreeNode<NodeType> *node)
{
if (NULL == node)
{
return ;
}

BSTreeNode<NodeType> *currentNode = FindValue(node->value);
if (NULL == currentNode)
{
return ;
}

BSTreeNode<NodeType> *lastNode;
while (currentNode != NULL)
{
lastNode = currentNode;
currentNode->value = currentNode->rChild.value;
currentNode = currentNode->rChild;
}

delete currentNode;
lastNode->rChild = NULL;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: