二叉搜索树添加功能

时间:2016-04-29 22:01:07

标签: c binary-tree

我正在尝试为二叉树构建函数,而_addNode()遇到了麻烦。

我的NodeBSTree结构如下:

struct Node {
    TYPE         val;
    struct Node *left;
    struct Node *right;
};

struct BSTree {
    struct Node *root;
    int          cnt;
};

我也有init,创建新树,大小,并且是空函数:

void initBSTree(struct BSTree *tree)
{
    tree->cnt  = 0;
    tree->root = 0;
}

struct BSTree*  newBSTree()
{
    struct BSTree *tree = (struct BSTree *)malloc(sizeof(struct BSTree));
    assert(tree != 0);

    initBSTree(tree);
    return tree;
}

int isEmptyBSTree(struct BSTree *tree) { return (tree->cnt == 0); }

int sizeBSTree(struct BSTree *tree) { return tree->cnt; }

但是我的添加功能是给我带来麻烦的功能:

struct Node *_addNode(struct Node *cur, TYPE val) {
    struct Node* newNode;
    //base case
    if(cur == 0)  {
        newNode = (struct Node*) malloc(sizeof(struct Node));
        assert(newNode !=0);
        newNode->val = val;
        newNode->left = 0;
        newNode->right = 0;
        return newNode;
    }
    //recursive case
    if(val < cur->val)  {
        cur->left = _addNode(cur->left,val);
    }
    else if(val > cur->val) {
        cur->right = _addNode(cur->right, val);
    }
    return cur;
}

void addBSTree(struct BSTree *tree, TYPE val)
{
    tree->root = _addNode(tree->root, val);
    tree->cnt++;
}

我将TYPE定义为:# define TYPE void*,我正在尝试使用以下比较函数对其进行测试:

struct data {
    int number;
    char *name;
};

int compare(TYPE left, TYPE right) {
    struct data *leftD;
    leftD = (struct data*)left;
    struct data *rightD;
    rightD = (struct data*)right;
    if (leftD->number < rightD->number) {
        return -1;
    }
    else if (leftD->number > rightD->number) {
        return 1;
    }
    else {
        return 0;
    }
    return 0;
}

测试时,添加一个正确的节点会破坏比较功能,因为我很确定它正在尝试与不存在的正确节点进行比较。我不完全确定问题是什么,但我几乎肯定是在我的添加节点功能而不是比较功能。

我的测试(测试3中断):

void testAddNode() {
    struct BSTree *tree = newBSTree();

    struct data myData1,  myData2,  myData3,  myData4;

    myData1.number = 50;
    myData1.name = "rooty";
    addBSTree(tree, &myData1);
    //check the root node
    if (compare(tree->root->val, (TYPE *) &myData1) != 0) {
        printf("addNode() test: FAIL to insert 50 as root\n");
        return;
    }
    //check the tree->cnt value after adding a node to the tree
    else if (tree->cnt != 1) {
        printf("addNode() test: FAIL to increase count when inserting 50 as root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 50 as root\n");


    myData2.number = 13;
    myData2.name = "lefty";
    addBSTree(tree, &myData2);

    //check the position of the second element that is added to the BST tree
    if (compare(tree->root->left->val, (TYPE *) &myData2) != 0) {
        printf("addNode() test: FAIL to insert 13 as left child of root\n");
        return;
    }
    else if (tree->cnt != 2) {
        printf("addNode() test: FAIL to increase count when inserting 13 as left of root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 13 as left of root\n");


    myData3.number = 110;
    myData3.name = "righty";
    addBSTree(tree, &myData3);

    //check the position of the third element that is added to the BST tree    
    if (compare(tree->root->right->val, (TYPE *) &myData3) != 0) {
        printf("addNode() test: FAIL to insert 110 as right child of root\n");
        return;
    }
    else if (tree->cnt != 3) {
        printf("addNode() test: FAIL to increase count when inserting 110 as right of root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 110 as right of root\n");


    myData4.number = 10;
    myData4.name = "righty of lefty";
    addBSTree(tree, &myData4);

    //check the position of the fourth element that is added to the BST tree
    if (compare(tree->root->left->left->val, (TYPE *) &myData4) != 0) {
        printf("addNode() test: FAIL to insert 10 as left child of left of root\n");
        return;
    }
    else if (tree->cnt != 4) {
        printf("addNode() test: FAIL to increase count when inserting 10 as left of left of root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 10 as left of left of root\n");
}

0 个答案:

没有答案