Binary Tree ADT

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值