二叉排序树

#include <iostream>
using namespace std;
typedef struct Node{
    int key;
    struct Node* Lchild;
    struct Node* Rchild;
}BSTNode;

int EQ(int a,int b){
    if(a==b){
        return 1;
    }else{
        return 0;
    }
}

int LT(int a,int b){
    if(a<b){
        return 1;
    }else{
        return 0;
    }
}

BSTNode* BST_Search(BSTNode*T,int k){
    if(T==NULL)
        return NULL;
    else {
        if(EQ(T->key,k)){
            return T;
        }else if(LT(k,T->key)){
            return BST_Search(T->Lchild,k);
        }else{
            return BST_Search(T->Rchild,k);
        }
    }
}

void Insert_BST(BSTNode*T,int k){
    BSTNode*p=new BSTNode;
    p->key=k;
    p->Lchild=NULL;
    p->Rchild=NULL;
    if(T==NULL){
        T=p;
    }else{
        if(EQ(T->key,p->key)){
            return ;
        }else if(LT(p->key,T->key)){
            Insert_BST(T->Lchild,k);
        }else{
            Insert_BST(T->Rchild,k);
        }
    }
}


void inOrderTraverse(BSTNode* root) {
	if (root) {
		inOrderTraverse(root->Lchild);
		cout << root->key;
		inOrderTraverse(root->Rchild);
	}
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        BSTNode*T=NULL;
        int num;
        for(int i=0;i<n;i++){
            cin>>num;
            Insert_BST(T,num);
        }
        inOrderTraverse(T);
        int m;
        cin>>m;
        while(m--){
            cin>>num;
            Insert_BST(T,num);
            inOrderTraverse(T);
        }
    }
    return 0;
}

存在问题,没有任何输入

可能原因,建树失败

 

 

什么情况?

#include <iostream>
using namespace std;

typedef struct Node{
    int key;
    struct Node* Lchild;
    struct Node* Rchild;
}BSTNode;

int EQ(int a,int b){
    if(a==b){
        return 1;
    }else{
        return 0;
    }
}

int LT(int a,int b){
    if(a<b){
        return 1;
    }else{
        return 0;
    }
}

BSTNode* BST_Search(BSTNode*T,int k){
    if(T==NULL)
        return NULL;
    else {
        if(EQ(T->key,k)){
            return T;
        }else if(LT(k,T->key)){
            return BST_Search(T->Lchild,k);
        }else{
            return BST_Search(T->Rchild,k);
        }
    }
}

void Insert_BST(BSTNode*T,int k){
    cout<<"***"<<endl;

    if(T==NULL){
            cout<<"666"<<endl;
       T=new BSTNode;
    T->key=k;
    T->Lchild=NULL;
    T->Rchild=NULL;
    return;
    }

        if(T->key==k){
            cout<<"111"<<endl;
            return ;
        }else if(k<T->key){
            cout<<"222"<<endl;
            Insert_BST(T->Lchild,k);
        }else{
            cout<<"333"<<endl;
            Insert_BST(T->Rchild,k);
        }

}


BSTNode* create(int data[],int n){
	BSTNode*T = NULL;
	for(int i = 0;i < n; i++){
		Insert_BST(T,data[i]);
	}
	return T;
}

void inOrderTraverse(BSTNode* root) {
	if (root) {
		inOrderTraverse(root->Lchild);
		cout << root->key;
		inOrderTraverse(root->Rchild);
	}
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;

        int*num=new int[n];
        for(int i=0;i<n;i++){
            cin>>num[i];
        }
        BSTNode*T=create(num,n);
        inOrderTraverse(T);
        int m;
        cin>>m;
        int z;
        while(m--){
            cin>>z;
            Insert_BST(T,z);
            inOrderTraverse(T);
        }
    }
    return 0;
}

???似乎解决问题了

#include <iostream>
using namespace std;

typedef struct Node{
    int key;
    struct Node* Lchild;
    struct Node* Rchild;
}BSTNode;

void Insert_BST(BSTNode*&T,int k){

    if(T==NULL){
       T=new BSTNode;
        T->key=k;
        T->Lchild=NULL;
        T->Rchild=NULL;
        return;
    }

        if(T->key==k){
            return ;
        }else if(k<T->key){
            Insert_BST(T->Lchild,k);
        }else{
            Insert_BST(T->Rchild,k);
        }
}

BSTNode* create(int n){
	BSTNode*T = NULL;
	int z;
	for(int i = 0;i < n; i++){
        cin>>z;
		Insert_BST(T,z);
	}
	return T;
}

