×

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

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

What Should We Learn First? Trees or Graphs in Data Structures

A data structure is a database used to store and manage data and optimize and manage computing resources. A data structure is a form used intelligently and quickly to store,...

6 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.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Data Structures Algorithms

What is an Algorithm? An algorithm is a sequence of steps used to complete a job or get a desired result. It is similar to programming building elements that let cell...

4 minutes read.

Sum of Nodes in a Binary Tree

In this article, we will see the sample problems that will help us understand the concept and summation of all the nodes in the binary tree. Implementation /* creating a program that...

4 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

Lowest common ancestor in a binary search tree

Suppose you have given two values of nodes in a binary search tree. You have to find out the lowest common ancestor between the nodes. Let’s take an example tree- For the...

4 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

Fundamental of Algorithms

An algorithm is a part of any programming solution or coding. If we have to make a solution then first we have to think of a clear idea about the...

13 minutes read.

Convert binary tree to a doubly linked list

Implementation //creating a C++ program for the transition of a binary tree into a linked list. #include <iostream> using namespace std; /* Firstly, let’s create a binary tree that will help us in setting...

4 minutes read.

What is a 2-3 Tree in Data Structure?

Tree Data structure The information about the tree is self-explanatory. Trees are ordered and, therefore, not linear. But they are actually designed differently. Tree A node-based data model that represents and...

5 minutes read.

Boruvkas algorithm

This algorithm is used for finding minimum spanning tree from a weighted graph. Like prim’s and kruskal’s algorithm it is also a greedy algorithm. Note:What is the minimum spanning tree?We know...

4 minutes read.

Serialize and Deserialize Binary Trees

In order to save a tree in a file that can later be restored, serialisation is used. The tree's structure must be preserved. Deserialization involves reading a tree from a...

4 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Treap data structure

In this article, we will discuss the treap data structure. The word treap is a combination of 'tree' and 'heap'. So, treap data structure is a combination of a heap...

8 minutes read.

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find...

2 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.