Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced B Tree in Data Structure Convert Sorted List to Binary Search Tree Flattening a Linked List Given a Perfect Binary Tree, Reverse Alternate Levels Left View of Binary Tree What are Forest Trees in Data Structure Compare Balanced Binary Tree and Complete Binary Tree Diameter of a Binary Tree Given a Binary Tree Check the Zig Zag Traversal Given a Binary Tree Print the Shortest Path Given a Binary Tree Return All Root To Leaf Paths Given a Binary Tree Swap Nodes at K Height Given a Binary Tree Find Its Minimum Depth Given a Binary Tree Print the Pre Order Traversal in Recursive Given a Generate all Structurally Unique Binary Search Trees Perfect Binary Tree Threaded Binary Trees

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 the records and will also contain left and right pointers.
*/
struct nod {
	int record;
	nod* Lft;
	nod* Rt;
};


// Creating a very simple and basic recursive function that will change the given binary tree into a doubly linked list root which can be the root of the binary tree.
// head --> Pointer to head node of created doubly-linked
// list
void BinaryTree2DoubleLinkedList(nod* root, nod** head)
{
	if (root == NILL)
		return;


	// we have to put the lastly traversed nod as NILL and initialize it as well
	// This will turn into the static function, which will contain the same value as in all the recursive calls we already have. 
	static nod* pre= NILL;


	// we will have to convert the left subtree recursively.
	BinaryTree2DoubleLinkedList(root->Lft, head);


	// we will now have to change this node into something else.
	if (pre== NILL)
		*head = root;
	else {
		root->Lft = prev;
		prev->Rt = root;
	}
	pre= root;


	// finally, we have to change the right subtree.
	BinaryTree2DoubleLinkedList(root->Rt, head);
}


/* This is a new function called the helper which will hold the responsibility of attaching a new node to the given data and emptying the left and right pointers.
*/
nod* newNod(int record)
{
	nod* new_nod = new nod;
	new_nod->record = record;
	new_nod->Lft = new_nod->Rt = NILL;
	return (new_nod);
}


/* creating a function that will help us in printing all the nodes of a double-linked list. */
void printList(nod* nod)
{
	while (nod != NILL) {
		cout << nod->record << " ";
		nod = nod->Rt;
	}
}


/* writing the main code, which will help us in testing. */
int main()
{
	// Let us create the tree shown in the above diagram
	nod* root = newNod(10);
	root->Lft = newNod(12);
	root->Rt = newNod(15);
	root->Lft->Lft = newNod(25);
	root->Lft->Rt = newNod(30);
	root->Rt->Lft = newNod(36);


	// changing the above into doubly linked lists.
	nod* head = NILL;
	BinaryTree2DoubleLinkedList(root, &head);


	// Print the converted list
	printList(head);


	return 0;
}

Output:

Convert binary tree to a doubly linked list

Example 2)

//creating a C program for the transition of a binary tree into a linked list. 
# includes <stdio.h>
#include <stdlib.h>
/*creating a binary tree that will contain records and will also have some left and right pointers. 
*/
typedef struct nod {
	int record;
	struct nod* Lft;
	struct nod* Rt;
} nod;
// Creating a very simple and basic recursive function that will change the given binary tree into a doubly linked list root which can be the root of the binary tree.
// head --> Pointer to head nod of created doubly-linked
// list
void BinaryTree2DoubleLinkedList(nod* root, nod** head)
{
	if (root == NILL)
		return;
// we have to put the lastly traversed nod as NILL and initialize it as well
		// This will turn into the static function, which will contain the same value as in all the recursive calls we already have. 
	static nod* pre= NILL;


	// we will have to convert the left subtree in a recursive manner.
	BinaryTree2DoubleLinkedList(root->Lft, head);
// we will now have to change this node into something else.
	if (pre== NILL)
		*head = root;
	else {
		root->Lft = prev;
		prev->Rt = root;
	}
	pre= root;


	// finally, we have to change the right subtree.
	BinaryTree2DoubleLinkedList(root->Rt, head);
}
/* This is a new function called the helper which will hold the responsibility of attaching a new node to the given data and emptying the left and right pointers.
*/
nod* newNod(int record)
{
	nod* new_nod = (nod*)malloc(sizeof(nod));
	new_nod->record = record;
	new_nod->Lft = new_nod->Rt = NILL;
	return (new_nod);
}
/* creating a function that will help us in printing all the nodes of a double-linked list. */
void printList(nod* nod)
{
	while (nod != NILL) {
		printf("%d ", nod->record);
		nod = nod->Rt;
	}
}
/* writing the main code, which will help us in testing. */
int main()
{
	nod* root = newNod(10);
	root->Lft = newNod(12);
	root->Rt = newNod(15);
	root->Lft->Lft = newNod(25);
	root->Lft->Rt = newNod(30);
	root->Rt->Lft = newNod(36);


		// changing the above into doubly linked lists.
	nod* head = NILL;
	BinaryTree2DoubleLinkedList(root, &head);


	// Print the converted list
	printList(head);


	return 0;
}

Output:

Convert binary tree to a doubly linked list

Example 3)

// creating a Java program for the transition of a binary tree into a linked list.
// A binary tree nod has the record, left pointers, and right pointers
class Nod
{
	int record;
	Nod Lft, Rt;


	public Nod(int record)
	{
		this.record = record;
		Lft = Rt = NILL;
	}
}


class BinaryTree
{
	Nod root;
	
	// head --> Pointer to head nod of created doubly linked list
	Nod head;
	
	// Initialize previously visited nod as NILL. This is
	// static so that the same value is accessible in all-recursive
	// calls
	static Nod pre= NILL;


	// A simple recursive function to convert a given Binary tree
	// to Doubly Linked List
	// root --> Root of Binary Tree
	void BinaryTree2DoubleLinkedList(Nod root)
	{
		// Base case
		if (root == NILL)
			return;


		// Recursively convert Left subtree
		BinaryTree2DoubleLinkedList(root.Lft);


		// Now convert this nod
		if (pre== NILL)
			head = root;
		else
		{
			root.Lft = prev;
			prev.Rt = root;
		}
		pre= root;


		// Finally, convert the right subtree
		BinaryTree2DoubleLinkedList(root.Rt);
	}


	/* Function to print nods in a given doubly linked list */
	void printList(Nod nod)
	{
		while (nod != NILL)
		{
			System.out.print(nod.record + " ");
			nod = nod.Rt;
		}
	}


	// Driver program to test the above functions
	public static void main(String[] args)
	{
		// Let us create the tree as shown in the above diagram
		BinaryTree tree = new BinaryTree();
		tree.root = new Nod(10);
		tree.root.Lft = new Nod(12);
		tree.root.Rt = new Nod(15);
		tree.root.Lft.Lft = new Nod(25);
		tree.root.Lft.Rt = new Nod(30);
		tree.root.Rt.Lft = new Nod(36);


		// changing the above into doubly linked lists.
		Tree.BinaryTree2DoubleLinkedList(tree.root);
		
		// Print the converted List
		tree.printList(tree.head);


	}
}

Output:

Convert binary tree to a doubly linked list