标签:struct ++ 码代码 pac char s delete order ebs 删除
void SearchBST(BiTree T, int key)//查找
{
if (T为空)return;
else if (T的值 == key)cout << "查找成功" << endl;
else if (T的值 > key)SearchBST(T->rchild, key);右子树查找
else if (T的值 > key)SearchBST(T->lchild, key);左子树查找
}
void SearchBST(BiTree T, int key)//查找
{
if (T==NULL)return;
else if (T->data == key)cout << "查找成功" << endl;
else if (T->data > key)SearchBST(T->rchild, key);
else if (T->data > key)SearchBST(T->lchild, key);
}
void InsertBST(BiTree T, int key)//插入
{
if (T为空) {
新建T
}
else{
if (T的值 == key)return;
else if (key<T的值)InsertBST(T->lchild, key);左子树插入
else if (key > T的值)InsertBST(T->rchild, key);右子树插入
}
}
void InsertBST(BiTree T, int key)//插入
{
if (T == NULL) {
T = new BiTNode;
T->lchild = NULL;
T->rchild = NULL;
}
else{
if (T->data == key)return;
else if (key<T->data)InsertBST(T->lchild, key);
else if (key > T->data)InsertBST(T->rchild, key);
}
}
void CreateBST(BiTree T)//创建
{
if(T为空){
新建T;
}
char str[20];
cin >> str;
for (int i = 0; str[i] != ‘\0‘; i++) {//使用数组循环插入
InsertBST(T, str[i]);
}
}
void CreateBST(BiTree T)//创建
{
if(T==NULL){
T=new BiTNode;
T->lchild = new BiTNode;
T->rchild = new BiTNode;
T->lchild = NULL;
T->rchild = NULL;
}
char str[20];
cin >> str;
for (int i = 0; str[i] != ‘\0‘; i++) {
InsertBST(T, str[i]);
}
}
void DeleteBST(BiTree T, int key)//删除
{
if (T的值 == key) {
if (T->rchild 不为空) {
if (T->lchild 不为空) {
T->rchild->lchild = T->lchild;右孩子先指向左孩子,再释放T;
free(T);
}
else free(T);右孩子为空,无论左孩子是否存在,都释放T
}
else if (T->lchild 不为空)free(T);左孩子不存在,无论右孩子释放存在,都释放T
}
}
void DeleteBST(BiTree T, int key)//删除
{
if (T->data == key) {
if (T->rchild != NULL) {
if (T->lchild != NULL) {
T->rchild->lchild = T->lchild;
free(T);
}
else free(T);
}
else if (T->lchild != NULL)free(T);
}
}
#include<iostream>
using namespace std;
typedef struct BiTNode {
int data;
struct BiTNode* lchild;
struct BiTNode* rchild;
}BiTNode, * BiTree;
void SearchBST(BiTree T, int key);
void InsertBST(BiTree T, int key);
void CreateBST(BiTree T);
void DeleteBST(BiTree T, int key);
void InorderTraverse(BiTree T);
void SearchBST(BiTree T, int key)//查找
{
if (!T)return;
else if (T->data == key)cout << "查找成功" << endl;
else if (T->data > key)SearchBST(T->rchild, key);
else if (T->data > key)SearchBST(T->lchild, key);
}
void InsertBST(BiTree T, int key)//插入
{
if (T == NULL) {
T = new BiTNode;
T->lchild = NULL;
T->rchild = NULL;
}
else{
if (T->data == key)return;
else if (key<T->data)InsertBST(T->lchild, key);
else if (key > T->data)InsertBST(T->rchild, key);
}
}
void CreateBST(BiTree T)//创建
{
if(T==NULL){
T=new BiTNode;
T->lchild = new BiTNode;
T->rchild = new BiTNode;
T->lchild = NULL;
T->rchild = NULL;
}
char str[20];
cin >> str;
for (int i = 0; str[i] != ‘\0‘; i++) {
InsertBST(T, str[i]);
}
}
void DeleteBST(BiTree T, int key)//删除
{
if (T->data == key) {
if (T->rchild != NULL) {
if (T->lchild != NULL) {
T->rchild->lchild = T->lchild;
free(T);
}
else free(T);
}
else if (T->lchild != NULL)free(T);
}
}
void InorderTraverse(BiTree T)//中序遍历
{
if (T)
{
InorderTraverse(T->lchild);
printf("%d ", T->data);
InorderTraverse(T->rchild);
}
}
int main()
{
int key;
BiTree T;
T = new BiTNode;
CreateBST(T);
cout << "中序输出" << endl;
InorderTraverse(T);
cout << "请输入查找数据" << endl;
cin >> key;
SearchBST(T, key);
InsertBST(T, key);
return 0;
}
标签:struct ++ 码代码 pac char s delete order ebs 删除
原文地址:https://www.cnblogs.com/lim-M/p/12734006.html