Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.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/4/wpf/14.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 - Fatal编程技术网

如何在C中为二叉树创建新节点

如何在C中为二叉树创建新节点,c,C,我正在尝试创建一个二叉搜索树,我了解其中的基本功能,但我只停留在一件事上。在我的主要职能中: struct listing { int id; char agent[12]; int price; int size; //In square feet int numBeds; double numBaths; int yearBuilt; char address[30]; struct listing *left;

我正在尝试创建一个二叉搜索树,我了解其中的基本功能,但我只停留在一件事上。在我的主要职能中:

struct listing
{
    int id;
    char agent[12];
    int price;
    int size; //In square feet
    int numBeds;
    double numBaths;
    int yearBuilt;
    char address[30];
    struct listing *left;
    struct listing *right;
};

struct listing* addListing(struct listing *node, int id, char agent[12], int price, int size, int numBeds, double numBaths, int yearBuilt,
char address[ADD_LENGTH])
{
    if(node == NULL)
    {
        struct listing *newNode = (struct listing*)malloc(sizeof(struct listing));
        newNode->id = id;
        newNode->agent[12] = agent[12];
        newNode->price = price;
        newNode->size = size;
        newNode->numBeds = numBeds;
        newNode->numBaths = numBaths;
        newNode->yearBuilt = yearBuilt;
        newNode->address[ADD_LENGTH] = address[ADD_LENGTH];
        newNode->left = NULL;
        newNode->right = NULL;
        return newNode;
    }

    if(id > node->id)
    {
       node->right = addListing(node->right, id, agent, price, size, numBeds, numBaths, yearBuilt, address);
    }
    else if(id < node->id)
    {
        node->left = addListing(node->left, id, agent, price, size, numBeds, numBaths, yearBuilt, address);
    }
}


int main(void)
{
    int i;
    struct listing* newNode = NULL;
    newNode = addListing(newNode, 143, "Blaze", 32, 400500, 13, 6.5, 2016, "8802 Reedy Drive");
    struct listing* newNode2 = addListing(newNode, 165, "Target", 78, 502050, 12, 8.0, 2017, "1648 Relish Lane");

    return 0;
}
struct列表
{
int-id;
炭剂[12];
国际价格;
int size;//以平方英尺为单位
整数;
双麻木;
国际年建成;
字符地址[30];
结构列表*左;
结构列表*右;
};
struct listing*addListing(struct listing*节点,int id,char代理[12],int price,int size,int numberds,double numBaths,int yearbuilded,
字符地址[添加长度])
{
if(node==NULL)
{
结构列表*newNode=(结构列表*)malloc(sizeof(结构列表));
newNode->id=id;
新建节点->代理[12]=代理[12];
新建节点->价格=价格;
新建节点->大小=大小;
newNode->numberds=numberds;
newNode->numBaths=numBaths;
newNode->yearbuild=yearbuild;
新建节点->地址[添加长度]=地址[添加长度];
newNode->left=NULL;
newNode->right=NULL;
返回newNode;
}
如果(id>节点->id)
{
node->right=addListing(node->right,id,agent,price,size,numberds,numBaths,yearbuilded,address);
}
else if(idid)
{
node->left=addListing(node->left,id,agent,price,size,numberds,numBaths,yearbuilded,address);
}
}
内部主(空)
{
int i;
结构列表*newNode=NULL;
newNode=addListing(newNode,143,“Blaze”,32400500,136.52016,“8802 Reedy Drive”);
结构列表*newNode2=addListing(newNode,165,“Target”,7850205012017年8月8日,“1648美味巷”);
返回0;
}

我有这个,我在网上找到了这个实现,但我不确定它到底是什么意思。有人能启发我吗?谢谢。

您需要发布
struct listing
addListing()
。我们不是心灵感应者。哦,对不起,我试着去做,所以我不需要,但我会的。我的坏习惯是二叉树,对吗?因此,
listing
是一个节点,
addListing()
是一个将内容推送到树上的函数。它使用指定的信息添加一个新节点。空树被认为具有值为
NULL
的“根”
newNode
初始化为
NULL
并添加到树中,表示之前没有根。返回的根存储为
newNode
的新值。然后将另一个节点添加到新创建的树中,并将返回值存储在
newNode2
中。
newNode2
是否与
newNode
相同取决于
addListing()
如何工作,这并不重要,只要不返回
NULL
。@Malzahar\u很好,首先,它不会编译,因为你要声明两次变量。但我想我知道你的意思,它也会做同样的事情。唯一的区别是您将丢失树的头部,它保存在
newNode
中,因为您已经覆盖了它。