×

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

Traversal of binary tree

Traversal of binary tree: A node is visited only once in the traversal of the binary tree. There are three main types of traversal methods in the binary tree. In-order traversalPre-order...

3 minutes read.

Lowest Common Ancestor in a Binary Tree

The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we...

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

Array Data Structure

Data Structure Array: The array is a non-primitive and linear data structure that is a group of similar data items. That is, it can store only one type of data....

6 minutes read.

Primitive Data Structure in C

The data structure is a logical or mathematical model for organizing and structuring the main memory or elements. We can classify the data structures in two ways one is primitive, and...

10 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Operations of B++ tree

Insertion When we discuss the insertion operation in the B++ tree, this operation helps us in pushing a new element in the tree at any given place. In this case, the...

17 minutes read.

Program to calculate the area of the circumcircle of an equilateral triangle

You have given one value which represents the side of the equilateral triangle. You have to find out the area of the circumcircle. Let’s take an example - For the above...

3 minutes read.

Binary tree insertion

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. Here the name of the tree itself portrays the mechanism...

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.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

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

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

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.

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.

Insertion Sort in Data Structures

Insertion Sort in C++ Insertion sort is a sorting algorithm that, in each iteration, installs an unsorted element in its proper position Insertion sort operates in a similar way to how we...

3 minutes read.

Linear Search

Searching: In the data structure, searching is the process in which an element is searched in a list that satisfies one or more than one condition. Types of searching There are two...

4 minutes read.

Queue Implementation using stacks Data Structure

Queue Implementation using stacks In this problem, we have stack data structure which supports only push() and pop() operations. We are required to implement a queue data structure using the instances...

4 minutes read.