GitHub 程序实现代码

# 1 二叉查找树

## 1.1 性质

x$x$为二叉查找树中的一个结点，如果y$y$x$x$左子树中的一个结点，则y>key<=x>key$y->key <= x->key$，如果y$y$x$x$右子树中的一个结点，则y>key>=x>key$y->key >= x->key$

## 2 二叉查找树程序实现

(1)BinarySearchTree.h$(1)BinarySearchTree.h$

#ifndef _BINARYSEARCHTREE_H_
#define _BINARYSEARCHTREE_H_

#include <iostream>

typedef struct BSTNode{
BSTNode *left;
BSTNode *right;
BSTNode *parent;

int key;

BSTNode(int data) : left(NULL), right(NULL), parent(NULL), key(data){}
};

class BinarySearchTree{
public:
BinarySearchTree();
~BinarySearchTree();

//插入删除操作
void Insert(int data);
BSTNode *Delete(int data);
BSTNode *root;
};

//查找操作
BSTNode *Search(BSTNode * node, int data);

//遍历操作
void InOrderWalk(BSTNode * node);
void PreOrderWalk(BSTNode * node);
void PostOrderWalk(BSTNode * node);

//查询最大最小值
BSTNode *Maximum(BSTNode * node);
BSTNode *Minimum(BSTNode * node);

//查找前驱与后继
BSTNode *PreDecessor(BSTNode *node);
BSTNode *Successor(BSTNode *node);

#endif

(2)BinarySearchTree.cpp$(2)BinarySearchTree.cpp$

#include "BinarySearchTree.h"
#include <iostream>

BinarySearchTree::BinarySearchTree()
{
root = NULL;
}

BinarySearchTree::~BinarySearchTree()
{
delete root;
}

//向二分查找树中插入数据data
void BinarySearchTree::Insert(int data)
{
BSTNode *node = new BSTNode(data);
BSTNode *p = root, *q = NULL;
while (p != NULL)
{
q = p;
if (p->key > data)
p = p->left;
else
p = p->right;
}
node->parent = q;
if (q == NULL)
root = node;
else if (q->key > data)
q->left = node;
else
q->right = node;
}

//从二分查找树中删除数据
BSTNode *BinarySearchTree::Delete(int data)
{
BSTNode *node = Search(root, data);
BSTNode *ret , *tmp;

if (node == NULL)
return node;

//如果目标结点只有一个子女则删除该结点，否则删除其后继结点
if (node->left == NULL || node->right == NULL)
ret = node;
else
ret = Successor(node);

//如果被删结点有左右孩子，将其链接到被删结点的父节点
if (ret->left != NULL)
tmp = ret->left;
else
tmp = ret->right;

if (tmp != NULL)
tmp->parent = ret->parent;
//如果被删结点的父节点为空，则说明要删的是根节点
if (ret->parent == NULL)
root = tmp;
else if (ret == ret->parent->left)
ret->parent->left = tmp;
else
ret->parent->right = tmp;

if (ret != node)
node->key = ret->key;

return ret;
}

//查找以node结点为根的子树中值为data的结点
BSTNode *Search(BSTNode * node, int data)
{
if (node == NULL || node->key == data)
return node;
if (data < node->key)
return Search(node->left, data);
else
return Search(node->right, data);
}

//遍历操作
void InOrderWalk(BSTNode * node)
{
if (node != NULL)
{
InOrderWalk(node->left);
std::cout << node->key << "\t";
InOrderWalk(node->right);
}
}

void PreOrderWalk(BSTNode * node)
{
if (node != NULL)
{
std::cout << node->key << "\t";
InOrderWalk(node->left);
InOrderWalk(node->right);
}
}

void PostOrderWalk(BSTNode * node)
{
InOrderWalk(node->left);
InOrderWalk(node->right);
std::cout << node->key << "\t";
}

//查询最大最小值
BSTNode *Maximum(BSTNode * node)
{
while (node->left != NULL)
node = node->left;
return node;
}

BSTNode *Minimum(BSTNode * node)
{
while (node->right != NULL)
node = node->right;
return node;
}

//查找前驱与后继
BSTNode *PreDecessor(BSTNode *node)
{
if (node->left != NULL)
return Maximum(node->left);

BSTNode *p = node->parent;
while (p != NULL && node == p->left)
{
node = p;
p = node->parent;
}

return p;
}

BSTNode *Successor(BSTNode *node)
{
if (node->right != NULL)
return Minimum(node->right);
BSTNode *p = node->parent;
while (p != NULL && node == p->right)
{
node = p;
p = node->parent;
}

return p;
}

(3)main.cpp$(3)main.cpp$

#include "BinarySearchTree.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int MAX = 101;
const int N = 10;

int main()
{
BinarySearchTree *bst = new BinarySearchTree();

//设置随机化种子，避免每次产生相同的随机数
srand(time(0));

//构造一个随机数组成的二分查找树
for (int i = 0; i < N; i++)
bst->Insert(rand() % MAX);

//遍历查找树
cout << "先序遍历二分查找树bst：" << endl;
PreOrderWalk(bst->root);

//遍历查找树
cout << endl << "中序遍历二分查找树bst：" << endl;
InOrderWalk(bst->root);

//遍历查找树
cout << endl << "后序遍历二分查找树bst：" << endl;
PostOrderWalk(bst->root);

int x = 45;
BSTNode *node = Search(bst->root, x);
if (node)
{
cout << endl << "二分查找树bst中存在结点x = " << x << endl;
bst->Delete(x);

cout << endl << "删除二分查找树中结点x = " << x << "成功！" << endl;

//遍历查找树
cout << endl << "先序遍历二分查找树bst：" << endl;
PreOrderWalk(bst->root);

//遍历查找树
cout << endl << "中序遍历二分查找树bst：" << endl;
InOrderWalk(bst->root);

//遍历查找树
cout << endl << "后序遍历二分查找树bst：" << endl;
PostOrderWalk(bst->root);
cout << endl << endl;
}
else{
cout << endl << "二分查找树bst中不存在结点x = " << x << endl;
cout << endl << endl;
}

system("pause");
return 0;
}

# 3 随机构造的二叉查找树

• 本文已收录于以下专栏：

举报原因： 您举报文章：《算法导论》— Chapter 12 二叉查找树 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)