Binary Tree ADT

来源:互联网 发布:iphone6网络wifi不稳定 编辑:程序博客网 时间:2024/05/22 06:48
int height(struct binarytree *T);void printtree(struct binarytree *T);void makeempty(struct binarytree *T);struct binarytree* find(struct binarytree *T,int x);struct binarytree* findmin(struct binarytree *T);struct binarytree* findmax(struct binarytree *T);struct binarytree* treesuccessor(struct binarytree *x);struct binarytree* treepredecessor(struct binarytree *x);struct binarytree* treeinsert(struct binarytree* T,int x);struct binarytree* treedelete(struct binarytree *T,int x);struct binarytree {    int num;    struct binarytree *left,*right,*parent;};void printtree(struct binarytree *T){    if(T!= NULL){        printtree(T->left);        printf("%d ",T->num);        printtree(T->right);    }}int height(struct binarytree *T){    if(T== NULL)        return -1;    else return (height(T->right)>height(T->left)?height(T->right):height(T->left)) + 1;}void makeempty(struct binarytree *T){    if(T != NULL){        makeempty(T->left);        makeempty(T->right);
        free(T);
    }}struct binarytree* find(struct binarytree *T,int x){  //递归查找    if(T->num == x) return T;    if(T->num>x)         find(T->left,x);    else if(T->num<x)        find(T->right,x);    else return NULL;}struct binarytree* find(struct binarytree *T,int x){  //非递归查找    while(T!=NULL){        if(T->num == x) break;        else if(T->num>x) T = T->left;        else T = T->right;    }    return T;}struct binarytree* findmin(struct binarytree *T){    while(T!=NULL){        T = T->left;    }    return T->parent;}struct binarytree* findmax(struct binarytree *T){    while(T!=NULL){        T = T->right;    }    return T->parent;}struct binarytree* treesuccessor(struct binarytree *x){ //x的后继,大于x的最小的元素    struct binarytree *y;    if(x->right!=NULL)        return findmin(x->right);    y = x->parent;    while(y!=NULL&&x == y->right){        y = y->parent;        x = x->parent;    }    return y;}struct binarytree* treepredecessor(struct binarytree *x){    struct binarytree *y;    if(x->left !=NULL)        return findmax(x->right);    y = x->parent;    while(y!=NULL&&x == y->left){        y = y->parent;        x = x->parent;    }    return y;}struct binarytree* treeinsert(struct binarytree* T,int x){ //插入元素(结构中有parent)    struct binarytree* p,*tree;    if(T == NULL){        T = (struct binarytree*)malloc(sizeof(struct binarytree));        if(T == NULL){            printf("Out of Space!\n");            return NULL;        }        else {            T->num = x;            T->left = T->right = NULL;            T->parent = NULL;        }    }    else {        tree = T;         while(tree != NULL){            if(tree->num>x) tree = tree->left;            else if(T->num<x) tree = tree->right;        }        p = tree->parent;        tree = (struct binarytree*)malloc(sizeof(struct binarytree));        if(tree == NULL){            printf("Out of Space!\n");            return NULL;        }        else {            tree->num = x;            tree->left = T->right = NULL;            tree->parent = p;        }    }    return tree;}struct binarytree* treedelete(struct binarytree *T,int a){  //删除元素(结构中有parent)    struct binarytree *x,*y,*tree;    if(T == NULL){        printf("Empty tree!\n");        return NULL;    }    while(T != NULL){        tree = T;        if(tree->num == a) break;        else if(tree->num >a) tree = tree->left;        else if(tree->num< a) tree = tree->right;    }    if(tree == NULL){        printf("Can't find the element!\n");        return NULL;    }    if(tree->left == NULL)        x = tree->right;    else if(tree->right == NULL)        x = tree->left;    else x = treesuccessor(tree);    tree->num = x->num;    if(x->left == NULL)        y = x->right;    else y = x->left;    if(y == NULL)        free(x);    else {        if(x->parent == NULL)            T = y;        else{y->parent = x->parent;        if(x == x->parent->right)             x->parent->right = y;        else x->parent->left = y;    }    free(x);    }    return y;}struct binarytree *insert(struct binarytree *T,int x){ //插入(结构中不包含parent)    struct binarytree *tmp;    if(T == NULL){        T = (struct binarytree*)malloc(sizeof(struct binarytree));        T->num = x;        T->left = T->right = NULL;    }    else if(T->num>x) T->left = insert(T->left,x);    else if(T->num<x) T->right = insert(T->right,x);    return T;}struct binarytree* treedelete(struct binarytree *T,int x){  //删除(结构中包含parent)    struct binarytree *tmp;    if(T == NULL){        printf("Can't find the element!\n");        return NULL;    }    else if(x>T->num) treedelete(T->right,x);    else if(x<T->num) treedelete(T->left,x);    else if(T->right&&T->left){        tmp = findmin(T->right);        T->num = tmp->num;        treedelete(T->right,T->num);    }    else {        tmp = T;        if(T->right == NULL)            T=T->left;        else if(T->left == NULL)            T = T->right;        free(tmp);    }    return T;}

0 0