#include<ctime>
#include<iostream>
using namespace std;
/*手工输入10个字母,生成一棵二叉查找树,
用递归算法打印树结构或分别输出先序和中序遍历序列以确认其结构。
键盘输入待查找的字母,计算比较次数。分别用查找成功、不成功进行测试。*/
typedef char keyType;
typedef struct {
keyType key;
}ElemType;
typedef struct BSTnode{
ElemType data;
struct BSTnode*lchild, *rchild;
}BSTnode,*BSTree;
//1.create
void create(BSTree & B,ElemType cdata) {
if (!B) {
BSTree S = new BSTnode;
S->data = cdata;
S->lchild = S->rchild = NULL;
B = S;
}
else if (cdata.key < B->data.key) {
create(B->lchild, cdata);
}else if (cdata.key > B->data.key) {
create(B->rchild, cdata);
}
}
//2.insert
void insert(BSTree & B, ElemType cdata) {
if (!B) {
BSTree S = new BSTnode;
S->data = cdata;
S->lchild = S->rchild = NULL;
B = S;
}
else if (cdata.key < B->data.key) {
insert(B->lchild, cdata);
}
else if (cdata.key > B->data.key) {
insert(B->rchild, cdata);
}
}
//非递归方式插入
void insert2(BSTree &B, ElemType cdata) {
BSTree S = new BSTnode;
S->data = cdata;
S->lchild = S->rchild = NULL;
if (!B) {
B = S;
return;
}
BSTree f = NULL;//前驱
BSTree q=B;//当前
while (q) {
if (q->data.key == cdata.key) return;
f = q;
//判断分支
if (q->data.key > cdata.key) q = q->lchild;
else q = q->rchild;
}//结束是q=NULL,f是最后一个有效结点
//通过前面如果没返回,说明没找到,可以在f的分支添加
if (f->data.key > cdata.key) f->lchild = S;
else f->rchild = S;
}
//3.分别输出先序和中序遍历序列
void preOrder(BSTree B) {
if (B) {
cout << B->data.key << " ";
preOrder(B->lchild);
preOrder( B->rchild);
}
}
void InOrder(BSTree B) {
if (B) {
InOrder(B->lchild);
cout << B->data.key << " ";
InOrder(B->rchild);
}
}
//4.用递归算法打印树结构
void visualBorder(BSTree B, int nplayer) {
if (B == NULL) return;
else {
visualBorder(B->lchild, nplayer + 1);
for (int i = 1; i <= nplayer; i++) {
if (i == nplayer) printf(" |--");
else
printf(" ");
}
cout << B->data.key << endl;
visualBorder(B->rchild, nplayer + 1);
}
}
//5.查找
/*
二叉树可以看作是一个有序表,所以在二叉排序树上进行查找和折半查找类似,也是逐步缩小范围的过程
1.若二叉树为空,则查找失败,返回空指针
2.若T->data.key == e则查找成功,返回T
3.T->data.key < e,则递归查找其的右子树
4.T->data.key > e,则递归查找其的左子树
*/
int compareTime = 0;
BSTree SearchBst(BSTree T, keyType e) {
//在根指针T所指的而二叉排序树中查找关键字为e的数据元素
//查找成功则返回指向该数据元素的结点的指针
compareTime++;
if (!T) return NULL;
else if (T->data.key == e) return T;
else if (T->data.key < e) return SearchBst(T->rchild, e);
else return SearchBst(T->lchild, e);
}
keyType getValue(BSTree t) {
if (t) return t->data.key;
else return NULL;
}
//6.删除
/*
算法思想:1.从二叉排序树的根节结点开始判断,如果次数不存在次数,就不做任何操作
2.否则假设被测的结点为*p,其双亲结点为*f,pl,pr分别表示其的左子树和右子树
分3种情况讨论:
(1)若为叶子结点,则只需要修改其双亲结点的指针即可
(2)p结点只有左子树或右子树
(3)若p的结点左右子数都有,2种处理方式==》
一。令*p的左子树为*f的左子树,*p的右子树为*s的右子树
二。令*p在中序遍历的直接前驱(或直接后继)替代*p,然后再从二叉排查树中删去它的直接前驱
*/
void deleteBst(BSTree & T, keyType e) {
BSTree p, f;
p = T; f = NULL;
//-----------------下面while循环开始操作查找关键字为e的结点-----------//
while (p) {
if (p->data.key == e) break;
f = p;
if (p->data.key < e) p = p->rchild;
else if (p->data.key > e) p = p->lchild;
}
if (!p) { cout << "找不到被删的结点" << endl; return; }//找不到被删的结点
else cout << "要删掉的点字符" << p->data.key << endl;
//==处理继承者的事===//
//=========区分3种情况==============//
BSTree q, s;
q = p;
//p是目标点,f是p的双亲,s是当前移动点,,q是s的双亲
if (p->lchild && p->rchild) {
s = p->lchild;//找到目的结点的左孩子,再从左孩子的右结点一直向右寻找到最后
while (s->rchild) {//寻找最右
q = s;
s = s->rchild;
}//结束循环时s是p中序排序的直接前驱
//前面说到的第二种做法,令*p在中序遍历的直接前驱(或直接后继)替代*p,然后再从二叉排查树种删去它的直接前驱
p->data = s->data;
//考虑特殊情况
if (q == p) //假设左子树没有右子树,即p的前驱就是p的左子树,直接把左子树的左孩子取代自己的左孩子
q->lchild = s->lchild;
else {
q->rchild = s->lchild;
}
delete s;
return;//返回函数调用处
}
else if (!p->lchild) p = p->rchild;//无右子树,只需重连接其左子树
else if (!p->rchild) p = p->lchild;//无左子树,只需重连接其右子树
//=============处理双亲后人关系======================//
if (!f) T = p;//被删的是根结点
else if (q == f->lchild) f->lchild = p;
else if (q == f->rchild) f->rchild = p;
delete q;
}
bool isEmpty(BSTree B) {
return B == NULL;
}
//递归函数输出>=x的所有数
void Print(BSTree T) {
if (T) {
Print(T->lchild);
cout << T->data.key << " ";
Print(T->rchild);
}
else return;
}
void PrintAllHighx(BSTree T, keyType x) {
//查找大于x的值并输出
BSTree p = T;
if (p) {
while (p&&p->data.key < x) {
p = p->rchild;
}//找到大于或等于x的结点
T = p;//T所指的是值>=x的结点的树的根
if (p) {
BSTree f;
f = p;
p = p->lchild;//寻找第一个小于x的结点
while (p&&p->data.key >= x) {
f = p;
p = p->lchild;
}
if (p) f->lchild = NULL;
Print(T);
}
}
}
void PrintAllLowx(BSTree T, keyType x) {
//查找小于x的值并输出
if (T) {
PrintAllLowx(T->lchild, x);
if (T->data.key <= x)
cout << T->data.key << " ";
PrintAllLowx(T->rchild, x);
}
else return;

}

