您的位置:首页 > 编程语言 > C语言/C++

二分查找树的C++实现 Binary Search Tree(BST)

2014-03-13 19:14 483 查看
用C++实现了二叉查找树的

创建

插入

删除

遍历(宽度优先和三种深度优先[前序,中序,后序])

查找某值

查找最大值和最小值

清空

复制二叉树(深复制)

#include <queue>
#include <iostream>
using namespace std;
struct BSTNode
{
int val;
BSTNode *left;
BSTNode *right;
};

/* version1
BSTNode *InsertNode(BSTNode *root, int data)  //if return is void,* should be *&
{
if(root == NULL)
{
root = new BSTNode;
root->left = NULL;
root->right = NULL;
root->val = data;
}
else if(root->val < data)
root->right = InsertNode(root->right,data);
else if(root->val > data)
root->left = InsertNode(root->left,data);
else
;//duplicate, do nothing
return root;
}

BSTNode *BuildBST(BSTNode *root, int a[], int n)
{
for (int i = 0; i < n; i++)
{
root = InsertNode(root,a[i]);
}
return root;
}*/

//version2
void InsertNode(BSTNode *&root, int data)
{
if(root == NULL)
{
root = new BSTNode;
root->left = NULL;
root->right = NULL;
root->val = data;
}
else if(root->val < data)
InsertNode(root->right,data);
else if(root->val > data)
InsertNode(root->left,data);
else
;//duplicate, do nothing
}

void BuildBST(BSTNode *&root, int a[], int n)
{
for (int i = 0; i < n; i++)
{
InsertNode(root,a[i]);
}
}

//breadth first 宽度优先
//从树的根开始,访问完一层后再访问下一层
//用队列来实现,在处理每一个节点(出队)时,把它的左右儿子(如果有)入队
//队列为空则处理完成
void LevelorderTraversal(BSTNode *treeNode)
{
queue<BSTNode *> Q;
Q.push(treeNode);
while(!Q.empty())
{
BSTNode *node = Q.front();
cout << node->val << " ";

if (node->left != NULL)
{
Q.push(node->left);
}
if (node->right != NULL)
{
Q.push(node->right);
}
Q.pop();
}
cout << "\n";
}

//递归实现前序遍历
void PreOrderTranversal(BSTNode *treeNode) //depth-first
{
if (treeNode != NULL)
{
cout << treeNode->val << " ";
PreOrderTranversal(treeNode->left);
PreOrderTranversal(treeNode->right);
}
}
//递归实现中序遍历
void InorderTranversal(BSTNode *treeNode)
{
if (treeNode != NULL)
{
InorderTranversal(treeNode->left);
cout << treeNode->val << " ";
InorderTranversal(treeNode->right);
}
}
//递归实现后续遍历
void PostOrderTranversal(BSTNode *treeNode)
{
if (treeNode != NULL)
{
PostOrderTranversal(treeNode->left);
PostOrderTranversal(treeNode->right);
cout << treeNode->val << " ";
}
}

//查找树中是否存在某一个元素
bool SearchBST(BSTNode *treeNode,int data)
{
if(treeNode == NULL)
return false;

if (treeNode->val == data)
return true;

else if (treeNode->val <data)
return SearchBST(treeNode->right,data);
else
return SearchBST(treeNode->left,data);
}

bool IsEmpty(BSTNode *treeNode)
{
if(NULL == treeNode)
return true;
return false;
}

//利用递归查找树的最小值,该值出现在最左的树叶上
BSTNode *FindMin(BSTNode *treeNode)
{
if (treeNode == NULL)//只有根节点为空时这行才被执行,递归过程中只会执行后两条分支,FindMax同理
return NULL;
else if(treeNode->left == NULL)
return treeNode;
else
return FindMin(treeNode->left);
}
//利用递归查找树的最大值,该值出现在最右的树叶上
BSTNode *FindMax(BSTNode *treeNode)
{
if (treeNode == NULL)
return NULL;
else if(treeNode->right == NULL)
return treeNode;
else
return FindMax(treeNode->right);
}
//利用非递归实现FindMax
BSTNode *FindMax1(BSTNode *treeNode)
{
if(treeNode == NULL)
return NULL;
while (treeNode->right != NULL)
{
treeNode = treeNode->right;
}
return treeNode;
}

//当找到带删节点时,分两种情况进行处理
//1.节点有左右子树时,把该节点的右子树的最小的值赋给该节点,
//然后删除右子树最小的值的节点(该节点一定为叶子)(或者取左子树最大的值)
//2.节点有一个儿子或无儿子时,把该节点指向儿子(或许为空),再删除该节点
void Remove(BSTNode *&treeNode, int data)
{
if (treeNode->val < data)
{
Remove(treeNode->right,data);
}
else if(treeNode->val > data)
{
Remove(treeNode->left,data);
}
else if(treeNode->left != NULL && treeNode->right !=NULL) //found,the node has two children
{
treeNode->val = FindMin(treeNode->right)->val;
Remove(treeNode->right,treeNode->val);
//or
/*treeNode->val = FindMax(treeNode->left)->val;
Remove(treeNode->left,treeNode->val);
*/
}
else //found, the node has one or no child
{
BSTNode *tmp = treeNode;
treeNode = (treeNode->left != NULL) ? treeNode->left : treeNode->right;
delete tmp;
}
}
void MakeEmpty(BSTNode *&root) //post order deletion
{
if (root != NULL)
{
MakeEmpty(root->left);
MakeEmpty(root->right);
delete root;
}
root = NULL;
}

BSTNode *Clone(BSTNode *treeNode)
{
if(treeNode == NULL)
return NULL;
BSTNode *node = new BSTNode;
node->val = treeNode->val;
node->left = Clone(treeNode->left);
node->right = Clone(treeNode->right);
return node; // return root at last
}
int main(void)
{
/*
4
2    6
1 3  5 7
*/
int a[] = {4,2,6,1,3,5,7};

BSTNode *root = NULL;
BuildBST(root,a,sizeof(a) / sizeof(a[0]));

LevelorderTraversal(root);
PreOrderTranversal(root);
cout << "\n";
InorderTranversal(root);
cout << "\n";
PostOrderTranversal(root);
cout << "\n";

cout << FindMin(root)->val << "\n";
cout << FindMax(root)->val << "\n";

Remove(root,6);
LevelorderTraversal(root);

BSTNode *dup = Clone(root);
LevelorderTraversal(dup);
MakeEmpty(root);
MakeEmpty(dup);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: