×

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

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.

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

Counts the number of times a given element occurs in a Linked List

Counts the number of times a given element occurs in a Linked List This article will explain how we can count the occurrences of a particular element in a list. Here,...

3 minutes read.

Difference Between Linear and Non Linear Data Structures

Data Structure A data structure is a data object together with the relationships between the instances and the individual elements that compose an instance. These relationships are defined by the operations...

5 minutes read.

Right side view of binary tree

The right view of the binary tree is generally known to be that side viewed from the right direction of the point of view. To be more precise, the right-side...

8 minutes read.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method. What is recursion? Defining anything in terms...

7 minutes read.

Threaded Binary Trees

Introduction Threaded Binary Trees (TBTs) are an enhancement of normal binary trees intended for in-order traversal only. This means that this data structure is developed with the objective of making the...

12 minutes read.

What is an AVL Tree in Data Structure?

AVL tree stands for (Adelson, Velskii, & Landis Tree) Data structure Data management is called database management. A data model is a system used to store, manage, and optimize computer resources. Data...

4 minutes read.

Difference between Structured and Object-Oriented Analysis

Analysis means observing and collecting relevant information about the structure of something or the basic details of a system's requirements. Structured and Object Oriented Analysis are both widely used in...

2 minutes read.

Merge Conflicts and ways to handle them

Merge Conflicts Whenever dealing with the Git merge operations, conflicts will be the frequently occurred. When more than two developers work on the same file on different systems using Git, they...

4 minutes read.

Intersection Point in Y Shaped Linked Lists in Java

Intersection Point in Y Shaped Linked Lists in Java In this article, we are going to see how to find the intersection point in a Y-shaped linked list. Method 1: We need to...

4 minutes read.

Depth of binary tree

We all know that a binary tree is a kind of tree that helps us maintain the order and balance of the tree. It is a type of tree in...

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

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.

Box Stacking Problem

Stacking of boxes depending on their base You have been given n different boxes. These boxes will have different heights, widths, and depths. You have to stack all these boxes in...

4 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

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

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the...

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