int main() {
//通过ascill码来比较
BSTree B = NULL;
ElemType cdata;
srand(unsigned(time(NULL)));
cout << "==============================" << endl;
cout << "操作:初始化,手工输入10个字母 " << endl;
cout << "==============================" << endl;
for (int i = 0; i <10; i++) {
cout << "Please input int element:" << ends;
cin >> cdata.key;// cdata.key = rand()%26 + 97;
create(B, cdata);
}
cout << "先序遍历" << ends;
preOrder(B); cout << endl;
cout << "中序遍历" << ends;
InOrder(B); cout << endl;
L:
cout << "递归实现可视化当前树型" << endl;
visualBorder(B, 1);
cout << "==============================" << endl;
cout << "输入操作:1.寻找目标元素指针 " << endl;
cout << " 2.删除 " << endl;
cout << " 3.插入 " << endl;
cout << " 4.查找比值大所有点 " << endl;
cout << " 5.查找比值小所有点 " << endl;
cout<< " 其他任意数字退出程序" << endl;
cout << "==============================" << endl;
int choice = 0;
cin >> choice;
switch (choice) {
keyType e;
case 1: {
cout << "请输入寻找的元素" << endl;
cin >> e;
if (SearchBst(B, e)) {
compareTime = 0;
cout << e << "元素的指针" << ends << SearchBst(B, e) << ends;
cout << "compareTime=" << compareTime << ends;
cout << "指针所指的数值是" << getValue(SearchBst(B, e)) << endl;
}
else
{
cout << e << "不存在" << ends;
cout << "compareTime=" << compareTime << endl;
}
goto L;
break;
}
case 2: {
cout << "输入删出目标数值结点" << endl;
cin >> e;
if (e != EOF && !isEmpty(B)) {
if (e == '\n') {
e = getchar();
}
else {
deleteBst(B, e);
cout << "先序遍历" << ends;
preOrder(B); cout << endl;
cout << "中序遍历" << ends;
InOrder(B); cout << endl;
}
}
goto L;
break;
}
case 3: {
cout << "Please input char element:" << ends;
cin >> cdata.key;
insert2(B, cdata);
cout << "先序遍历" << ends;
preOrder(B); cout << endl;
cout << "中序遍历" << ends;
InOrder(B); cout << endl;
goto L;
break;
}
case 4:{
keyType x;
cout << "input the x in order to find some numbers more than it" << endl;
cin >> x;
PrintAllHighx(B, x);
cout << endl;
goto L;
break;
}
case 5:{
keyType x;
cout << "input the x in order to find some numbers lower than it" << endl;
cin >> x;
PrintAllLowx(B, x);
cout << endl;
goto L;
break;
}
default:{
break;
}
}
}