二叉搜索树的使用
//这一个版本写的是较为简单的树,分为了三个部分组成,
主要是利用栈的思想来进行前序遍历我们的树,本程序没有采用递归去前序遍历,用递归的话效率过低,不推荐使用.
//本程序所实现的功能有
//插入元素(必须小于栈的最大容量). 前序遍历 二叉搜索树的删除(必须小于当前树内元素的个数) .再次前序遍历 .查找指定节点
teer.h
#pragma once
#define MAX_NODE 1024
typedef int ElemeType;
typedef struct _Bnode {
ElemeType data;
struct _Bnode *lchild, *rchild;
}Bnode, Btree;
stack.h
#pragma once
#include<stdlib.h>
#include<stdio.h>
#include"tree.h"
#define MaxSize 128
typedef struct _SqStack {
Bnode *base;
Bnode *top;
}SqStack;
bool InitStack(SqStack &S) {
S.base = new Bnode[MaxSize];
if (!S.base) return false;
S.top = S.base;
return true;
}
bool PushStack(SqStack &S, Bnode e) {
if (S.top - S.base == MaxSize)
return false;
*(S.top++) = e;
return true;
}
bool PopStack(SqStack &S, Bnode &e) {
if (S.base == S.top)
return false;
e = *(--S.top);
return true;
}
Bnode *GetTop(SqStack &S) {
if (S.top != S.base) {
return S.top - 1;
} else {
return NULL;
}
}
int GetSize(SqStack &S) {
return(S.top - S.base);
}
bool IsEmpty(SqStack &S) {
if (S.top == S.base) {
return true;
} else {
return false;
}
}
void DestroyStack(SqStack &S) {
if (S.base) {
free(S.base);
S.base = NULL;
S.top = NULL;
}
}
tree.cpp
#include<iostream>
#include<Windows.h>
#include"stack.h"
using namespace std;
static int length = 0;
bool InsertBnode(Btree* &root, Bnode* node) {
Btree *tmp=NULL, *parent = NULL;;
if (!node) return false;
node->lchild = NULL;
node->rchild = NULL;
if (root->data == NULL) {
root->data = node->data;
length++;
return true;
}
tmp = root;
while (tmp) {
parent = tmp;
if (node->data > parent->data) {
tmp = tmp->rchild;
} else {
tmp = tmp->lchild;
}
}
if (node->data > parent->data) {
parent->rchild = node;
} else {
parent->lchild= node;
}
length++;
return true;
}
void proprint(Btree* root) {
Btree tmp;
SqStack stack;
InitStack(stack);
PushStack(stack, *root);
while (!(IsEmpty(stack))) {
PopStack(stack, tmp);
printf("- %d -", tmp.data);
if (tmp.rchild != NULL) {
PushStack(stack, *(tmp.rchild));
}
if (tmp.lchild != NULL) {
PushStack(stack, *(tmp.lchild));
}
}
DestroyStack(stack);
}
int findMax(Btree* root) {
while(root->rchild){
root = root->rchild;
}
return root->data;
}
Btree* DeleteNode(Btree* root, int key) {
if (root == NULL) return NULL;
if (root->data > key) {
root->lchild = DeleteNode(root->lchild, key);
return root;
}
if (root->data < key) {
root->rchild = DeleteNode(root->rchild, key);
return root;
}
if (root->lchild == NULL && root->rchild == NULL) {
length--;
return NULL;
}
if (root->lchild == NULL && root->rchild != NULL) {
length--;
return root->rchild;
}
if (root->lchild != NULL && root->rchild == NULL){
length--;
return root->lchild;
}
int val = findMax(root->lchild);
root->data=val;
root->lchild = DeleteNode(root->lchild,val);
length--;
return root;
}
bool QueryByLoop(Bnode* root, int Element) {
while (root != NULL && root->data!= Element) {
if (Element <root->data) {
root = root->lchild;
} else {
root = root->rchild;
}
}
if (root == NULL) {
return false;
} else {
return true;
}
}
int main(void) {
Btree *s=new Btree;
s->lchild = s->rchild = NULL;
s->data = NULL;
Bnode *node;
node = new Bnode;
cout << "请输入你想要入树的元素个数(不得超过本栈的最大容量: " << MaxSize << "): ";
int num=-1;
cin >> num;
while (1) {
if (num > MaxSize) {
cout << "输入的个数超过了本栈的最大容量,请重新输入!!!! ";
cout << "请输入你想要入树的元素个数(不得超过本栈的最大容量: " << MaxSize << "): ";
cin >> num;
} else {
break;
}
}
cout << "请输入你想要插入的元素: ";
cin>>node->data;
if(InsertBnode(s, node)) {
cout << "插入元素 " << node->data << "成功! " << endl;
} else {
cout << "插入元素失败! " << endl;
}
for (int i = 1; i < num; i++) {
node = new Bnode;
cout << "请输入你想要插入的元素: ";
cin >> node->data;
if (InsertBnode(s, node)) {
cout << "插入元素 " << node->data << "成功! " << endl;;
} else {
cout << "插入元素失败! " << endl;
}
}
proprint(s);
cout << "一共有 " << length << " 个元素" << endl;
cout << "请输入你要删除的元素的个数(不能超过现在的树的最大个数: " << length << "): ";
int Element;
cout << endl;
cin >> num;
while (1) {
if (num > length) {
cout << "输入的个数超过了本栈的最大容量,请重新输入!!!! ";
cout << "请输入你想要入树的元素个数(不得超过本栈的最大容量: " << length << "): ";
cin >> num;
} else {
break;
}
}
for (int i = 0; i < num; i++) {
cout << "请输入你想删除的值: ";
cin >> Element;
DeleteNode(s, Element);
cout << "***********************************************" << endl;
proprint(s);
cout << "一共有 " << length << " 个元素)" << endl;
cout << "***********************************************" << endl;
}
cout << "请输入你想要查找的值: " << endl;
cin >> Element;
if (QueryByLoop(s, Element)) {
cout << "查找成功!! 查找的值是: " << Element << endl;
} else {
cout << "查找失败!不存在这个值" << endl;
}
proprint(s);
cout << "一共有 " << length << " 个元素" << endl;
system("pause");
return 0;
}