-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTToDLList.cpp
94 lines (91 loc) · 1.75 KB
/
BSTToDLList.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
#include<iostream>
#include<climits>
#include<vector>
#include<stack>
using namespace std;
class Node{
public:
int data;
Node * left;
Node * right;
Node(int val): data(val),left(NULL), right(NULL){};
Node (): data(0), left(NULL), right(NULL){};
};
Node * head = NULL;
Node * current = NULL;
Node * tail = NULL;
void buildTree(int * arr, int size, Node * nodeArr){
for(int i=0; i<size; i++){
if(arr[i]!=0){
if(i==0){
nodeArr[0].data= arr[i];
}
else{
nodeArr[i].data = arr[i];
int parentPos= (i-1)/2;
//Node parent = nodeArr[parentPos];
if(i%2 == 1)
nodeArr[parentPos].left=&nodeArr[i];
else
nodeArr[parentPos].right=&nodeArr[i];
}
}
}
return ;
}
void attachToLL(Node * n){
if(head == NULL){
head = n;
current = n;
}
else{
current->right = n;
n->left = current;
current = n;
}
}
void InOrderTrav(Node *node){
if(node == NULL) return;
InOrderTrav(node->left);
attachToLL(node);
InOrderTrav(node->right);
}
void convertToDLL(Node * root){
InOrderTrav(root);
tail = current;
tail->right = head;
head->left = tail;
}
int main(){
int n;
int num;
cin>> n;
int inputArr[n];
for (int i =0; i<n; i++){
cin>> num;
inputArr[i]=num;
}
Node root[n];
buildTree(inputArr, n, root);
convertToDLL(root);
Node * temp = head;
if(temp != NULL){
cout << temp->data << " ";
temp = temp->right;
while(head != temp){
cout << temp->data << " ";
temp = temp->right;
}
}
cout << endl;
temp=tail;
if(temp != NULL){
cout << temp->data << " ";
temp = temp->left;
while(tail != temp){
cout << temp->data << " ";
temp = temp->left;
}
}
return 0;
}