根据预先计算的树构建树函数

发布于 2024-11-10 14:19:59 字数 2575 浏览 5 评论 0原文

我正在构建一棵二叉树。二叉树是预先构建在文件中的,我需要构建它。由于它的结构方式,我将树读入数组中。每个树节点看起来都是这样的。

struct Tree_Node
{
    float normalX;
    float normalY;
    float normalZ;
    float splitdistance;
    long region;
    long left, right; //array index
    Tree_Node* left_node; // pointer to left node
    Tree_Node* right_node; // pointer to right node
} typedef Tree_Node;

我尝试了多种方法来编写一些构建树的代码。让我给你一些伪代码,以便你理解我想要做什么。

  • 读入头节点。节点是数组中的第一个节点。
  • 如果节点有左右数组索引,则创建新节点并 插入数组中的信息 该树节点的索引。
  • 如果该节点没有左右索引,则它是叶节点。

这是我的构建函数:

void WLD::treeInsert(BSP_Node *tree_root, int node_number)
{
    /// Add the item to the binary sort tree to which the parameter
    // "root" refers.  Note that root is passed by reference since
    // its value can change in the case where the tree is empty.
    if ( tree_root == NULL ) 
    {
        // The tree is empty.  Set root to point to a new node containing
        // the new item.  This becomes the only node in the tree.
        tree_root = new BSP_Node();

        tree_root->normalX = bsp_array[node_number].normal[0];
        tree_root->normalY = bsp_array[node_number].normal[1];
        tree_root->normalZ = bsp_array[node_number].normal[2];
        tree_root->splitdistance = bsp_array[node_number].splitdistance;;
        tree_root->region = bsp_array[node_number].region;
        tree_root->left = bsp_array[node_number].left;
        tree_root->right = bsp_array[node_number].right;
        tree_root->left_node[node_number];
        tree_root->right_node[node_number];

        errorLog.OutputSuccess("Inserting new root node: %i", node_number);
                    // NOTE:  The left and right subtrees of root
                    // are automatically set to NULL by the constructor.
                    // This is important...
    }

    if ( tree_root->left != 0 ) 
    {
        errorLog.OutputSuccess("Inserting left node number: %i!", tree_root->left);
        treeInsert( tree_root->left_node, tree_root->left );
    }
    else if (  tree_root->right != 0 )
    {
        errorLog.OutputSuccess("Inserting right node: %i!", tree_root->right);
        treeInsert( tree_root->right_node, tree_root->right );
    }
    else if ( tree_root->right == 0 && tree_root->left == 0)
    {
        errorLog.OutputSuccess("Reached a leaf node!");
        return;
    }
    else
    {
    errorLog.OutputError("Unknown BSP tree error!");
    }

}

我的调试显示该函数尝试插入节点 2,直到程序崩溃。

有人可以帮我解决这个问题吗?

I am building a binary tree. The binary tree is pre-built in a file and I need to construct it. Due to the way it is structured, I read the tree into an array. Each tree nodes look something like this.

struct Tree_Node
{
    float normalX;
    float normalY;
    float normalZ;
    float splitdistance;
    long region;
    long left, right; //array index
    Tree_Node* left_node; // pointer to left node
    Tree_Node* right_node; // pointer to right node
} typedef Tree_Node;

I have tried a number of ways to write some code that will build the tree. Let me give you some pseudocode so you understand what I am trying to do.

  • Read in head node. Node is number one in the array.
  • If the node has a right and left array index, create new nodes and
    insert the information from the array
    indicies into that tree node.
  • If the node does not have a right and left index, it is a leaf node.

Here is my building function:

void WLD::treeInsert(BSP_Node *tree_root, int node_number)
{
    /// Add the item to the binary sort tree to which the parameter
    // "root" refers.  Note that root is passed by reference since
    // its value can change in the case where the tree is empty.
    if ( tree_root == NULL ) 
    {
        // The tree is empty.  Set root to point to a new node containing
        // the new item.  This becomes the only node in the tree.
        tree_root = new BSP_Node();

        tree_root->normalX = bsp_array[node_number].normal[0];
        tree_root->normalY = bsp_array[node_number].normal[1];
        tree_root->normalZ = bsp_array[node_number].normal[2];
        tree_root->splitdistance = bsp_array[node_number].splitdistance;;
        tree_root->region = bsp_array[node_number].region;
        tree_root->left = bsp_array[node_number].left;
        tree_root->right = bsp_array[node_number].right;
        tree_root->left_node[node_number];
        tree_root->right_node[node_number];

        errorLog.OutputSuccess("Inserting new root node: %i", node_number);
                    // NOTE:  The left and right subtrees of root
                    // are automatically set to NULL by the constructor.
                    // This is important...
    }

    if ( tree_root->left != 0 ) 
    {
        errorLog.OutputSuccess("Inserting left node number: %i!", tree_root->left);
        treeInsert( tree_root->left_node, tree_root->left );
    }
    else if (  tree_root->right != 0 )
    {
        errorLog.OutputSuccess("Inserting right node: %i!", tree_root->right);
        treeInsert( tree_root->right_node, tree_root->right );
    }
    else if ( tree_root->right == 0 && tree_root->left == 0)
    {
        errorLog.OutputSuccess("Reached a leaf node!");
        return;
    }
    else
    {
    errorLog.OutputError("Unknown BSP tree error!");
    }

}

My debug shows that the function tries to insert node 2 until the program crashes.

Can someone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-11-17 14:19:59
tree_root->left_node[node_number];

我没有看到任何初始化该数组的代码,因此这将引用无效的内容。

然后,当您转到下一个函数时,

treeInsert( tree_root->left_node, tree_root->left );

treeInsert 将使用无效指针调用,因为 left_node 不会去任何地方。

我想您需要类似 tree_root->left_node = NULL 而不是 tree_root->left_node[node_number] ,以便对 treeInsert 的递归调用创建下一个节点。

tree_root->left_node[node_number];

I don't see any code that initializes this array, so this'll be referring to something invalid.

Then by the time you come around to the next function

treeInsert( tree_root->left_node, tree_root->left );

treeInsert will be called with an invalid pointer, since left_node doesn't go anywhere.

I imagine you need something like tree_root->left_node = NULL instead of tree_root->left_node[node_number] so that the recursive call to treeInsert creates the next node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文