void inOrderTraverse(BSTNode* root) {
	if (root) {
		inOrderTraverse(root->Lchild);
		cout << root->key<<" ";
		inOrderTraverse(root->Rchild);
	}
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        BSTNode*T=create(n);
        inOrderTraverse(T);
        cout<<endl;
        int m;
        cin>>m;
        int z;
        while(m--){
            cin>>z;
            Insert_BST(T,z);
            inOrderTraverse(T);
            cout<<endl;
        }
    }
    return 0;
}

加一个地址符?

完美搞定

查找:

#include <iostream>
using namespace std;

typedef struct Node{
    int key;
    struct Node* Lchild;
    struct Node* Rchild;
}BSTNode;

BSTNode* BST_Search(BSTNode*T,int k,int&num){

    if(T==NULL)
        return NULL;
    else {
            num++;
        if(T->key==k){

            return T;
        }else if(k<T->key){
            return BST_Search(T->Lchild,k,num);
        }else{
            return BST_Search(T->Rchild,k,num);
        }
    }
}

void Insert_BST(BSTNode*&T,int k){

    if(T==NULL){
       T=new BSTNode;
        T->key=k;
        T->Lchild=NULL;
        T->Rchild=NULL;
        return;
    }

        if(T->key==k){
            return ;
        }else if(k<T->key){
            Insert_BST(T->Lchild,k);
        }else{
            Insert_BST(T->Rchild,k);
        }
}


BSTNode* create(int n){
	BSTNode*T = NULL;
	int z;
	for(int i = 0;i < n; i++){
        cin>>z;
		Insert_BST(T,z);
	}
	return T;
}

void inOrderTraverse(BSTNode* root) {
	if (root) {
		inOrderTraverse(root->Lchild);
		cout << root->key<<" ";
		inOrderTraverse(root->Rchild);
	}
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        BSTNode*T=create(n);
        inOrderTraverse(T);
        cout<<endl;
        int m;
        cin>>m;
        int z,num;
        while(m--){
            cin>>z;
            num=0;
            BSTNode*F=BST_Search(T,z,num);
            if(F==NULL){
                cout<<"-1"<<endl;
            }else{
                cout<<num<<endl;
            }

        }
    }
    return 0;
}

删除:

#include <iostream>
using namespace std;

typedef struct Node{
    int key;
    struct Node* Lchild;
    struct Node* Rchild;
}BSTNode;

BSTNode*BST_Search(BSTNode*T,int k){
    if(T==NULL)
        return NULL;
    else {
        if(T->key==k){
            return T;
        }else if(k<T->key){
            return BST_Search(T->Lchild,k);
        }else{
            return BST_Search(T->Rchild,k);
        }
    }
}

void Delete(BSTNode*&F){
    if(F->Lchild==NULL){
            cout<<"Lch"<<endl;
        F=F->Rchild;
    }else if(F->Rchild==NULL){
        cout<<"Rch"<<endl;
        F=F->Lchild;
    }else{
        cout<<"emm"<<endl;
        BSTNode*q=F;
        BSTNode*p=F->Lchild;
        while(p->Rchild){
            q=p;
            p=p->Rchild;
        }
        F->key=p->key;
        if(q!=F){
            q->Rchild=p->Lchild;
        }else{
            q->Lchild=p->Lchild;
        }
        return ;
    }
}

void Insert_BST(BSTNode*&T,int k){

    if(T==NULL){
       T=new BSTNode;
        T->key=k;
        T->Lchild=NULL;
        T->Rchild=NULL;
        return;
    }

        if(T->key==k){
            return ;
        }else if(k<T->key){
            Insert_BST(T->Lchild,k);
        }else{
            Insert_BST(T->Rchild,k);
        }
}


BSTNode* create(int n){
	BSTNode*T = NULL;
	int z;
	for(int i = 0;i < n; i++){
        cin>>z;
		Insert_BST(T,z);
	}
	return T;
}

void inOrderTraverse(BSTNode* root) {
	if (root) {
		inOrderTraverse(root->Lchild);
		cout << root->key<<" ";
		inOrderTraverse(root->Rchild);
	}
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        BSTNode*T=create(n);
        inOrderTraverse(T);
        cout<<endl;
        int m;
        cin>>m;
        int z,num;
        while(m--){
            cin>>z;
            BSTNode*F=BST_Search(T,z);
            if(F!=NULL){
                    cout<<"could found"<<endl;
                Delete(F);
            }
            inOrderTraverse(T);
            cout<<endl;
        }
    }
    return 0;
}

也许是没有删除野指针的操作,导致出问题

#include <iostream>
using namespace std;

typedef struct Node{
    int key;
    struct Node* Lchild;
    struct Node* Rchild;
}BSTNode;

