您的位置:首页 > 其它

二叉查找树

2010-10-01 18:57 381 查看
// binarySearchTree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
/*实现二叉查找树的一些操作
作者:blackmamba
时间:2010年9月30日
二叉查找数的一些性质:
(1)、如果x是一棵包含n个节点的子树的根,则调用inorderOutput函数的时间复杂度为O(n)。
(2)、对于一棵高度为h的二叉查找树,动态集合操作“查询”、“获取最大最小值”、“获得前驱后继结点”的时间复杂度为O(h)。
(3)、对于高度为h的二叉查找,动态几个操作“插入”和“删除”的时间复杂度为O(h)。
*/
/*树的节点*/
struct TNode
{
int data;//节点数据
TNode *left;//左孩子指针
TNode *right;//右孩子指针
TNode(int d, TNode *lp=NULL, TNode *rp=NULL)
{
data = d;
left = lp;
right = rp;
}
};
/*二叉查找树类*/
class BinarySearchTree
{
public:
TNode* insertElement1(TNode* root, int element);//插入元素(递归方法)
TNode* insertElement2(TNode* root, int element);//插入元素(非递归方法)
TNode* makeBinSearchTree(int arr[], int n);//创建二叉查找树
void inorderOutput(TNode* root, const string &separator = " ");//中序输出树中的元素
TNode* searchElement(TNode* root, int target);//查询树中的元素,返回指向该元素的节点
TNode* getMinmum(TNode* root, int &min);//获取树中最小的元素
TNode* getMaxmun(TNode* root, int &max);//获取树中最大的元素
void deleteElement(TNode* root, int element);//删除指定的元素element
TNode* getSuccessor(TNode* root, int element);//获取节点element在中序序列的情况的后继结点
TNode* getParent(TNode* root, int element);//获取节点element的父节点指针
};
/*向树中插入元素(递归方法),比较好理解*/
TNode* BinarySearchTree::insertElement1(TNode* root, int element)
{
if (root == NULL)
{
root = new TNode(element);
}
else if (element < root->data)
{
root->left = insertElement1(root->left, element);
}
else
{
root->right = insertElement1(root->right, element);
}
return root;
}
/*向树中插入元素(非递归方法),比较难理解*/
TNode* BinarySearchTree::insertElement2(TNode* root, int element)
{
if (root == NULL)
{
root = new TNode(element);
}
TNode *current = root;/*current用来查找element应该存放的位置,parent的该位置的父节点*/
TNode *parent = current;
while (current != NULL)
{
if (element < current->data)
{
parent = current;
current = current->left;
}
else
{
parent = current;
current = current->right;
}
}
//转向左子树
if (element < parent->data)
{
parent->left = new TNode(element);
}
else if (parent->data < element)
//转向右子树
{
parent->right = new TNode(element);
}
return root;
}
/*创建二叉查询树*/
TNode* BinarySearchTree::makeBinSearchTree(int arr[], int n)
{
TNode *root = NULL;
for (int i=0; i<n; i++)
{
root = insertElement2(root, arr[i]);
}
return root;
}
/*中序输入二叉树,输出结果为升序排列*/
void BinarySearchTree::inorderOutput(TNode *root, const string &separator /* =  */)
{
if (root != NULL)
{
inorderOutput(root->left);
cout<<root->data<<separator;
inorderOutput(root->right);
}
}
/*查询树中的元素*/
TNode* BinarySearchTree::searchElement(TNode* root, int target)
{
if (root == NULL || root->data == target)
{
return root;
}
else if (target < root->data)
{
return searchElement(root->left, target);
}
else
{
return searchElement(root->right, target);
}
}
/*获取树中最小的元素
参数:root为树根节点,min的最小值的引用,函数的返回值为指向最小值的节点的指针
*/
TNode* BinarySearchTree::getMinmum(TNode *root, int &min)
{
while(root->left != NULL)
{
root = root->left;
}
min = root->data;
return root;
}
/*获取树中最大的元素
参数:root为树根节点,min的最大值的引用,函数的返回值为指向最大值的节点的指针
*/
TNode* BinarySearchTree::getMaxmun(TNode* root, int &max)
{
while (root->right != NULL)
{
root = root->right;
}
max = root->data;
return root;
}
/*获取节点element在中序序列的情况的后继结点
参数:root的为树根,element的要寻找的目标节点,返回element的在中序输出情况下的后继节点
*/
TNode* BinarySearchTree::getSuccessor(TNode* root, int element)
{
TNode* current = searchElement(root, element);
int min;
//节点element的右子树不为空,则后继结点为右子树的最小值
if (current->right != NULL)
{
return getMinmum(current->right, min);
}

TNode* parent = getParent(root, element);//节点element的父节点
//查找节点current的最低祖先节点
while (parent != NULL && current == parent->right)
{
current = parent;
parent = getParent(root, current->data);
}
return parent;
}
/*获取节点element的父节点指针*/
TNode* BinarySearchTree::getParent(TNode *root, int element)
{
TNode* curremt = root;//记录当前节点指针
TNode* parent = root;//记录当前节点的父节点指针
while (curremt != NULL)
{
if (curremt->data == element)
{
return parent;
}
else if (element < curremt->data)
{
parent = curremt;
curremt = curremt->left;
}
else
{
parent = curremt;
curremt = curremt->right;
}
}
return parent;
}
/*删除指定的元素element(没写出来)
*/
void BinarySearchTree::deleteElement(TNode *root, int element)
{
TNode* temp_x = NULL;
TNode* temp_y = NULL;
TNode* current = searchElement(root, element);
if (current->left == NULL || current->right == NULL)
{
temp_y = current;
}
else
{
temp_y = getSuccessor(root, current->data);
}
if (temp_y->left != NULL)
{
temp_x = temp_y->left;
}
else
{
temp_x = temp_y->right;
}
if (temp_x != NULL)
{
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int arr[8];
int size = sizeof(arr) / sizeof(int);
for (int i=0; i<size; i++)
{
arr[i] = rand()%100;
cout<<arr[i]<<" ";
}
cout<<endl;
TNode *root = NULL;
BinarySearchTree tree;
root = tree.makeBinSearchTree(arr, size);
tree.inorderOutput(root);
cout<<endl;
TNode *node = tree.searchElement(root, 0);
if (node != NULL)
{
cout<<"找到元素"<<endl;
}
else
{
cout<<"未找到元素"<<endl;
}
int min, max;
node = tree.getMinmum(root, min);
cout<<"最小元素为:"<<min<<" "<<node->data<<endl;
node = tree.getMaxmun(root, max);
cout<<"最大元素为:"<<max<<" "<<node->data<<endl;
node = tree.getParent(root, 58);
cout<<"父节点为:"<<node->data<<endl;
node = tree.getSuccessor(root, 58);
cout<<"后继结点为:"<<node->data<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: