c - AVL 树插入(在 C 中)失败

标签 c data-structures avl-tree

我从Data Structures and Algorithms Analysis in C学了AVL tree,自己敲代码,插入功能不太好用。

我用很多数据检查了这些功能。有些节点无法插入,有些节点随机插入。未分类,我是说。

这是我的部分代码:

AVLTree.h:

/* Data structures model */
typedef int data_type;
typedef struct avlnode {
    data_type data;
    int height;
    struct avlnode *lchild;
    struct avlnode *rchild;
} AvlNode;
typedef AvlNode AvlTree;

/* Function Prototypes including init, find(custom/min/max) and insert */
AvlTree *AVL_create(data_type value);
AvlNode *AVL_find(AvlTree *tree, data_type value);
AvlNode *AVL_find_min(AvlTree *tree);
AvlTree *AVL_find_max(AvlTree *tree);
AvlTree *AVL_insert(AvlTree *tree, data_type value);
#define MAX_HEIGHT(x,y) (x > y) ? x : y

AVL树.c

/* Static function to get the height of a node in the tree */
static int height(AvlNode *node) {
    return (node == NULL) ? -1 : node->height;
}

/* Tree init func with a valued root node */
AvlTree *AVL_create(data_type value) {
    AvlTree *newtree;
    newtree = (AvlTree *)malloc(sizeof(AvlTree));
    if (newtree == NULL)
        return NULL;

    newtree->lchild = NULL;
    newtree->rchild = NULL;
    newtree->height = 0;
    newtree->data = value;
    return newtree;
}

/* Node search functions. In fact I use BST search functions here */ 
/* I'm not sure could them run well here in AVL tree */

AvlNode *AVL_find(AvlTree *tree, data_type value) {
    AvlTree *temptree = tree;
    if (temptree == NULL)
        return NULL;
    if (value < temptree->data)
        return AVL_find(tree->lchild, value);
    else if (value > temptree->data)
        return AVL_find(tree->rchild, value);
    else
        return temptree;
}
AvlNode *AVL_find_min(AvlTree *tree) {
    AvlTree *temptree = tree;
    if (temptree != NULL) {
        while (temptree->lchild != NULL)
            temptree = temptree->lchild;
    }
    return temptree;
}
AvlTree *AVL_find_max(AvlTree *tree) {
    AvlTree *temptree = tree;
    if (temptree != NULL) {
        while (temptree->rchild != NULL)
            temptree = temptree->rchild;
    }
    return temptree;
}


AvlTree *AVL_insert(AvlTree *tree, data_type value) {
    AvlTree *temptree = tree;

    if (temptree == NULL) {
        temptree = (AvlNode *)malloc(sizeof(AvlNode));
        if (temptree == NULL)
            return NULL;
        else {
            temptree->data = value;
            temptree->height = 0;
            temptree->lchild = NULL;
            temptree->rchild = NULL;
        }
    }
    else if (value < temptree->data) {
        temptree->lchild = AVL_insert(temptree->lchild, value);
        if (height(temptree->lchild) - height(temptree->rchild) == 2) {
            if (value < temptree->lchild->data)
                temptree = single_rotate_with_left(temptree);
            else
                temptree = double_rotate_with_right_left(temptree);
        }
    }
    else if (value > temptree->data) {
        temptree->rchild = AVL_insert(temptree->rchild, value);
        if (height(temptree->rchild) - height(temptree->lchild) == 2) {
            if (value > temptree->rchild->data)
                temptree = single_rotate_with_right(temptree);
            else
                temptree = double_rotate_with_left_right(temptree);
        }
    }
    temptree->height = MAX_HEIGHT(height(temptree->lchild), height(temptree->rchild)) + 1;
    return temptree;
}

主.c

#include "AVLTree.h"

int main() {
    AvlTree *newtree = AVL_create(50);

    AVL_insert(newtree, 70);
    AVL_insert(newtree, 80);
    AVL_insert(newtree, 90);

    for (int i = -5; i < 20; i++) {
        AVL_insert(newtree, i * i * i);
    }

    printf("root node: %d\n", newtree->data);
    printf("left of root node: %d\n", newtree->lchild->data);
    printf("findmin: %d\n", AVL_find_min(newtree)->data);
    printf("findmax: %d\n", AVL_find_max(newtree)->data);
    return 0;
}

最佳答案

我已经尝试了您的程序并禁用了重新平衡(请参阅下面的位置)。 然后就可以正常工作了,打印出来:

root node: 50
left of root node: -125
findmin: -125
findmax: 6859

我认为这是正确的。

所以我认为问题出在您的轮换功能之一。

如果您找不到问题,请将它们展示给我们。

else if (value < temptree->data) {
    temptree->lchild = AVL_insert(temptree->lchild, value);
    /*if (height(temptree->lchild) - height(temptree->rchild) == 2) {
        if (value < temptree->lchild->data)
            temptree = single_rotate_with_left(temptree);
        else
            temptree = double_rotate_with_right_left(temptree);
    }*/
}
else if (value > temptree->data) {
    temptree->rchild = AVL_insert(temptree->rchild, value);
    /*if (height(temptree->rchild) - height(temptree->lchild) == 2) {
        if (value > temptree->rchild->data)
            temptree = single_rotate_with_right(temptree);
        else
            temptree = double_rotate_with_left_right(temptree);
    }*/
}

关于c - AVL 树插入(在 C 中)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26749899/

相关文章:

algorithm - 找到删除需要旋转2次的节点?

c - 在C中,访问数组索引更快还是通过指针访问更快?

data-structures - 在 B 树的上下文中, "key"究竟意味着什么?

c++ - 如何编写自定义词典比较器 C++

C - 构建动态分配的指针数组,指向由文件输入填充的结构

java - 如何修复决定树是否平衡树的方法?

c - fscanf() 仅拾取文件的第一行

Java : When does corresponding memory come into existence for local variables?

c - Scanf() 无法识别 %c 之前的空格

data-structures - AVL 树 : difference between leaves' depths?