BSTNode*BST_Search(BSTNode*T,int k){
    if(T==NULL)
        return NULL;
    else {
        if(T->key==k){
            return T;
        }else if(k<T->key){
            return BST_Search(T->Lchild,k);
        }else{
            return BST_Search(T->Rchild,k);
        }
    }
}

void Delete(BSTNode*F){
    BSTNode*q;
    BSTNode*s;
    if(F->Lchild==NULL){
        cout<<"Lch"<<endl;
        F=F->Rchild;
    }else if(F->Rchild==NULL){
        cout<<"Rch"<<endl;
        F=F->Lchild;
    }else{
        cout<<"emm"<<endl;
        q=F;
        s=F->Lchild;
        while(s->Rchild){
            q=s;
            s=s->Rchild;
        }
        F->key=s->key;
        if(q!=F){
            q->Rchild=s->Lchild;
        }else{
            q->Lchild=s->Lchild;
        }
    }
     return ;
}

void Insert_BST(BSTNode*&T,int k){
    if(T==NULL){
       T=new BSTNode;
        T->key=k;
        T->Lchild=NULL;
        T->Rchild=NULL;
        return;
    }
        if(T->key==k){
            return ;
        }else if(k<T->key){
            Insert_BST(T->Lchild,k);
        }else{
            Insert_BST(T->Rchild,k);
        }
}


BSTNode* create(int n){
	BSTNode*T = NULL;
	int z;
	for(int i = 0;i < n; i++){
        cin>>z;
		Insert_BST(T,z);
	}
	return T;
}

void inOrderTraverse(BSTNode* root) {
	if (root) {
		inOrderTraverse(root->Lchild);
		cout << root->key<<" ";
		inOrderTraverse(root->Rchild);
	}
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        BSTNode*T=create(n);
        inOrderTraverse(T);
        cout<<endl;
        int m;
        cin>>m;
        int z,num;
        while(m--){
            cin>>z;
            BSTNode*F=BST_Search(T,z);
            if(F!=NULL){
                    cout<<"could found"<<endl;
                Delete(F);
            }
            inOrderTraverse(T);
            cout<<endl;
        }
    }
    return 0;
}

有问题啊?

 

 

#include <iostream>
using namespace std;

typedef struct Node{
    int key;
    struct Node* Lchild;
    struct Node* Rchild;
    struct Node* Father;
}BSTNode;

BSTNode*BST_Search(BSTNode*T,int k){
    if(T==NULL)
        return NULL;
    else {
        if(T->key==k){
            return T;
        }else if(k<T->key){
            return BST_Search(T->Lchild,k);
        }else{
            return BST_Search(T->Rchild,k);
        }
    }
}

void Delete(BSTNode*F){
    BSTNode*q;
    BSTNode*s;
    if(F->Lchild==NULL){
        cout<<"Lch"<<endl;
        q=F;
        F=F->Rchild;

    }else if(F->Rchild==NULL){
        cout<<"Rch"<<endl;
        q=F;
        F=F->Lchild;
    }else{
        cout<<"emm"<<endl;
        q=F;
        s=F->Lchild;
        while(s->Rchild){
            q=s;
            s=s->Rchild;
        }
        F->key=s->key;
        if(q!=F){
            q->Rchild=s->Lchild;
        }else{
            q->Lchild=s->Lchild;
        }
    }
}

void Insert_BST(BSTNode*&T,int k,BSTNode*&F){
    if(T==NULL){
       T=new BSTNode;
        T->key=k;
        T->Lchild=NULL;
        T->Rchild=NULL;
        T->Father=F;
        return;
    }
        if(T->key==k){
            return ;
        }else if(k<T->key){
            Insert_BST(T->Lchild,k,T);
        }else{
            Insert_BST(T->Rchild,k,T);
        }
}


BSTNode* create(int n){
	BSTNode*T = NULL;
	int z;
	for(int i = 0;i < n; i++){
        cin>>z;
		Insert_BST(T,z,NULL);
	}
	return T;
}

void inOrderTraverse(BSTNode* root) {
	if (root) {
		inOrderTraverse(root->Lchild);
		cout << root->key<<" ";
		inOrderTraverse(root->Rchild);
	}
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        BSTNode*T=create(n);
        inOrderTraverse(T);
        cout<<endl;
        int m;
        cin>>m;
        int z,num;
        while(m--){
            cin>>z;
            BSTNode*F=BST_Search(T,z);
            inOrderTraverse(T);
            if(F!=NULL){
                cout<<"could found"<<endl;
                inOrderTraverse(T);
                Delete(F);
            }
            inOrderTraverse(T);
            cout<<endl;
        }
    }
    return 0;
}

