BST build tree double pointers

前端 未结 1 985
遥遥无期
遥遥无期 2021-01-07 03:16

I am unsure how to set a pointer to a pointer to build a tree. Like once I have traveled to a leaf and call insert, how should I insert another element calling insert with

相关标签:
1条回答
  • 2021-01-07 04:08

    Say you created a function addNodeToTree(TreeNode *root, int data), pass the root node, and data to it as argument.

    Now inside this function, simply create another variable say TreeNode *current = root which will help us basically to traverse the tree and place the node at its respective position, and TreeNode *newNode = NULL(this will become the new node, which is to be inserted).

    Now before moving ahead, to actually place the node, we will first check, if the root is not null, i.e. the tree is EMPTY. For that, we will test:

    if (root == NULL)
    {
        newNode = malloc(sizeof(*newNode)); // else we can make a function for this 
                                            // thingy too. Creating a function too,
                                            // for you to look at.
        root = newNode;
    }
    

    If the tree is not EMPTY, i.e. it contains a node already, then we will traverse the tree to find the place, where to put the new node. So the else part, will be like:

    else
    {
        parent = current = root;
        // This loop will actually help us, find the `parent`, 
        // of the `newNode`(which is to be inserted)
        while (current != NULL)
        {
            parent = current;
            if (current->data > data)
                current = current->left;
            else if (current->data < data)
                current = current->right;
        }
        // Now once, we have found, the node, under which the
        // newNode will be placed, now we have to check, whether
        // newNode will become a `left child/right child` of the 
        // parent.
        newNode = getNewNode(data);
        if (parent->data > data)
            parent->left = newNode;
        else if (parent->data < data)
            parent->right = newNode;
    
    
        return root;
    }
    
    TreeNode * getNewNode(int data)
    {
        TreeNode *newNode = malloc(sizeof(*newNode));
        if (newNode != NULL)
        {
            newNode->data = data;
            newNode->left = NULL;
            newNode->right = NULL;
        }
    
        return newNode;
    }
    

    Now the newNode has been inserted, and you can simply traverse in any order to see the Tree.

    EDIT 1:

    Here is one working example, just see if this makes sense. Else please do ask any question, that might may arise.

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct TREENODE
    {
        int data;
        struct TREENODE *left;
        struct TREENODE *right;
    }TreeNode;
    
    void display(TreeNode *node)
    {
        printf("*********************************\n");
        printf("Address of Node: %p\n", node);
        printf("Data: %d\n", node->data);
        printf("Left Child: %p\n", node->left);
        printf("Right Child: %p\n", node->right);
        printf("*********************************\n");
    }
    
    TreeNode * getNewNode(int data)
    {
        TreeNode *newNode = malloc(sizeof(*newNode));
        if (newNode != NULL)
        {
            newNode->data = data;
            newNode->left = NULL;
            newNode->right = NULL;
        }
    
        return newNode;
    }
    
    int getIntData(char *message)
    {
        int value = 0;
        char buffer[BUFSIZ] = {'\0'};
        fputs(message, stdout);
        fgets(buffer, sizeof(buffer), stdin);
        sscanf(buffer, "%d", &value);
    
        return value;
    }
    
    TreeNode * addNodeToTree(TreeNode *root, int data)
    {
        TreeNode *current = root, *parent = root;
        TreeNode *newNode = getNewNode(data);
    
        if (root == NULL)
        {
            root = newNode;
        }
        else
        {
            while(current != NULL)
            {
                parent = current;
                if (current->data > data)
                    current = current->left;
                else if (current->data < data)
                    current = current->right;
            }
    
            if (parent->data > data)
                parent->left = newNode;
            else if (parent->data < data)
                parent->right = newNode;
        }
    
        return root;
    }
    
    void inOrderTraversal(TreeNode *root)
    {
        if (root != NULL)
        {
            inOrderTraversal(root->left);
            display(root);
            inOrderTraversal(root->right);
        }
    }
    
    int main(void)
    {
        TreeNode *root = NULL;
        int data = 0;
        data = getIntData("Enter Data: ");
        root = addNodeToTree(root, data);
        data = getIntData("Enter Data: ");
        root = addNodeToTree(root, data);
        data = getIntData("Enter Data: ");
        root = addNodeToTree(root, data);
        inOrderTraversal(root);
    
        return EXIT_SUCCESS;
    }
    

    EDIT 2:

    To make addNodeToTree(...), implement pointer to a pointer, one simply needs to change the function as:

    void addNodeToTree(TreeNode **root, int data)
    {
        TreeNode *current = *root;
        TreeNode *parent = *root;
        TreeNode *newNode = getNewNode(data);
    
        if (*root == NULL)
        {
            *root = newNode;
        }
        else
        {
            // same as before, just don't return anythingy, from the function.
            // As the function uses pointer to a pointer, hence whatever changes
            // are done, here will be reciprocated in the main function automatically
        }
    
        // no need to return anythingy
    }
    

    And the call from main will now look like:

    int main(void)
    {
        TreeNode *root = NULL;
        int data = 0;
        data = getIntData("Enter Data: ");
        addNodeToTree(&root, data);
        // Just see the call to addNodeToTree(...), the rest is same, as before
    }
    

    EDIT 3:

    In order to take elements from an array instead of adding them directly to tree, just change the main method to this form:

    int main(void)
    {
        TreeNode *root = NULL;
        int element[5] = {19, 11, 5, 28, 25};
        int size = sizeof(element) / sizeof(element[0]);
        int counter = 0;
        for (counter = 0; counter < size; ++counter)
        {
            addNodeToTree(&root, element[counter]);
        }
        inOrderTraversal(root);
    
        return EXIT_SUCCESS;
    }
    
    0 讨论(0)
提交回复
热议问题