Cau truc du lieu va giai thuat

Bai2: cau5

#include<iostream>
using namespace std;
// khoi tao struct node;
struct node{
    int data;
    node *leftNode;
    node *rightNode;
};
typedef struct node NODE;
typedef NODE* TREE;
// khoi tao node moi
void createTree(TREE &t){
    t=NULL;
}
void createNewNodeForTree(TREE &t, int data){
    if(t==NULL){
        NODE *p = new node;
        p->data=data;
        p->leftNode=NULL;
        p->rightNode=NULL;
        t=p;// rootNode
    }else{
        if(data>t->data){
            createNewNodeForTree(t->rightNode,data);
        }else if(data<t->data){
            createNewNodeForTree(t->leftNode,data);
        }
    }
}
void duyetTree(TREE t){
    if(t!=NULL){
        duyetTree(t->leftNode);
        cout<<t->data<<" ";
        duyetTree(t->rightNode);
    }
}
void nodeTheMAng(TREE &X,TREE &Y){
    if(Y->leftNode!=NULL){
        nodeTheMAng(X,Y->leftNode);
    }else{
        X->data=Y->data;
        X=Y;
        Y=Y->rightNode;
    }
}
void delete1LeefNodeIntoTree(TREE &t,int data){
    if(t==NULL){
        //cay rong
        return ;
    }else{
        if(data>t->data){
            delete1LeefNodeIntoTree(t->rightNode,data);
        }else if(data<t->data){
            delete1LeefNodeIntoTree(t->leftNode,data);
        }else{
            //tim thay
            NODE *X = t;
            if(t->leftNode==NULL){
                t=t->rightNode;
            }else if(t->rightNode==NULL){
                t=t->leftNode;
            }else{
                //node co 2 con
                NODE *Y = t; //node Y la node the mang
                nodeTheMAng(X,Y);
            }
            delete X;
        }
    }
}
void Menu(TREE &t){
    while(true){
        cout<<"\n\n\r\r-------MENU-------";
        cout<<"\n1. Add node into tree";
        cout<<"\n2. Duyet tree";
        cout<<"\n3. Delete node into tree\n";
        int luachon;
        cin>>luachon;
        if(luachon==1){
            int x;// x la nodeInput
            cin>>x;
            createNewNodeForTree(t,x);
        }else if(luachon==2){
           duyetTree(t);
        }else{
            int x;
            cin>>x;
            delete1LeefNodeIntoTree(t,x);
        }
    }
}
int main(){
    TREE t;
    createTree(t);
    Menu(t);
    return 0;
   
}

bai4 cau1

#include<iostream>
using namespace std;
//======== quickSort function
int quickSort(int *a,int l, int r){
    int mid=(l+r)/2;
    int i=l,j=r;
    while(i<j){
        while(a[i]<a[mid]){
            i++;
        }
        while(a[j]>a[mid]){
            j--;
        }
        if(i<=j){
            int temp = a[i];
            a[i]=a[j];
            a[j]=temp;
            i++;
            j--;
        }
    }
    if(i<r){
        quickSort(a,i,r);
    }
    if(l<j){
        quickSort(a,l,j);
    }
}
//========== mainFunc
int main(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    quickSort(a,0,n);
    for(int i=0;i<n;i++){
        cout<<a[i]<<" ";
    };
    return 0;
}

Vu Tien Dat vjp pro xin nhat VietNam

Nhận xét

Bài đăng phổ biến từ blog này

KMACTF và nỗi buồn éo làm được gì cả T__T