这里还没改完,我觉得是传父亲结点,但是看来不用,再试一试

#include <iostream>
using namespace std;

typedef struct Node{
    int key;
    struct Node* Lchild;
    struct Node* Rchild;
}BSTNode;

BSTNode*BST_Search(BSTNode*T,int k){
    if(T==NULL)
        return NULL;
    else {
        if(T->key==k){
            return T;
        }else if(k<T->key){
            return BST_Search(T->Lchild,k);
        }else{
            return BST_Search(T->Rchild,k);
        }
    }
}

void Delete(BSTNode*&F){
    BSTNode*q;
    BSTNode*s;
    if(F->Lchild==NULL){
        q=F;
        F=F->Rchild;
        delete F;
    }else if(F->Rchild==NULL){
        q=F;
        F=F->Lchild;
        delete F;
    }else{
        q=F;
        s=F->Lchild;
        while(s->Rchild){
            q=s;
            s=s->Rchild;
        }
        F->key=s->key;
        if(q!=F){
            q->Rchild=s->Lchild;
        }else{
            q->Lchild=s->Lchild;
        }
        delete s;
    }
}

void Insert_BST(BSTNode*&T,int k){
    if(T==NULL){
       T=new BSTNode;
        T->key=k;
        T->Lchild=NULL;
        T->Rchild=NULL;
        return;
    }
        if(T->key==k){
            return ;
        }else if(k<T->key){
            Insert_BST(T->Lchild,k);
        }else{
            Insert_BST(T->Rchild,k);
        }
}


BSTNode* create(int n){
	BSTNode*T = NULL;
	int z;
	for(int i = 0;i < n; i++){
        cin>>z;
		Insert_BST(T,z);
	}
	return T;
}

void inOrderTraverse(BSTNode* root) {
	if (root) {
		inOrderTraverse(root->Lchild);
		cout << root->key<<" ";
		inOrderTraverse(root->Rchild);
	}
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        BSTNode*T=create(n);
        inOrderTraverse(T);
        cout<<endl;
        int m;
        cin>>m;
        int z,num;
        while(m--){
            cin>>z;
            BSTNode*F=BST_Search(T,z);
            if(F!=NULL){
                Delete(F);
            }
            inOrderTraverse(T);
            cout<<endl;
        }
    }
    return 0;
}

照旧

#include <iostream>
using namespace std;

typedef struct Node{
    int key;
    struct Node* Lchild;
    struct Node* Rchild;
}BSTNode;


void Delete(BSTNode*&F){
    BSTNode*q;
    BSTNode*s;
    if(F->Lchild==NULL){
        q=F;
        F=F->Rchild;
        delete F;
    }else if(F->Rchild==NULL){
        q=F;
        F=F->Lchild;
        delete F;
    }else{
        q=F;
        s=F->Lchild;
        while(s->Rchild){
            q=s;
            s=s->Rchild;
        }
        F->key=s->key;
        if(q!=F){
            q->Rchild=s->Lchild;
        }else{
            q->Lchild=s->Lchild;
        }
        delete s;
    }
}

void Delete_Search(BSTNode*&T,int k){
    if(T==NULL)
        return ;
    else {
        if(T->key==k){
            Delete(T);
        }else if(k<T->key){
            Delete_Search(T->Lchild,k);
        }else{
            Delete_Search(T->Rchild,k);
        }
    }
}


void Insert_BST(BSTNode*&T,int k){
    if(T==NULL){
       T=new BSTNode;
        T->key=k;
        T->Lchild=NULL;
        T->Rchild=NULL;
        return;
    }
        if(T->key==k){
            return ;
        }else if(k<T->key){
            Insert_BST(T->Lchild,k);
        }else{
            Insert_BST(T->Rchild,k);
        }
}


BSTNode* create(int n){
	BSTNode*T = NULL;
	int z;
	for(int i = 0;i < n; i++){
        cin>>z;
		Insert_BST(T,z);
	}
	return T;
}

void inOrderTraverse(BSTNode* root) {
	if (root) {
		inOrderTraverse(root->Lchild);
		cout << root->key<<" ";
		inOrderTraverse(root->Rchild);
	}
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        BSTNode*T=create(n);
        inOrderTraverse(T);
        cout<<endl;
        int m;
        cin>>m;
        int z,num;
        while(m--){
            cin>>z;
            Delete_Search(T,z);
            inOrderTraverse(T);
            cout<<endl;
        }
    }
    return 0;
}

引用是个好东西,指针也不能保证确实会返回。

 

当F变化,F->Father->Child因为和它时同一个,所以也直接发生了变化,你第一感觉做的题没有问题,后面想多了

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值