2

I want to store the binary tree in the array with null values at the missing nodes.

For eg:

Input:

      1
     / \
    2   3
   / \   \
  4   5   6

Output:{1,2,3,4,5,0,6}

I have tried linear traversal of the binary tree in the array, but I want null in the missing nodes positions of the tree.

std::vector< int > levelorder( tree *root){
    queue<tree*> q; 
    tree* temp;
    q.push(root);
    while(!q.empty()){
        temp=q.front();
        q.pop();
        arr.push_back(temp->data);
        if(temp->left && temp->right)
        {
            q.push(temp->left);
            q.push(temp->right);
        }
        else if(temp->left && !temp->right)
        {
            q.push(temp->left);
            arr.insert(0);
        }
        else if(temp->right && !temp->left)
        {
            q.push(temp->right);
            arr.push_back(0);
        }

    }
    return arr;
}

int main() 
{ 

    tree *root = newNode(1);  
    root->left = newNode(2);  
    root->right = newNode(3);  
    root->left->left = newNode(4);  
    root->left->right = newNode(5);
    root->right->right = newNode(6);

    cout<<"Level Order traversal of binary tree is :"<<endl; 
    levelorder(root);
    for(int i =0; i<arr.size(); i++)
    {
        cout<< arr[i]<<" ";
    } 

    return 0; 
}

I am getting output:{1,2,3,0,4,5,6} But I want the output as: {1,2,3,4,5,0,6}

5
  • I think you'll have to push nulls into q, and when you get a null from q add a zero to the array then. The non-null numbers on the level get added to arr when the q entry is processed, not when they are queued. Commented May 7, 2019 at 14:08
  • @Rup can you please edit in my code and send it back to me. Commented May 7, 2019 at 14:15
  • 1
    In your example, only a leaf is missing. What do you expect if a whole branch is missing? Commented May 7, 2019 at 14:19
  • if in my example 6 is also missing then I want output as:{1,2,3,4,5} Commented May 7, 2019 at 14:23
  • I don't care about missing, I care about only missing node Commented May 7, 2019 at 14:34

2 Answers 2

3

Algorithm

The following code supports missing sub-trees and stop as soon as no meaningful nodes are present:

std::vector<int> levelorder(tree *root){
    int null_in_queue = 0;
    std::queue<tree*> q;
    std::vector<int> arr;
    q.push(root);
    while(q.size() != null_in_queue){
        tree* temp=q.front();
        q.pop();
        if(!temp)
        {
           arr.push_back(0);
           q.push(nullptr);
           q.push(nullptr);
           null_in_queue++; // One was removed, two were added
        }
        else
        {
            arr.push_back(temp->data);
            q.push(temp->left);
            q.push(temp->right);
            if (!temp->left) null_in_queue++;
            if (!temp->right) null_in_queue++;
        }
    }
    return arr;
}

It pushes virtual nodes (nullptr) in the queue and count the number of virtual nodes. When only virtual nodes are remaining, it terminates.

Complexity

The time and memory complexity of this algorithm is O(n), where n is the size of the output array. The size of this array is at most the node count (minus one) of a full tree up to the deepest node. For a tree of depth d, the algorithm has complexity O(2^d).

Details: The code in the loop is constant (amortized) and the q vector contains nodes and virtual nodes, which contains at most 2^(d+1) nodes (an additional row of virtual nodes will be added before the algorithm finishes). This means that the whole algorithm will execute in O(2^(d+1)) ~ O(2*2^d) ~ O(2^d).

Sign up to request clarification or add additional context in comments.

4 Comments

An error is coming while pushing the nullptr,Error is: 'nullptr' was not declared in this scope
Your compiler may not support the nullptr keyword (it appeared in C++11). Replace nullptr with 0.
what will be the time complexity for this function
@UtkarshGoyal I added a paragraph describing the complexity of the algorithm.
1

Assuming that the empty nodes are properly initialized to nullptr:

while(!q.empty()){
    tree* temp=q.front();
    q.pop();
    if(!temp)
       arr.push_back(0);
    else if (temp->left || temp->right)//Only recurse into internal nodes, not leafs
    {
        arr.push_back(temp->data);
        q.push(temp->left);
        q.push(temp->right);
    }
}

I would like to note that this won't print missing subtrees, only missing leafs. Please specify if it should.

2 Comments

the output is {1,2,3,4,5,0,6,0,0,0,0,0,0} why these extra zeros are coming in the end
Because those nodes in the lowest level are also missing leafs, both of them. This is not probably what you wanted, sorry ,will edit the code shortly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.