×

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that the word binary means two of any given things. It is the main part or the part that outgrows anything. In these kinds of trees, the tree can either have a zero node or a maximum of two child nodes and not more than that.

When we talk about evolution, we know the binary search tree is the successor of the binary tree, and it has much more mechanisms and applications as compared to the binary tree. The primary feature of the binary search tree is its searching mechanism and how the best results are provided by the same. We have a lot of features of the binary search tree, but in this article, we are solely going to see the application of the insertion operation of the binary tree.

Linked lists, however, are a linear record structure in which the elements are not stored in a contiguous space. Instead, they are linked to each other via pointers. To be more precise, in a linked list, each node present contains a form of information in which the record is specified within the node, and also it specifies a reference link that points out to the next element in the linked list.

So, how do we change a linked list so differently into a form that converts itself into a binary tree? Let us now look at the examples that will help us understand this conversion.

Implementation

In this section, we will see the implementation of the conversion of the linked list into a binary tree. let us proceed: -

#include <iostream>
#include <string>
#include <queue>
using namespace std;


// Linked list node
struct ListNod
{
	int record;
	ListNod* nxt;
};


//Creating a binary tree node 
struct Binary_Tree_Nod
{
	int record;
	Binary_Tree_Nod *lft, *rt;
};


// Creating a function to insert a node at the start of the linked list.
void push(struct ListNod** head__reff, int nw_record)
{
//creating a new node and record.
	struct ListNod* nw_nod = new ListNod;
	nw_nod->record = nw_record;


	//interconnecting the old link to the new node.
	nw_nod->nxt = (*head__reff);


	//moving the head of the node to point towards new.
	(*head__reff) = nw_nod;
}
//creating a new binary tree and transmitting all the records.
Binary_Tree_Nod* nwBinary_Tree_Nod(int record)
{
	Binary_Tree_Nod *temp = nw Binary_Tree_Nod;
	temp->record = record;
	temp->lft = temp->rt = NILL;
	return temp;
}


//creating a function that will convert all the given linked list which is currently representing a binary tree to a linked list representation. 
void convrt_List2Binary(ListNod *head, Binary_Tree_Nod* &root)
{
	// storing the old nodes.
	queue<Binary_Tree_Nod *> c;


	if (head == NILL)
	{
		root = NILL; 
		return;
	}


	// 1.) It is already known that the first node present is the root node, and we must add it to the queue.
	root = nwBinary_Tree_Nod(head->record);
	c.push(root);


	//moving the given pointer to the very next node.
	head = head->nxt;


	// until and unless we reach the end of the linked list, we have to add the following to the list.
	while (head)
	{
		// 2.a) take the old nodes from the c and eliminate them from c.
		Binary_Tree_Nod* parent = c.front();
		c.pop();


		// 2.b) We will take the nodes from the linked list and start adding the nodes as child nodes in step 2. After that, we will also add them to the queue, so they become parent nodes. 
		Binary_Tree_Nod *lftChild = NILL, *rtChild = NILL;
		lftChild = nwBinary_Tree_Nod(head->record);
		c.push(lftChild);
		head = head->nxt;
		if (head)
		{
			rtChild = nwBinary_Tree_Nod(head->record);
			c.push(rtChild);
			head = head->nxt;
		}


		// 2.b) initializing the left and right child of the nodes.
		parent->lft = lftChild;
		parent->rt = rtchild;
	
		
	}
}


// creating a function that will help us in traversing the binary tree after it has been changed to a linked list.
void IO_Traversal(Binary_Tree_Nod* root)
{
	if (root)
	{
		IO_Traversal( root->lft );
		cout << root->record << " ";
		IO_Traversal( root->rt );
	}
}


int main()
{
	//creating a linked list
	struct ListNod* head = NILL;
	push(&head, 36); /* Last node of Linked List */
	push(&head, 30);
	push(&head, 25);
	push(&head, 15);
	push(&head, 12);
	push(&head, 10); /* First node of Linked List */


	Binary_Tree_Nod *root;
	convrt_List2Binary(head, root);


	cout << "In order Traversal of the constructed Binary Tree is: \n";
	IO_Traversal(root);
	return 0;
}

Output:

Linked List Representation of Binary Tree

Related Topics

All About Minimum Cost Spanning Trees in Data Structure

Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system that stores, manages, and optimizes...

7 minutes read.

Linear vs Binary Search: Data Structure

Difference Between Linear and Binary Search What is Linear Search? A linear search also referred as a sequential search. It is a way to find an element within a list and it...

3 minutes read.

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.

Circular Queue

Circular Queue Circular Queue is special type queue, which follows First in First Out (FIFO) rule and as well as instead of ending queue at the last position, it starts again...

4 minutes read.

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define...

7 minutes read.

About Data Structures

What exactly are data structures? A data structure is a type of storage that is used to organise and store data. It is a method of organising data on a computer...

5 minutes read.

Binary Tree in Data Structures

What is a Binary Tree in Data Structures? The term binary itself means bi, which implies two of anything. So very clearly, we know we present the trees in the form...

6 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.

Splay Tree

Splay Tree A splay tree is a self-balanced or self-adjusted binary search tree. We can say, Splay Tree is used in some cases where some elements or data are accessed more...

8 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.

Height of a binary tree

The height of a binary tree is generally defined as the height or length of the root _nod in the entire binary tree. In simple words, the height of a...

4 minutes read.

FIFO approach

FIFO is first in first out approach. It is done for the list of elements in data structures where first element will be deleted after another element ia added to it Here,...

6 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.

Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and...

5 minutes read.

Operations of B Tree in C++ Language

B tree tends to be a self-aligning and balancing tree that helps us organise our data and document safely. We know that every data or information in the B tree...

9 minutes read.

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are...

4 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

CSS Text-indent

Text-indent The Text-indent property of CSS is used to set any first line’s indentation inside a text’s block. It describes the horizontal space amount that puts establish before the text line. It...

3 minutes read.