简体   繁体   English

插入功能到AVL树中不会插入

[英]Insertion function into AVL tree won't insert

I am working on an avl tree with strings as keys. 我正在使用字符串作为键的avl树上工作。 The print statements indicate that the insert is happening but in the testing function it reports that the left and right nodes of the root remain null. 打印语句表明正在进行插入,但是在测试功能中它报告根的左节点和右节点保持为空。

Here is my avl tree code: 这是我的avl树代码:

#include "AVLAdt.h"

void printVal(node * toPrint){
    printf("\n node value: %s\n", toPrint->nodeValue);
}

node * search(node * root, char * searchVal){
    if(isExternal(root) == 1) return NULL;
    if(strcmp(searchVal,root->nodeValue)<0){
        return(search(root->leftNode,searchVal));
    }
    else if(strcmp(searchVal,root->nodeValue)==0){
        return(root);
    }
    else {
        return(search(root->rightNode,searchVal));
    }
}



/*initialize a node*/   
node * initNode(char * toAdd){
    node * newNode = malloc(sizeof(node));
    strcpy(newNode->nodeValue, toAdd);
    newNode->leftNode = NULL;
    newNode->rightNode = NULL;
    newNode->height = 1;
    return newNode;
}



/*function to insert a new node into tree and rebalance if necessary*/
node * insert(node * root, char * newValue){


    if(root == NULL){
        printf("\n Inserting %s. \n", newValue);
        return(initNode(newValue));

    }
    else{

        if(strcmp(newValue,root->nodeValue)<0){
            printf("go left");
            insert(root->leftNode, newValue);
        }
        else if(strcmp(newValue,root->nodeValue)>0){
            printf("go to right node of %s", root->nodeValue);
            insert(root->rightNode, newValue);
        }
        else{
            root->count++;
            return (root);
        }
    }

Testing program: 测试程序:

#include "AVLAdt.h"

int main(){


    node * root = NULL;

    char * testString = malloc(sizeof(char)*50);
    strcpy(testString, "aa");

    char * testString1 = malloc(sizeof(char)*50);
    strcpy(testString1, "bb");


    printf("does it try to insert?");

    root = insert(root, testString);
    root = insert(root, testString1);

    printVal(root);

    if(getRight(root) == NULL) printf("right is null");
    else{

        printf("right is");
        printVal(getRight(root));
    }

    if(getLeft(root) == NULL) printf("left is null");
    else{

        printf("left is");
        printVal(getRight(root));
    }



    return(0);
}

The code returns that both left and right nodes of "aa" are null. 该代码返回“ aa”的左右节点均为空。 Why is this? 为什么是这样?

In the search() function, not sure why you do search()函数中,不确定为什么要这样做

if(isExternal(root) == 1) return NULL;

If the node is external, ie doesn't have any leaves, you'd still want to compare its nodeValue to the searchVal and return root in case of match. 如果该node位于外部,即没有任何叶子,则您仍想将其nodeValuesearchVal进行比较,并在匹配的情况下返回root

In initNode() function, next-to-the-last line should be initNode()函数中,倒数第二行应为

newNode->count = 1;

instead of 代替

newNode->height = 1;

Also, it seems to me that in the insert() function, initNode() 's return value should be assigned to root to store the pointer to the newly added node in the tree, ie you should have: 另外,在我看来,在insert()函数中,应该将initNode()的返回值分配给root以将指向新添加的node的指针存储在树中,即您应该具有:

return root = initNode(newValue);

instead of 代替

return(initNode(newValue));

(you also don't have to put return value in parenthesis by the way). (顺便说一下,您也不必将返回值放在括号中)。

WhozCraig has already pointed out issue with the return values of recursive insert() calls. WhozCraig已经指出了递归insert()调用的返回值问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM