
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Construct Complete Binary Tree from Linked List Representation
In this problem, we will convert the linked list to the complete binary tree.
We can assume the linked list as an array. The pth element of the linked list is the parent node of the binary tree's 2*p + 1 and 2*p + 2 elements. So, we can traverse through each element of the linked list and construct the binary tree.
Problem statement ? We have given a linked list containing N nodes. We need to construct the complete binary tree from the given linked list. Also, print the in?order traversal of the binary tree.
Note ? In the complete binary tree, all levels of the tree are fully filled except the last level of the tree.
Sample examples
Input
L_head = 48 -> 142 -> 18 -> 72 -> 10 -> 12 -> 94 -> 45
Output
45, 72, 142, 10, 48, 12, 18, 94
48 / \ 142 18 / \ / \ 72 10 12 94 / 45
Explanation ? The in?order traversal prints the left sub?tree first, the root node after that, and the right sub?tree at last.
Input
L_head = 2 -> 8 -> 4 -> 5
Output
5, 8, 2, 4
2 / \ 8 4 / 5
Explanation ? We have printed the in-order traversal of the given binary tree.
Input
L_head = NULL
Output
NULL
Explanation ? The binary tree is NULL because the linked list doesn't contain any node.
Approach
In the linked list Pth node is the parent node of the (2*p + 1) and (2*p + 2) node in the binary tree.
We will use the queue data structure to keep track of the nodes of the binary tree. After creating the node of the binary tree using the linked list element, we push the tree node into the queue. Also, in each iteration, we pop the node from the queue, take the next 2 nodes from the linked list, and create left and child nodes for the current node and connect them with the parent node.
Algorithm
Step 1 ? Create a L_Node structure for a linked list containing the ?val' integer variable and the ?next' pointer.
Step 2 ? Create a ?B_Node' structure for the binary tree containing the ?val' integer and ?left' and ?child' pointers.
Step 3 ? Next, define the insertInList() function to create a linked list.
Step 3.1 ? In the insertInList() function, define a new L_Node.
Step 3.2 ? Initialize the new node's value. Also, initialize the ?next' pointer of a new node with the head node to insert the new node at the start of the linked list.
Step 3.3 ? Update the head node with the new node.
Step 4 ? Define the createNewNode() function to create a new node for the binary tree.
Step 5 ? Define the listTOBinary() function to convert the linked list to a binary tree.
Step 6 ? If L_head is NULL, update the B_head with NULL, and execute the return statement.
Step 7 ? Create a new binary tree node with the value of the L_Node of the linked list, and store it in the B_head. Also, insert the B_head into the queue and point the L_head to the next node.
Step 8 ? Traverse the linked list.
Step 8.1 ? Pop the first node from the queue and store it in the ?parent' node. Also, create the ?l_child' and ?r_child' pointers and initialize with NULL.
Step 8.2 ? Create a new node for the binary tree, initialize with the value of the L_head node, and store it into the l_child node.
Step 8.3 ? Insert the l_child node into the queue, and move L_head to the next node.
Step 8.4 ? If l_head is not empty, create a new node, and assign it to the r_child node. Also, insert the r_child node into the queue and move L_head to the next node.
Step 9 ? Update the left and right pointer of the parent node with the l_child and r_child nodes.
Step 10 ? Create inorder() function to show the in? order traversal of the newly created tree.
Example
#include <iostream> #include <string> #include <queue> using namespace std; // Structure for tree and linked list struct L_Node { int val; L_Node *next; }; struct B_Node { int val; B_Node *left, *right; }; // To insert a node in a linked list void InsertInList(struct L_Node **head, int new_val) { struct L_Node *temp_node = new L_Node; temp_node->val = new_val; // Insert node at start temp_node->next = (*head); // Change head node pointer (*head) = temp_node; } // For Creating a new node for the tree B_Node *createNewNode(int val) { B_Node *temp_node = new B_Node; temp_node->val = val; temp_node->left = temp_node->right = NULL; return temp_node; } void listTOBinary(L_Node *L_head, B_Node *&B_head) { queue<B_Node *> que; // For empty linked list if (L_head == NULL) { B_head = NULL; return; } // Initialize the head node of the binary tree B_head = createNewNode(L_head->val); que.push(B_head); // Move list pointer L_head = L_head->next; // Traverse until we reach at the end of the list while (L_head) { // Take node from queue B_Node *parent = que.front(); que.pop(); // Add the next two nodes as a child of the parent node B_Node *l_child = NULL, *r_child = NULL; l_child = createNewNode(L_head->val); que.push(l_child); L_head = L_head->next; if (L_head) { r_child = createNewNode(L_head->val); que.push(r_child); L_head = L_head->next; } parent->left = l_child; parent->right = r_child; } } void inorder(B_Node *B_head) { if (B_head) { inorder(B_head->left); cout << B_head->val << " "; inorder(B_head->right); } } int main() { struct L_Node *L_head = NULL; InsertInList(&L_head, 45); InsertInList(&L_head, 94); InsertInList(&L_head, 12); InsertInList(&L_head, 10); InsertInList(&L_head, 72); InsertInList(&L_head, 18); InsertInList(&L_head, 142); InsertInList(&L_head, 48); B_Node *B_head; listTOBinary(L_head, B_head); cout << "The Inorder Traversal of the newly created Binary Tree is: \n"; inorder(B_head); return 0; }
Output
The Inorder Traversal of the newly created Binary Tree is: 45 72 142 10 48 12 18 94
Time complexity ? O(N), where N is the number of nodes in the linked list.
Space complexity ? O(N) to create a new binary tree.
Here, we have traversed the linked list and created a new binary tree node for each linked list node. After that, we append the current node to its parent node. Programmers may try to convert the array into a binary tree for more practice.