-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdoble_tree.cpp
91 lines (72 loc) · 1.81 KB
/
doble_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// C++ program to convert binary tree to double tree
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
class node
{
public:
int data;
node* left;
node* right;
};
/* function to create a new
node of tree and returns pointer */
node* newNode(int data);
/* Function to convert a tree to double tree */
void doubleTree(node* Node)
{
node* oldLeft;
if (Node == NULL) return;
/* do the subtrees */
doubleTree(Node->left);
doubleTree(Node->right);
/* duplicate this node to its left */
oldLeft = Node->left;
Node->left = newNode(Node->data);
Node->left->left = oldLeft;
}
/* UTILITY FUNCTIONS TO TEST doubleTree() FUNCTION */
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
node* newNode(int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
/* Given a binary tree, print its nodes in inorder*/
void printInorder(node* node)
{
if (node == NULL)
return;
printInorder(node->left);
cout << node->data << " ";
printInorder(node->right);
}
/* Driver code*/
int main()
{
/* Constructed binary tree is
1
/ \
2 3
/ \
4 5
*/
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
cout << "Inorder traversal of the original tree is \n";
printInorder(root);
doubleTree(root);
cout << "\nInorder traversal of the double tree is \n";
printInorder(root);
return 0;
}
// This code is contributed by rathbhupendra