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

时间:2016-04-10 17:50:27

标签: 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 *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;
}

我有这个,我在网上找到了这个实现,但我不确定它的含义。有人可以开导我吗?谢谢。

0 个答案:

没有答案