-
Notifications
You must be signed in to change notification settings - Fork 101
/
LLfromBST.cpp
169 lines (152 loc) · 4.05 KB
/
LLfromBST.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*Create Linked List from a given Binary Search Tree */
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class BinaryTree
{
public:
T data;
BinaryTree<int> *left= NULL;
BinaryTree<int> *right= NULL;
BinaryTree(T data)
{
this->data = data;
}
BinaryTree<int> *TakeinputLevelwise()
{
queue<BinaryTree<int> *> pendingNodes;
int rootData;
cout << "Enter Root Data : ";
cin >> rootData;
if (rootData == -1)
{
return NULL;
}
BinaryTree<int> *root = new BinaryTree<int>(rootData);
pendingNodes.push(root);
while (pendingNodes.size() != 0)
{
BinaryTree<int> *front = pendingNodes.front();
pendingNodes.pop();
cout << "Enter Left child of " << front->data << " : ";
int leftchild;
cin >> leftchild;
if (leftchild != -1)
{
BinaryTree<int> *child = new BinaryTree<int>(leftchild);
front->left = child;
pendingNodes.push(child);
}
cout << "Enter right child of " << front->data << " : ";
int rightchild;
cin >> rightchild;
if (rightchild != -1)
{
BinaryTree<int> *child = new BinaryTree<int>(rightchild);
front->right = child;
pendingNodes.push(child);
}
}
return root;
}
void printTree(BinaryTree<int> *root)
{
queue<BinaryTree<int> *> pendingNodes;
pendingNodes.push(root);
while (pendingNodes.size() != 0)
{
BinaryTree<int> *front = pendingNodes.front();
pendingNodes.pop();
cout << endl;
cout << front->data << " : ";
if (front->left != NULL)
{
cout << " L" << front->left->data;
pendingNodes.push(front->left);
}
if (front->right != NULL)
{
cout << " R" << front->right->data;
pendingNodes.push(front->right);
}
}
}
~BinaryTree(){
delete left;
delete right;
}
};
class node{
public: int data;
node* next;
node(int data){
this->data=data;
next=NULL;
}
};
class Pair{
public: node* head;
node* tail;
};
Pair LLfromBST(BinaryTree<int>* root){
if(root==NULL){
Pair zero;
zero.head=NULL;
zero.tail=NULL;
return zero;
}
if(root->left==NULL){
if(root->right==NULL){
Pair ans;
node* newnode=new node(root->data);
ans.head=newnode;
ans.tail=newnode;
return ans;
}
else{
node* newnode=new node(root->data);
Pair ans;
ans=LLfromBST(root->right);
newnode->next=ans.head;
ans.head=newnode;
return ans;
}
}
else{
if(root->right==NULL){
Pair ans;
ans=LLfromBST(root->left);
node* newnode= new node(root->data);
ans.tail->next=newnode;
ans.tail=newnode;
return ans;
}
else{
Pair ans;
ans=LLfromBST(root->left);
node* newnode= new node(root->data);
ans.tail->next=newnode;
ans.tail=newnode;
Pair ans2;
ans2=LLfromBST(root->right);
ans.tail->next=ans2.head;
ans.tail=ans2.tail;
return ans;
}
}
}
void print(node* head){
node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
BinaryTree<int> *root=root->TakeinputLevelwise();
node* head=LLfromBST(root).head;
print(head);
return 0;
}