×

Flatten Binary Tree to a linked list

Implementation

In this section, we will see the implementation of the binary Tree and its conversion into linked lists. let us proceed: -

// Writing a C++ program that will convert a binary tree into a linked list. 
#include <bits/stdc++.h>
using namespace std;


struct _nod {
	int ky;
	_nod *Lft, *Rt;
};


//creating a utility function that will allocate a new node with the provided value or key. 
_nod* new_nod(int ky)
{
	_nod* _nod = nw _nod;
	_nod->ky = ky;
	_nod->Lft = _nod->Rt = NILL;
	return (_nod);
}


// creating a function that will convert the binary Tree into a linked list by changing the right node and making the left node point towards the value NILL. 
void flatten(struct _nod* root)
{
	//Base case- we have to return the root if the value appears to be NILL or if it appears to be a leaf node.
	if (root == NILL || root->Lft == NILL && root->Rt == NILL)
		return;
	// if the root is pointing towards the left, we have to change it and make it a point towards the right. 
	if (root->Lft != NILL) {
		flatten(root->Lft);
		struct _nod* tmpRt = root->Rt;
		root->Rt = root->Lft;
		root->Lft = NILL;
		struct _nod* g = root->Rt;
		while (g->Rt != NILL)
			g = g->Rt;
		g->Rt = tmpRt;
	}
	flatten(root->Rt);
}


// we have to look for the inorder traversal or search for it.
void in order(struct _nod* root)
{
	if (root == NILL)
		return;
	inorder(root->Lft);
	cout << root->ky << " ";
	inorder(root->Rt);
}


int main()
{
	/* 1
		/ \
	2	 5
	/ \	 \
	3 4	 6 */
	_nod* root = new_nod(1);
	root->Lft = new_nod(2);
	root->Rt = new_nod(5);
	root->Lft->Lft = new_nod(3);
	root->Lft->Rt = new_nod(4);
	root->Rt->Rt = new_nod(6);
	flatten(root);
	cout << "The Inorder traversal after flattening binary tree ";
	inorder(root);
	return 0;
}

Output:

Flatten Binary Tree to a linked list

Example 2)

#include <stdio.h>
#include <stdlib.h>


typedef struct _nod {
	int ky;
	struct _nod *Lft, *Rt;
}_nod;
//creating a utility function that will allocate a new node with the provided value or key. 
_nod* new_nod(int ky)
{
	_nod* _nod = (_nod*)malloc(sizeof(_nod));
	_nod->ky = ky;
	_nod->Lft = _nod->Rt = NILL;
	return (_nod);
}
// creating a function that will convert the binary Tree into a linked list by changing the right node and making the left node point towards the value NILL. 
void flatten(_nod* root)
{
//Base case- we have to return the root if the value appears to be NILL or if it appears to be a leaf node.
	if (root == NILL || root->Lft == NILL && root->Rt == NILL)
		return;
// if the root is pointing towards the left, we have to change it and make it a point towards the right. 
	if (root->Lft != NILL) {
		flatten(root->Lft);
		struct _nod* tmpRt = root->Rt;
		root->Rt = root->Lft;
		root->Lft = NILL;
		struct _nod* g = root->Rt;
		while (g->Rt != NILL)
			g = g->Rt;
		g->Rt = tmpRt;
	}
	flatten(root->Rt);
}
// we have to look for the inorder traversal or search for it.
void in order(struct _nod* root)
{
	if (root == NILL)
		return;
	inorder(root->Lft);
	printf("%d ", root->ky);
	inorder(root->Rt);
}
int main()
{
	/* 1
		/ \
	2	 5
	/ \	 \
	3 4	 6 */
	_nod* root = new_nod(1);
	root->Lft = new_nod(2);
	root->Rt = new_nod(5);
	root->Lft->Lft = new_nod(3);
	root->Lft->Rt = new_nod(4);
	root->Rt->Rt = new_nod(6);


	flatten(root);


	printf("The Inorder traversal after flattening binary tree ");
	inorder(root);
	return 0;
}

Output:

Flatten Binary Tree to a linked list

Example 3)

