二叉查找树(英语:Binary Search Tree),也称为二叉搜索树、有序二叉树(ordered binary tree)或排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树:
- 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
- 若任意节点的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
- 任意节点的左、右子树也分别为二叉查找树;
- 没有键值相等的节点。
SEARCH_H
这里一样的函数我写了很多种,功能可能有点不一样,请注意区别
#pragma once
#ifndef SEARCH_H
#define SEARCH_H
struct BiTree
{
BiTree *left;
BiTree *right;
int key;
int BL;
BiTree(BiTree* left, BiTree* right, int key) :left(left), right(right), key(key) {};
};
class Search
{
public:
Search();
~Search();
static bool BiTreeSearch(BiTree* T,BiTree* F,BiTree** C, int key);
static void BitreeInsert(BiTree** T,int key);//迭代算法
static void BitreeDelete(BiTree** T,int key);
static void CreateBitree(BiTree** T, int arr[],int length);
static void InOrderBitree(BiTree* T);
static void bitreeinsert(BiTree** T, int key);//递归算法
static bool bitreesearch(BiTree* T, BiTree** F, BiTree** C, int key);
static void bitreedelete(BiTree** T,int key);
static int inFindBL(BiTree* cur);
static int FindBL(BiTree* cur);
private:
int data;
};
#endif // !1
search.cpp
#include"Search.h"
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
Search::Search()
{
}
Search::~Search()
{
}
bool Search::BiTreeSearch(BiTree* T, BiTree* F, BiTree** C, int key)
{
if (!T)
{
*C = F;
return false;
}
else if(key==T->key)
{
*C = T;
return true;
}
else if (T->key < key)
{
BiTreeSearch(T->right, T, C, key);
}
else if (T->key > key)
{
BiTreeSearch(T->left, T, C, key);
}
}
void Search::BitreeInsert(BiTree** T, int key)
{
//cout << "OK2" << endl;
BiTree* P;
// cout << BiTreeSearch(*T, NULL, &P, key)<<endl;
if (!BiTreeSearch(*T, NULL, &P, key))
{
// cout << "OK3" << endl;
BiTree* cur;
cur = new BiTree(NULL, NULL, key);
// cout << cur->key << endl;
if (P == NULL)
{
// cout << "OK0" << endl;
*T = cur;
}
else
{
if (P->key > key)
{
P->left = cur;
}
// cout << "OK5" << endl;
if (P->key < key)
{
P->right = cur;
}
}
}
//cout << "OK4" << endl;
return;
}
void Search::BitreeDelete(BiTree** T, int key)
{
BiTree *q, *s,*p;
if (BiTreeSearch(*T, NULL, &p, key))
// 该节点为叶子节点,直接删除
{
if (!p->right && !p->left) {
delete p;
p = NULL; // Delete(BiTree *p) 要加&才能使P指向NULL
}
else if (!p->right) { // 右子树空则只需重接它的左子树
q = p->left;
p->key = q->key;
p->left = q->left;
p->right = q->right;
delete q;
}
else if (!p->left) { // 左子树空只需重接它的右子树
q = p->right;
p->key = q->key;
p->left = q->left;
p->right = q->right;
delete q;
}
else { // 左右子树均不空
q = p;
s = p->left;
while (s->right) {
q = s;
s = s->right;
} // 转左,然后向右到尽头
p->key = s->key; // s指向被删结点的“前驱”
if (q != p)
q->right = s->left; // 重接*q的右子树
else
q->left = s->left; // 重接*q的左子树
delete s;
}
}
}
void Search::InOrderBitree(BiTree* T)
{
if (T == NULL)
return;
InOrderBitree(T->left);
cout << T->key << " ";
InOrderBitree(T->right);
}
void Search::CreateBitree(BiTree** T, int arr[],int length)
{
for (int i = 0; i < length; i++)
{
// cout << "~~~~~~~~~~~~" << endl;
// cout << "OK1" << endl;
//cout << arr[i] << endl;
bitreeinsert(T, arr[i]);
}
}
void Search::bitreeinsert(BiTree** T, int key)
{
if (!(*T))
{
BiTree* p;
p = new BiTree(NULL, NULL, key);
p->BL = 0;
*T = p;
return;
}
else if ((*T)->key == key)
return;
else if ((*T)->key > key)
bitreeinsert(&((*T)->left), key);//改变了*T左边的地址,所以用了一个&
else if ((*T)->key < key)
bitreeinsert(&((*T)->right), key);//改变了*T又边的地址,所以用了一个&
return;
}
bool Search::bitreesearch(BiTree* T, BiTree** F, BiTree** C, int key)
{
BiTree *ff, *cc;
ff = T;
cc = ff;
while (cc)
{
if (key == cc->key) break;
else
{
ff = cc;
if (key > cc->key)
cc = cc->right;
if (key < cc->key)
cc = cc->left;
}
}
*C = cc;
*F = ff;
if (!cc) return 0;
else return 1;
}
void Search::bitreedelete(BiTree** T, int key)
{
BiTree *ff, *cc, *p, **H;
H = NULL;
if (bitreesearch(*T, &ff, &cc, key))
{
if (ff == cc)
H = T;
else if (cc->key < ff->key)
H = &(ff->left);
else if (cc->key > ff->key)
H = &(ff->right);
if (!(cc->left))
*H = cc->right;
else if (!(cc->right))
*H = cc->left;
else if (cc->left&&cc->right)
{
*H = cc->right;
p = cc->right;
while (p->left)
{
p = p->left;
}
p->left = cc->left;
}
}
}
int Search::inFindBL(BiTree* cur)
{
if (cur == NULL)
return 0;
int left = 1;
int right = 1;
left+= inFindBL(cur->left);
right+=inFindBL(cur->right);
return std::max(left, right);
}
int Search::FindBL(BiTree* cur)
{
return inFindBL(cur->left) - inFindBL(cur->right);
}
main.cpp
#include "Search.h"
#include<iostream>
using namespace std;
int main()
{
int arr[] = { 4,7,3,2,5,6,8};
int length = (int)sizeof(arr) /sizeof(*arr);
Search s;
BiTree* T = NULL ;
s.CreateBitree(&T, arr, length);
s.InOrderBitree(T);
s.bitreedelete(&T, 3);
cout << endl;
s.InOrderBitree(T);
int high;
int bl;
high=s.inFindBL(T);
bl = s.FindBL(T->left);
cout << endl;
cout << high;
cout << endl;
cout << bl;
return 0;
}