> For the complete documentation index, see [llms.txt](https://nitinranganath.gitbook.io/data-structures/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nitinranganath.gitbook.io/data-structures/binary-tree/creating-a-binary-tree-using-queue.md).

# Creating a Binary Tree using Queue

**Note : The below code assumes that a queue of type pointer to Node is already created and initialised.**&#x20;

```c
struct Node {
    int data;
    struct Node *left;
    struct Node *right;
}

struct Node *root = NULL;

void createTree(int noOfNodes) {

    // Pointer to keep track of current node and for new nodes
    struct Node *ptr, *newNode;
    int data;
    
    // A queue to store addresses of each node
    struct Queue q;
    create(&q, noOfNodes);
    
    // Create the root node and make its child NULL by default
    printf("Enter the value of root\n");
    scanf("%d", &data);
    root = (struct Node *)malloc(sizeof(struct Node));
    root -> data = data;
    root -> left = root -> right = NULL;
    // Enqueue the root to queue
    
    // Repeat until no node is to be inserted further
    while (isEmpty(q)) {
        
        // Get address of current node
        ptr = dequque(&q);
        
        // Inserting left child node
        printf("Enter the left child of %d:\n", ptr -> data);
        scanf("%d", &data);
        if (data != -1) {
            newNode = (struct Node *)malloc(sizeof(struct Node));
            newNode -> data = data;
            newNode -> left = newNode -> right = NULL;
            ptr -> left = newNode;
        }
        
        // Inserting right child node
        printf("Enter the right child of %d:\n", ptr -> data);
        scanf("%d", &data);
        if (data != -1) {
            newNode = (struct Node *)malloc(sizeof(struct Node));
            newNode -> data = data;
            newNode -> left = newNode -> right = NULL;
            ptr -> right = newNode;
        }
    }

}
```

Contributed by Nitin Ranganath


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://nitinranganath.gitbook.io/data-structures/binary-tree/creating-a-binary-tree-using-queue.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
