# Convert binary tree to threaded binary tree

Here given code implementation process.

``````/*
C Program
+ Convert left-right representation of a binary tree to down-right
*/
#include <stdio.h>
#include <stdlib.h>
//structure of Binary Tree node
struct Node
{
int data;
struct Node*left,*right;
};

//Create a binary tree nodes and node fields (data,pointer)
//And returning the reference of newly nodes

struct Node* newNode(int data)
{
//create dynamic memory to new binary tree node
struct Node*new_node=(struct Node*)malloc(sizeof(struct Node));
if(new_node!=NULL)
{
//set data and pointer values
new_node->data=data;
new_node->left=NULL; //Initially node left-pointer is NULL
new_node->right=NULL;//Initially node right-pointer is NULL
}else
{
printf("Memory Overflow\n");
exit(0); //Terminate program execution
}
//return reference
return new_node;

}

void inorder(struct Node *root)
{
if (root != NULL)
{

inorder(root->left);
printf("%3d",root->data);
inorder(root->right);

}
}
}
}
}
}
{
}

{
}

}
}
int main(){

struct Node*root=NULL;
/*   Make A Binary Tree
-----------------------
1
/  \
2    3
/    /  \
4    5    6
\
7
/
8
*/
root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->right->right = newNode(6);
root->right->left = newNode(5);
root->left->left = newNode(4);
root->left->left->right = newNode(7);
root->left->left->right->left = newNode(8);
inorder(root);

return 0;
}```
```

#### Output

``````  4  8  7  2  1  5  3  6
4  8  7  2  1  5  3  6``````
``````/*
C++ Program
Convert binary tree to threaded binary tree
*/
#include <iostream>

using namespace std;
class Node {
public:
int data;
Node *left;
Node *right;
Node(int value) {
this->data = value;
this->left = NULL;
this->right = NULL;
}
};
class BinaryTree {
public:
Node *root;
BinaryTree() {
this->root = NULL;
}
cout << "  " << head->data;
}
}
//Convert Binary tree to Threaded Binary tree
}
}
}
}
}
cout << "  " << head->data;

}

}
}
};

int main() {
BinaryTree obj;
/*   Make A Binary Tree
-----------------------
1
/  \
2    3
/    /  \
4    5    6
\
7
/
8
*/
obj.root = new Node(1);
obj.root->left = new Node(2);
obj.root->right = new Node(3);
obj.root->right->right = new Node(6);
obj.root->right->left = new Node(5);
obj.root->left->left = new Node(4);
obj.root->left->left->right = new Node(7);
obj.root->left->left->right->left = new Node(8);
obj.inorder(obj.root);

cout << "\n Threaded inorder \n";
return 0;
}```
```

#### Output

``````  4  8  7  2  1  5  3  6
4  8  7  2  1  5  3  6``````
``````/*
Java Program
Convert binary tree to threaded binary tree
//using recursion
*/

//Structure of Binary Tree node
class Node
{

public int data;
public Node left;
public Node right;
//make a tree node
public Node(int value)
{
//Assign field values
data=value;
left=null;
right=null;
}
};

public class BinaryTree
{

public Node root;

public BinaryTree(){
//Set initial tree root to null
root=null;
}
//Display tree element inorder form
{

{
//Print node value
}

}
//Converting a given binary to its threaded binary tree
{
{

{
}
{
}
}
}
{
{

{
}
{
}
}
}
public static void main(String[] args)
{
//Make object of Binary Tree
BinaryTree obj=new BinaryTree();

/*   Make A Binary Tree
-----------------------
1
/  \
2    3
/    /  \
4    5    6
\
7
/
8
*/
obj.root               = new Node(1);
obj.root.left          = new Node(2);
obj.root.right         = new Node(3);
obj.root.right.right   = new Node(6);
obj.root.right.left    = new Node(5);
obj.root.left.left     = new Node(4);
obj.root.left.left.right = new Node(7);

obj.root.left.left.right.left = new Node(8);
//Display Tree elements
obj.inorder(obj.root);

}
}```
```

#### Output

``````  4  8  7  2  1  5  3  6
4  8  7  2  1  5  3  6``````
``````/*
C# Program
Convert binary tree to threaded binary tree
//using recursion
*/
using System;
//Structure of Binary Tree node
public class Node
{

public int data;
public Node left;
public Node right;
//make a tree node
public Node(int value)
{
//Assign field values
data=value;
left=null;
right=null;
}
};

public class BinaryTree
{

public Node root;

public BinaryTree(){
//Set initial tree root to null
root=null;
}
//Display tree element inorder form
{

{
//Print node value
}

}
//Converting a given binary to its threaded binary tree
{
{

{
}
{
}
}
}
{
{

{
}
{
}
}
}
public static void Main(String[] args)
{
//Make object of Binary Tree
BinaryTree obj=new BinaryTree();

/*   Make A Binary Tree
-----------------------
1
/  \
2    3
/    /  \
4    5    6
\
7
/
8
*/
obj.root               = new Node(1);
obj.root.left          = new Node(2);
obj.root.right         = new Node(3);
obj.root.right.right   = new Node(6);
obj.root.right.left    = new Node(5);
obj.root.left.left     = new Node(4);
obj.root.left.left.right = new Node(7);

obj.root.left.left.right.left = new Node(8);
//Display Tree elements
obj.inorder(obj.root);

}
}```
```

