Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.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/8/meteor/3.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++_Tree_Binary Search Tree_Tree Traversal - Fatal编程技术网

C++ 二叉搜索树遍历

C++ 二叉搜索树遍历,c++,tree,binary-search-tree,tree-traversal,C++,Tree,Binary Search Tree,Tree Traversal,大家好,我对在BST中插入新节点表示怀疑。在addNode模块中,我试图在BST中插入一个元素,但每次添加新节点时,它都会添加到我最初从main函数传递的同一根节点,而无需遍历树内部 这是我写的代码 #include<stdio.h> #include<stdlib.h> #include<cstdio> #include<iostream> using namespace std; struct node { int data;

大家好,我对在BST中插入新节点表示怀疑。在addNode模块中,我试图在BST中插入一个元素,但每次添加新节点时,它都会添加到我最初从main函数传递的同一根节点,而无需遍历树内部

这是我写的代码

#include<stdio.h>
#include<stdlib.h>
#include<cstdio>
#include<iostream>
using namespace std;
struct node
{
    int data;
    struct node *left;
    struct node *right;
};
struct node* newNode(int data)
{
    node* temp = (node*)malloc(sizeof(struct node));
    //struct temp = new node;
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return(temp);
};
int addNode(node *dest, node *root)
{
    if(root == NULL)
    {
        cout<<"adding data to node for "<< dest->data<<endl;
        root = dest;
        cout<<"ROOT VALUE = root->data "<<root->data<<endl;
        return 1;
    }
    if(dest->data > root->data)
    {
        cout<<"Traverse right for "<<dest->data<<endl;
        addNode(dest, root->right);
    }
    else if(dest->data < root->data)
    {
        cout<<"Traverse left for "<<dest->data<<endl;
        addNode(dest, root->left);
    }
}
void printNodes(node *root)
{
    if(root != NULL)
    {
        printNodes(root->left);
        if(root->left != NULL && root->right != NULL)
            std::cout<< root->data <<" ";
        printNodes(root->right);
    }
}
int main()
{
    int i, j, k, flag;
    int arr[6] = {4, 2,8, 1, 0, 10};
    node *start = newNode(arr[0]);
    for(i = 1; i < 6; i++)
    {
        node *newOne = newNode(0);
        newOne->data = arr[i];
        cout<<"NODE DATA - start->data "<<start->data;
        if(addNode(newOne, start))
            std::cout<<"\nNode added"<<endl;
    }
    printNodes(start);
    return 1;
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构节点
{
int数据;
结构节点*左;
结构节点*右;
};
结构节点*新节点(整型数据)
{
node*temp=(node*)malloc(sizeof(struct node));
//结构温度=新节点;
温度->数据=数据;
temp->left=NULL;
temp->right=NULL;
返回(临时);
};
int addNode(节点*dest,节点*root)
{
if(root==NULL)
{
库特
…但每次添加新节点时,它都会添加到同一根
节点

这是因为您总是将其添加到同一根目录中,如下所示

if(addNode(newOne, start))
start
总是一样的。您可以让
addNode
返回新的根目录,并像这样调用它:

start = addNode(newOne,start);
我将让你来实施它


<>注意,C++中的参数总是通过值传递(除非您通过引用),因此更改方法内部的参数,
root=dest;
,对
main

中的
start
没有任何特殊的指针。分配给指针参数与分配给
int
参数没有什么不同。