您的位置:首页 > 其它

二叉查找树的算法实现

2012-07-26 15:12 375 查看
二叉查找树主要包含以下算法:search,insert,delete,max,min,successor和predecessor。下面的程序是在vc6.0下实现的。其中,关于insert和search提高了迭代和递归两个版本,display使用了递归版本,其余算法都只提供了迭代版本。

#include <iostream>

using namespace std;

template<typename type>

class BinarySortTree;

template<typename type>

class Node{

public:

Node(type d):data(d), left(0), right(0), parent(0){}

friend BinarySortTree<type>;

private:

type data;

Node *left;

Node *right;

Node *parent;

};

template<typename type>

class BinarySortTree{

public:

BinarySortTree():root(0){}

Node<type> *searchIteration(type data){

if (root == NULL)

return NULL;

Node<type> *p = root;

while (p)

{

if (p->data == data)

return p;

else if (p->data > data)

p = p->left;

else

p = p->right;

}

return NULL;

}

Node<type> *searchRecursion(int data){

return searchRecursion(root, data);

}

Node<type> *predecessor(Node<type> *p){

Node<type> *q;

if (p->left)

{

q = p->left;

while (q->right)

q = q->right;

}else{

q = p;

while (q == q->parent->left)

q = q->parent;

}

return q;

}

Node<type> *successor(Node<type> *p){

Node<type> *q;

if (p->right)

{

q = p->right;

while (q->left)

q = q->left;

}else{

q = p;

while (q == q->parent->right)

q = q->parent;

}

return q;

}

Node<type> *min(){

Node<type> *minPtr = root;

if (root)

{

while (minPtr->left)

minPtr = minPtr->left;

return minPtr;

}

return NULL;

}

Node<type> *max(){

Node<type> *maxPtr;

if (root)

{

while (maxPtr->right)

maxPtr = maxPtr->right;

return maxPtr;

}

return NULL;

}

void insertIteration(type data){

Node<type> *p = root;

Node<type> *parent = NULL;

while (p)

{

parent = p;

if (p->data > data)

p = p->left;

else

p = p->right;

}

Node<type> *newNode = new Node<type>(data);

if (parent == NULL)

root = newNode;

else

{

newNode->parent = parent;

if (parent->data > data)

parent->left = newNode;

else

parent->right = newNode;

}

}

void insertRecursion(type data){

Node<type> *newNode = new Node<type>(data);

insertRecursion(NULL, root, newNode);

}

Node<type> *del(type data){

Node<type> *z = searchIteration(data);

if (z == NULL)

return NULL;

Node<type> *y;

if (z->left == NULL || z->right == NULL)

y = z;

else

y = successor(z);

Node<type> *x;

if (y->left)

x = y->left;

else

x = y->right;

if (x)

x->parent = y->parent;

if (y->parent == NULL)

root = x;

else{

if (y == y->parent->left)

y->parent->left = x;

else

y->parent->right = x;

}

if (y != z)

z->data = y->data;

return y;

}

void display(){

displayOneNode(root, 3);

}

private:

void displayOneNode(Node<type> *root, int layer){

if (root == NULL)

return;

displayOneNode(root->left, layer + 5);

for (int i = 0; i < layer; i ++)

cout << " ";

cout << root->data << endl;

displayOneNode(root->right, layer + 5);

}

Node<type> *searchRecursion(Node<type> *root, type data){

if (root == NULL)

return NULL;

if (root->data == data)

return root;

if (root->data > data)

return searchRecursion(root->left, data);

else

return searchRecursion(root->right, data);

}

void insertRecursion(Node<type> *parent, Node<type> *cur, Node<type> *newNode){

if (cur == NULL)

{

if (parent == NULL)

root = newNode;

else{

newNode->parent = parent;

if (parent->data > newNode->data)

parent->left = newNode;

else

parent->right = newNode;

}

return;

}

if (cur->data > newNode->data)

insertRecursion(cur, cur->left, newNode);

else

insertRecursion(cur, cur->right, newNode);

}

private:

Node<type> *root;

};

void main(){

BinarySortTree<int> binaryTree;

int arr[] = {15, 5, 16, 3, 12, 20, 10, 13, 18, 23, 6, 7};

for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i ++)

binaryTree.insertIteration(arr[i]);

cout << "**************" << endl;

binaryTree.display();

int delData[] = {13, 16, 5};

for (i = 0; i < sizeof(delData)/sizeof(delData[0]); i ++)

{

Node<int> *node = binaryTree.del(delData[i]);

if (node)

{

cout << "delete " << delData[i] << " success" << endl;

cout << "******after delete********" << endl;

binaryTree.display();

binaryTree.insertIteration(delData[i]);

cout << "**************" << endl;

binaryTree.display();

}

}

//test recursion algorithm

BinarySortTree<int> binaryTree2;

cout << "test insertRecursion" << endl;

for (i = 0; i < sizeof(arr)/sizeof(arr[0]); i ++)

binaryTree2.insertRecursion(arr[i]);

binaryTree2.display();

}

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