#### Output

``````  4  8  7  2  1  5  3  6
4  8  7  2  1  5  3  6``````
``````# Python Program
# Convert binary tree to threaded binary tree
class Node :

def __init__(self, value) :
self.data = value
self.left = None
self.right = None

class BinaryTree :

def __init__(self) :
self.root = None

def main() :
obj = BinaryTree()
#   Make A Binary Tree
#          1
#         /  \
#        2    3
#       /    /  \
#      4    5    6
#       \
#        7
#       /
#      8
#
obj.root = Node(1)
obj.root.left = Node(2)
obj.root.right = Node(3)
obj.root.right.right = Node(6)
obj.root.right.left = Node(5)
obj.root.left.left = Node(4)
obj.root.left.left.right = Node(7)
obj.root.left.left.right.left = Node(8)
obj.inorder(obj.root)

if __name__ == "__main__":
main()```
```

#### Output

``````  4  8  7  2  1  5  3  6
4  8  7  2  1  5  3  6``````
``````# Ruby Program
# Convert binary tree to threaded binary tree
class Node
attr_accessor :data, :left, :right
def initialize(value)
@data = value
@left = nil
@right = nil
end
end

class BinaryTree
attr_accessor :root
def initialize()
@root = nil
end
end
end
end
end
end
end
end
end
end
end
end
def main()
obj = BinaryTree.new()
#   Make A Binary Tree
#          1
#         /  \
#        2    3
#       /    /  \
#      4    5    6
#       \
#        7
#       /
#      8
#
obj.root = Node.new(1)
obj.root.left = Node.new(2)
obj.root.right = Node.new(3)
obj.root.right.right = Node.new(6)
obj.root.right.left = Node.new(5)
obj.root.left.left = Node.new(4)
obj.root.left.left.right = Node.new(7)
obj.root.left.left.right.left = Node.new(8)
obj.inorder(obj.root)
end

main()```
```

#### Output

``````  4  8  7  2  1  5  3  6
4  8  7  2  1  5  3  6``````
``````<?php
/*
Php Program
Convert binary tree to threaded binary tree
*/
class Node {
public \$data;
public \$left;
public \$right;

function __construct(\$value) {
\$this->data = \$value;
\$this->left = null;
\$this->right = null;
}
}
class BinaryTree {
public \$root;

function __construct() {
\$this->root = null;
}
}
}
}
}
}
}
}
}
}
}
}
function main() {
\$obj = new BinaryTree();
/*   Make A Binary Tree
-----------------------
1
/  \
2    3
/    /  \
4    5    6
\
7
/
8
*/
\$obj->root = new Node(1);
\$obj->root->left = new Node(2);
\$obj->root->right = new Node(3);
\$obj->root->right->right = new Node(6);
\$obj->root->right->left = new Node(5);
\$obj->root->left->left = new Node(4);
\$obj->root->left->left->right = new Node(7);
\$obj->root->left->left->right->left = new Node(8);
\$obj->inorder(\$obj->root);
}
main();```
```

#### Output

``````  4  8  7  2  1  5  3  6
4  8  7  2  1  5  3  6``````
``````/*
Node JS Program
Convert binary tree to threaded binary tree
*/
class Node {

constructor(value) {
this.data = value;
this.left = null;
this.right = null;
}
}
class BinaryTree {

constructor() {
this.root = null;
}
}
}
}
}
}
}
}
}
}
}
}

function main() {
var obj = new BinaryTree();
/*   Make A Binary Tree
-----------------------
1
/  \
2    3
/    /  \
4    5    6
\
7
/
8
*/
obj.root = new Node(1);
obj.root.left = new Node(2);
obj.root.right = new Node(3);
obj.root.right.right = new Node(6);
obj.root.right.left = new Node(5);
obj.root.left.left = new Node(4);
obj.root.left.left.right = new Node(7);
obj.root.left.left.right.left = new Node(8);
obj.inorder(obj.root);
}
main();```
```

#### Output

``````  4  8  7  2  1  5  3  6
4  8  7  2  1  5  3  6``````
``````/*
Swift 4 Program
Convert binary tree to threaded binary tree
*/
class Node  {
var data: Int;
var left: Node? ;
var right: Node? ;

init(_ value: Int) {
self.data = value;
self.left = nil;
self.right = nil;
}
}
class BinaryTree {
var root: Node? ;
init() {
self.root = nil;
}
func inorder(_ head: Node? ) {
}
}
func threadedBT(_ head: Node? , _ leftP : Node? , _ rightP : Node? ) {
}
}
}
}
func threaded_inorder(_ head: Node? , _ leftP : Node? , _ rightP : Node? ) {
}
}
}
}
}
func main() {
let obj: BinaryTree = BinaryTree();
/*   Make A Binary Tree
-----------------------
1
/  \
2    3
/    /  \
4    5    6
\
7
/
8
*/
obj.root = Node(1);
obj.root!.left = Node(2);
obj.root!.right = Node(3);
obj.root!.right!.right = Node(6);
obj.root!.right!.left = Node(5);
obj.root!.left!.left = Node(4);
obj.root!.left!.left!.right = Node(7);
obj.root!.left!.left!.right!.left = Node(8);
obj.inorder(obj.root);
}
main();```
```

#### Output

``````  4  8  7  2  1  5  3  6
4  8  7  2  1  5  3  6``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.