Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将函数插入AVL树中赢得';t插入_C_Nodes_Avl Tree - Fatal编程技术网

C 将函数插入AVL树中赢得';t插入

C 将函数插入AVL树中赢得';t插入,c,nodes,avl-tree,C,Nodes,Avl Tree,我正在使用字符串作为键的avl树上工作。print语句表示插入正在进行,但在测试函数中,它报告根的左、右节点仍然为null 以下是我的avl树代码: #include "AVLAdt.h" void printVal(node * toPrint){ printf("\n node value: %s\n", toPrint->nodeValue); } node * search(node * root, char * searchVal){ if(isExterna

我正在使用字符串作为键的avl树上工作。print语句表示插入正在进行,但在测试函数中,它报告根的左、右节点仍然为null

以下是我的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);
        }
    }
代码返回“aa”的左、右节点均为null。这是为什么?

search()函数中,不确定为什么要这样做

if(isExternal(root) == 1) return NULL;
如果
节点
是外部的,即没有任何叶子,您仍然希望将其
节点值
搜索值
进行比较,并在匹配的情况下返回

initNode()
函数中,最后一行的旁边应该是

newNode->count = 1;
而不是

newNode->height = 1;
return(initNode(newValue));
此外,在我看来,在
insert()
函数中,
initNode()
的返回值应该分配给
root
,以在树中存储指向新添加的
节点的指针,即,您应该具有:

return root = initNode(newValue);
而不是

newNode->height = 1;
return(initNode(newValue));
(顺便说一下,您也不必将返回值放在括号中)


WhozCraig已经指出了递归
insert()调用的返回值的问题。

请密切注意
insert
函数的返回值。当您调用
insert(root->leftNode,newValue)
insert(root->righnode,newValue)时,您认为这可能很重要吗目前这两个都忽略了结果?谢谢你告诉我去哪里看而没有给出确切的答案,我还是花了一分钟的时间哈哈。我仍然很难理解递归函数。不用担心。当您第一次使用递归时,它是一件易变的事情。请记住,调用堆栈和/或返回值通常是在下降过程中“存储”东西的位置,使它们在稍后退出时可以恢复。祝你好运。那么你有没有尝试过提议的改变(见下面的答案),它们对你有用吗?