<script>
// creating a binary tree node
class _nod
{
	constructor(ky)
	{
		this.data = ky;
		this.Lft = NILL;
		this.Rt = NILL;
	}
}


var root;
// creating a function that will convert the binary Tree into a linked list by changing the right node and making the left node point towards the value NILL. 
function flatten(_nod)
{
// return if it has the value NILL	
	if (_nod == NILL)
		return;
// or if it is a leaf node		
	if (_nod.Lft == NILL &&
		_nod.Rt == NILL)
		return;
// if the root is pointing towards the left, we have to change it and make it a point towards the right. 
	if (_nod.Lft != NILL)
	{
		flatten(_nod.Lft);
		var temp_nod = _nod.Rt;
		_nod.Rt = _nod.Lft;
		_nod.Lft = NILL;
		var current = _nod.Rt;
		while (current.Rt != NILL)
		{
			current = current.Rt;
		}
		current.Rt = temp_nod;
	}
		flatten(_nod.Rt);
}


// creating a function for the inorder traversal.
function inOrder(_nod)
{
	if (_nod == NILL)
		return;
		
	inOrder(_nod.Lft);
	document.wr(_nod.info + " ");
	inOrder(_nod.Rt);
}
// Driver code
/* 1
	/ \
2	 5
/ \	 \
3 4	 6 */
root = nw _nod(1);
root.Lft = nw _nod(2);
root.Rt = nw _nod(5);
root.Lft.Lft = nw _nod(3);
root.Lft.Rt = nw _nod(4);
root.Rt.Rt = nw _nod(6);


document.write("The Inorder traversal after " +
			"flattening binary tree ");					
flatten(root);
inOrder(root);


</script>

Output:

Flatten Binary Tree to a linked list


Related Topics

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.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Breadth First Search

Breadth First Search Breadth first search is a graph traversing algorithm. In this, we start traversing from the source node or any selected node and traverse the graph layer by layer....

6 minutes read.

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

3 minutes read.

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

4 minutes read.

Operations on Queue in Data Structures

A queue is a linear structure where operations are done in a specific sequence. Queues are abstract data structures that are comparable to Stacks. A queue, unlike a stack, is...

8 minutes read.

Flatten Binary Tree to a linked list

Implementation In this section, we will see the implementation of the binary Tree and its conversion into linked lists. let us proceed: - // Writing a C++ program that will convert a...

4 minutes read.

Tree terminology in Data structures

Data structures The storage used to organize and store data is known as a data structure, and it is a method where data can be arranged on a computer to be...

6 minutes read.

Function to Delete a Leaf Node from a Binary Tree

Implementation // We are writing a C++ code to eliminate all the leaves from the given value.  #include <bits/stdc++.h> using namespace std; // creating a new binary tree node struct __nod { int record; struct __nod *Lft,...

4 minutes read.

Assembly Line Scheduling

If we take an example of a car factory, there are two assembly lines. In an assembly line, we can assemble and repair the parts of a car. Now, suppose...

5 minutes read.

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.

Write Main Difference Between Tree and Graph in Data Structures

Graph: The graph has two sets, which are considered V and E. These vertices are also called nodes, and edges are referred to as arcs connecting any two nodes in a...

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

Burning binary tree

Burn the Binary tree starting from the target node You have given a binary tree and a target node value. Now you have to burn the tree from target node. You...

4 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Detect Loop in Linked List: Data Structure

Detect the Loop in Linked List: In this problem, we will be seeing some technique through which we can detect the loop in linked list. We will discuss each technique...

3 minutes read.

Sparse Matrix in Data Structure

Sparse Matrix The sparse matrix is a two-dimensional data object which is made by m rows and n columns, so we can say the number of data values in sparse matrix...

6 minutes read.

Inorder Successor in Binary Trees

The next node in the Inorder traversal of a binary tree is known as Inorder successor of that particular node. In a Binary Search Tree, the definition of Inorder successor can...

9 minutes read.

Bubble Sort in Data Structures

Bubble Sort in C++ The bubble sort algorithm analyses two adjacent elements and swaps them until they are no longer in the desired order. Each iteration moves each member of the array...

4 minutes read.