【问题标题】:Tree building function from precalculated tree来自预先计算的树的树构建函数
【发布时间】:2011-05-31 07:25:53
【问题描述】:

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

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,直到程序崩溃。

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

【问题讨论】:

  • 这是作业吗?如果是这样,你能把它标记为这样吗?
  • 请使用格式化功能让大家更容易理解您的下一个问题:)
  • @tjameson 这不是家庭作业。我正在尝试从 Quake 引擎操作 BSP 格式。

标签: c++ binary-tree bsp-tree


【解决方案1】:
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 的递归调用会创建下一个节点。

【讨论】:

  • @Jeff Foster 你是在建议我使用构造函数吗?我如何为这个结构写一个构造函数?
  • 构造函数可能会有所帮助,但我认为“tree_root->left_node[node_number]”行可能是导致问题的原因。你能解释一下这条线的意图是什么吗?我不确定我明白它在做什么。
  • 你能再具体一点吗?不同的错误?你能附加一个调试器并得到一些有用的东西吗?
  • @Jeff Foster Hrm。甚至不确定那在做什么。我将其注释为相同的效果。该代码只是应该使用数组中相应值的值创建一个节点。如果它有左索引,则使用该索引值创建一个左节点。如果它具有正确的索引,请执行相同的操作。如果两者都没有,则它是一个叶节点,这就是 return 语句应该递归进入的地方。
  • @Jeff Foster 调试输出显示它一直在命中 if (tree_root->left != 0) 部分并传递索引值 2。这可能是我需要一个构造函数并且需要最初将节点中的所有指针初始化为 NULL。
猜你喜欢
  • 2020-12-12
  • 1970-01-01
  • 2017-03-19
  • 2019-10-20
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多