-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsaket.cpp
More file actions
94 lines (69 loc) · 1.24 KB
/
saket.cpp
File metadata and controls
94 lines (69 loc) · 1.24 KB
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
#include <bits/stdc++.h>
using namespace std;
struct nodeLL{
int data;
nodeLL *pre, *next;
};
struct nodeT{
int data;
nodeT *pre, *lch, *rch;
nodeLL *lp, *rp;
};
void insert(nodeT* ptr, int data){
if(data < ptr->data){
if(ptr->lch == NULL){
}
else insert(ptr->lch, data);
}
else if(data > ptr->data){
if(ptr->rch == NULL){
}
else insert(ptr->rch, data);
}
}
int main(){
cout << "Enter no of elements" << endl;
int n;
cin >> n;
vector<int> pre(n,0);
vector<int> in(n,0);
nodeLL *start = NULL;
nodeLL *end = NULL;
for(int i = 0; i < n; i++){
nodeLL *temp = new nodeLL();
temp->next = NULL;
temp->data = in[i];
if(start == NULL){
start = temp;
end = temp;
temp->pre = NULL;
}
else{
temp->pre = end;
end->next = temp;
end = temp;
}
}
nodeT *root = NULL;
nodeT *temp = new nodeT();
temp->data = pre[0];
temp->pre = NULL;
temp->rch = NULL;
temp->lch = NULL;
root = temp;
nodeLL* itr = start;
while(itr->data != pre[0]) itr = itr->next;
if(itr != start){
temp->lp = start;
itr->pre->next = NULL;
}
else temp->lp = NULL;
if(itr != end){
temp->rp = itr->next;
itr->next->pre = NULL;
}
else temp->rp = NULL;
for(int i = 1; i < n; i++){
insert(root,pre[i]);
}
}