【问题标题】:Reordering a binary search tree 'in place'“就地”重新排序二叉搜索树
【发布时间】:2015-11-14 11:45:28
【问题描述】:

我想要一些有用的建议或代码来实现符合以下条件的程序:

  1. 在命令行(按给定顺序)从整数序列构建二叉搜索树

  2. 对树重新排序,使值以降序出现,即新树具有:

    • 左子树中的所有值都大于根
    • 右子树中的所有值都小于根
    • 此属性适用于树中的所有节点

重新排序不得涉及从输入整数构建新树(我已经实现了这一点);相反,它必须简单地在同一棵树中重新排序它们。

到目前为止,这是我的代码,它使用了不正确的方法(即制作两个单独的树并使用不同的规则插入它们)。

typedef struct node{
    int value;
    struct node *left;
    struct node *right; 
} NodeT;

NodeT *newNode(int);
NodeT *insertAscend(NodeT *, int);
NodeT *insertDescend(NodeT *, int);
void printTree(NodeT *, int);
void freeTree(NodeT *);

int main(int argc,char *argv[]){
    NodeT *t1 = NULL;
    NodeT *t2 = NULL;
    int i;
    int retval = 0;

    if(argc == 1){
        fprintf(stderr, "Usage: %s integers ...\n", argv[0]);
        retval = 1;
    }else{
        int dataGood = 1;
        for(i =1; i < argc && dataGood; i++){
            int num;
            if(sscanf(argv[i], "%d", &num) != 1){
                fprintf(stderr, "Usage: %s integers ...\n", argv[0]);
                freeTree(t1);
                freeTree(t2);
                dataGood = 0;
                retval = 1;
            }else{
                t1 = insertAscend(t1, num);
                t2 = insertDescend(t2, num);
            }
        }
        if(dataGood){
            printTree(t1, 0);

            printf("Swapped tree:\n");
            printTree(t2, 0);
            freeTree(t1);
            freeTree(t2);

        }
    }

    return retval;
}

NodeT *newNode(int v){
    NodeT *new;
    new = (NodeT *)malloc(sizeof(NodeT));
    assert(new != NULL);
    new->value = v;
    new->left = NULL;
    new->right = NULL;
    return new;
}

NodeT *insertAscend(NodeT *t, int v){
    if(t == NULL){
        t = newNode(v);
    }else if(v == t->value){
        ; // no duplicates
    }else if(v < t->value){
        t->left = insertAscend(t->left, v);
    }else if(v > t->value){
        t->right = insertAscend(t->right, v);
    }
    return t;
}

NodeT *insertDescend(NodeT *t, int v){
    if(t == NULL){
        t = newNode(v);
    }else if(v == t->value){
        ; // no duplicates
    }else if(v > t->value){
        t->left = insertDescend(t->left, v);
    }else if(v < t->value){
        t->right = insertDescend(t->right, v);
    }
    return t;
}

void printTree(NodeT *t, int depth){
    if(t != NULL){
        depth++;
        printTree(t->left, depth);
        int i;
        for(i = 1; i < depth; i++){
            putchar('\t');
        }
        printf("%d\n", t->value);
        printTree(t->right, depth);
    }
}

void freeTree(NodeT *t){
    if(t != NULL){
        freeTree(t->left);
        freeTree(t->right);
        free(t);
    }
}

我再次寻求帮助,以便在不创建任何新数据结构的情况下简单地重新排序 BST。如果我下面的代码不适用于那些愿意测试的人,我可以提供更多说明和一些期望的示例。我对此很感兴趣,因为我在之前的公司面试和考试中看到过这个问题,但似乎无法根据他们的指导有效地实施它。

【问题讨论】:

    标签: c binary-tree binary-search-tree


    【解决方案1】:

    要重新排序树,只需交换树中每个节点的左右子树即可。

    【讨论】:

    • 后缀方式遍历 BST 是否适用于这种方法?
    • 顺序无关紧要;你只需要点击每个节点。
    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多