标签:type 初始 插入 return 自定义类 else code ret 代码
typedef struct BiTNode {
ElemType data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
SearchBST(T, key){
while(T不空){
if(T的值等于key){
返回T;
}
if(key小于T的值)T=T->lchild;
else T=T->rchild;
}
返回NULL;
}
void SearchBST(BiTree T, ElemType key) {
while (T) {
if (T->data == key) {
return T;
}
else if (T->data > key) {
T = T->lchild;
}
else {
T = T->rchild;
}
}
return NULL;
}
InsertBST(T, key) {
if (T为空) { //原树为空,插入节点为根节点
T = new BiTNode;
T的值为key;
T的lchild和rchild都为空;
}
else if (T->data = key) { //存在相同节点,返回
返回;
}
else if (key < T - data)
返回InsertBST(T->lchild, key);
else
返回InsertBST(T->rchild, key);
}
void InsertBST(BiTree* T, ElemType key) {
if (T==NULL) {
T = new BiTNode;
T->data = key;
T->lchild = T->rchild = NULL;
}
else if (T->data = key) {
return;
}
else if (key < T - data)
return InsertBST(T->lchild, key);
else
return InsertBST(T->rchild, key);
}
CreateBST(T) {
BiTNode* T = NULL;//初始时T空树
int i=0;
自定义类型key[Maxsize];
输入key[];
while (key[]不空) {
InsertBST(T, key[i]);
i++;
}
返回T;
}
void CreateBST(BiTree& T) {
BiTNode* T = NULL;
int i = 0;
ElemType key[Maxsize];
cin << key;
while (key[i]) {
InsertBST(T, key[i]);
i++;
}
return T;
}
void InOrder(BiTree T) { //中序输出
if (T) {
InOrder(T->lchild);
cout << T->key<<‘ ‘;
InOrder(T->rchild);
}
}
DeleteBST(T, key) {
if (T为空)返回;
else
找到对应节点;
调用Delete(T)函数;
}
Delete(T) {
BiTNode* q;
if (T->rchild为空) {
q = T;
T = T->lchild;
free(q);//左子树的根节点放在被删节点的位置上
}
else if (T->lchild为空) {
q = T;
T = T->rchild;
free(q);//右子树的根节点放在被删节点的位置上
}
else
调用Delete1(T,T->lchild)//左右子树都存在
}
Delete1(T,r) {
BiTNode* q;
if (r->rchild不空)
Delete1(T, r->rchild);//递归查找前驱
else {
T->data = r->data;//赋值
q = r;r = r->lchild;
free(q);//左子树的根节点放在被删节点的位置上
}
}
void DeleteBST(BiTree& T, ElemType key) {
if (T == NULL) return;
else
{
if (key < T->data) return DeleteBST(T->lchild, key);
else if (key > T->data) return DeleteBST(T->rchild, key);
else Delete(T);
}
}
void Delete(BiTree& T) {
BiTNode* q;
if (T->rchild == NULL) {
q = T;
T = T->lchild;
free(q);
}
else if (T->lchild == NULL) {
q = T;
T = T->rchild;
free(q);
}
else
Delete1(T, T->lchild);
}
void Delete1(BiTree* T, BiTree& r) {
BiTNode* q;
if (r->rchild != NULL)
Delete1(T, r->rchild);
else {
T->data = r->data;
q = r;r = r->lchild;
free(q);
}
}
标签:type 初始 插入 return 自定义类 else code ret 代码
原文地址:https://www.cnblogs.com/2b-or-not-2b-/p/12733730.html