【问题标题】:Dynamic array does not fill in a recursive function from a binary search tree动态数组不填充二叉搜索树的递归函数
【发布时间】:2022-11-25 14:06:06
【问题描述】:

我正在制作一个程序来测试 C 语言中的二叉搜索树。我想使用不太好的数组方法来平衡它。我根据 BST 中的节点数动态分配数组,并将数组发送给另一个进行搜索的函数,并将元素放入数组中。但是,当我运行程序时,只有第一个元素(树的根)出现在数组中,而其他所有迭代都没有发生。我不确定哪里出了问题。

下面是程序中不起作用的部分的 MCVE。第一个代码块只是制作和填充 BST。它是不起作用的 sorterToArray 函数。

在递归函数 sorterToArray 中,我试图发送数组中的下一个位置,但这不起作用,因为它“是一个 nullptr”。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct treeNode
{
    int data;
    struct treeNode* left;
    struct treeNode* right;
};

typedef struct treeNode* BSTree;

void insertSorted(BSTree* tree, int data);
void sorterToArray(struct treeNode* temp, int* pArray, int i);
int counter(int i, const BSTree tree);
void goLeftAndRight(struct treeNode* newNode, struct treeNode* tempTree, const int data);
void balanceTree(BSTree* tree);
static int* writeSortedToArray(const BSTree tree);

int main(void) {
    BSTree tree = NULL;
    insertSorted(&tree, 10);
    for (int i = 0; i < 9; i++)
        insertSorted(&tree, i + 20);

    balanceTree(&tree);

}

static struct treeNode* createNode(int data)
{
    //Creates the new node for the tree
    struct treeNode* newNode;

    //Allocates memory for the new node
    newNode = calloc(1, 1 + sizeof(struct treeNode*));

    //Tests the new memory
    assert(newNode != NULL);

    //Assigns data to the new node
    newNode->data = data;

    //Sets the left and right pointers to NULL so that other functions know where the ends are
    newNode->left = NULL;
    newNode->right = NULL;

    return newNode; //Returns the new node
}

void insertSorted(BSTree* tree, int data)
{

    //Creates a new temp struct
    struct treeNode* newNode = createNode(data);
    struct treeNode* tempTree = (*tree);

    //If the tree is empty then the data will be at the root
    if (tempTree == NULL) {
        (*tree) = newNode;
    }
    //If the tree is not empty the function will call the help function in order to find the right node.
    else {
        goLeftAndRight(newNode, tempTree, data);
    }
}

void goLeftAndRight(struct treeNode* newNode, struct treeNode* tempTree, const int data) {
    if (data < tempTree->data && tempTree->left == NULL) {
        tempTree->left = newNode;
        return;
    }
    else if (data > tempTree->data && tempTree->right == NULL) {
        tempTree->right = newNode;
        return;
    }

    if (data < tempTree->data && data != tempTree->data) {
        goLeftAndRight(newNode, tempTree->left, data);
    }
    else if (data > tempTree->data && data != tempTree->data) {
        goLeftAndRight(newNode, tempTree->right, data);
    }
}

int counter(int i, const BSTree tree) {

    if (tree->left != NULL)
        i = 1 + counter(i, tree->left);
    if (tree->right != NULL)
        i = 1 + counter(i, tree->right);

    return i;
}

void balanceTree(BSTree* tree) {
    int* sortedArray = writeSortedToArray((*tree));
}

//Code blocks writeSortedToArray ans SorterToArray are the ones causing the problems.
static int* writeSortedToArray(const BSTree tree)
{
    struct treeNode* temp = tree;
    int i = 0, number = 1 + counter(i, tree);
    int* pArray = calloc(number, sizeof(int*));
    assert(pArray != NULL);

    sorterToArray(temp, pArray, i);

    return pArray;
}

//Function where the problem is most likely located.
void sorterToArray(struct treeNode* temp, int* pArray, int i) {

    if (temp->left != NULL)
        sorterToArray(temp->left, pArray, i);

    pArray[i] = temp->data;
    i = i + 1;

    if (temp->right != NULL)
        sorterToArray(temp->right, pArray, i);
}

【问题讨论】:

    标签: arrays c recursion binary-search-tree dynamic-arrays


    【解决方案1】:

    问题确实出在您指向的函数中。变量i 是函数执行上下文的局部变量,因此每个递归执行都有其“自己的”ii = i + 1 只影响一个本地实例,而其他版本的i 不会改变。这意味着那些 i 变量中没有一个会获得 2 或更大的值。

    否则将:i 按值传递给递归函数调用。

    解决方案是具有功能返回i 的更新值给调用者:

    int sorterToArray(struct treeNode* temp, int* pArray, int i) {
        if (temp->left != NULL)
            i = sorterToArray(temp->left, pArray, i);
        pArray[i] = temp->data;
        i = i + 1;
        if (temp->right != NULL)
            i = sorterToArray(temp->right, pArray, i);
        return i;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      相关资源
      最近更新 更多