Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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++ c+中的二叉树+;,can';我不知道如何保存节点 Node*binTree::insert(vector&vec,int-index,Node*leaf){ Node*new_Node=&vec[index]; Node*left_leaf=NULL; Node*right_leaf=NULL; 叶=新的_节点; 如果(新建节点->左==-1 | |新建节点->右==-1) 返回NULL; 如果(c='n') { 根=新的_节点; c='y'; } 索引=新建节点->左侧; 新建\u节点->左\u叶=插入(vec,新建\u节点->左,新建\u节点->左\u叶); 索引=新建节点->右侧; 新建\u节点->右\u叶=插入(vec,新建\u节点->右,新建\u节点->右\u叶); 返回NULL; };_C++ - Fatal编程技术网

C++ c+中的二叉树+;,can';我不知道如何保存节点 Node*binTree::insert(vector&vec,int-index,Node*leaf){ Node*new_Node=&vec[index]; Node*left_leaf=NULL; Node*right_leaf=NULL; 叶=新的_节点; 如果(新建节点->左==-1 | |新建节点->右==-1) 返回NULL; 如果(c='n') { 根=新的_节点; c='y'; } 索引=新建节点->左侧; 新建\u节点->左\u叶=插入(vec,新建\u节点->左,新建\u节点->左\u叶); 索引=新建节点->右侧; 新建\u节点->右\u叶=插入(vec,新建\u节点->右,新建\u节点->右\u叶); 返回NULL; };

C++ c+中的二叉树+;,can';我不知道如何保存节点 Node*binTree::insert(vector&vec,int-index,Node*leaf){ Node*new_Node=&vec[index]; Node*left_leaf=NULL; Node*right_leaf=NULL; 叶=新的_节点; 如果(新建节点->左==-1 | |新建节点->右==-1) 返回NULL; 如果(c='n') { 根=新的_节点; c='y'; } 索引=新建节点->左侧; 新建\u节点->左\u叶=插入(vec,新建\u节点->左,新建\u节点->左\u叶); 索引=新建节点->右侧; 新建\u节点->右\u叶=插入(vec,新建\u节点->右,新建\u节点->右\u叶); 返回NULL; };,c++,C++,所以,我的想法是,我应该创建一棵如下所示的树: 其中圆圈下面的数字是我通过向量访问的索引。在向量的每个空间上都有:左边的数字(索引)、右边的数字(索引)和节点内部的数字,所有这些似乎都起作用了,但不起作用的是我连接树的方式。我不知道如何连接这棵树。它似乎正确地遍历(预排序),但不知道从这里到哪里…有什么想法吗?递归步骤的想法也困扰着我,我不知道在哪里返回实际的左指针我不知道我是否正确理解了您的问题,但我们现在开始: 事实上,您“知道”的唯一节点是根节点,然后,如果您想转到其他节点,您只需通过指针

所以,我的想法是,我应该创建一棵如下所示的树:


其中圆圈下面的数字是我通过向量访问的索引。在向量的每个空间上都有:左边的数字(索引)、右边的数字(索引)和节点内部的数字,所有这些似乎都起作用了,但不起作用的是我连接树的方式。我不知道如何连接这棵树。它似乎正确地遍历(预排序),但不知道从这里到哪里…有什么想法吗?递归步骤的想法也困扰着我,我不知道在哪里返回实际的左指针

我不知道我是否正确理解了您的问题,但我们现在开始:

事实上,您“知道”的唯一节点是根节点,然后,如果您想转到其他节点,您只需通过指针(右和左)找到您的路

另外,这是我在预订单上的递归代码:

    Node* binTree::insert(vector<Node>& vec, int index, Node* leaf) {
    Node* new_node = &vec[index];
    Node* left_leaf=NULL;
    Node* right_leaf=NULL;
    leaf = new_node;
    if (new_node->left == -1 || new_node->right == -1)
        return NULL;
    if (c == 'n')
        {
            root = new_node;
            c = 'y';
        }
    index = new_node->left;
    new_node->left_leaf = insert(vec, new_node->left, new_node->left_leaf);
    index = new_node->right;
    new_node->right_leaf = insert(vec, new_node->right, new_node->right_leaf);
    return NULL;
};

首先,您只返回
NULL
。其次,
leaf=new\u节点
什么都不做。有用。虽然在某个点返回新的_节点可能与I重复,但我不确定应该在哪里返回实际节点的代码
void pre_ordem(node *root)
{
    if(root!= NULL)
    {
        printf("%d - ",root->num);
        pre_ordem(root->left);
        pre_ordem(root->right);
    }
}