[go: up one dir, main page]

Menu

[r303]: / libpetey / tree_tmp.cpp  Maximize  Restore  History

Download this file

161 lines (135 with data), 3.6 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
 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
#include <stdint.h>
#include "tree_tmp.h"
#include "string_petey.h"
#include "time_class.h"
#define TEST 0
#if TEST
#include <iostream>
#endif
namespace libpetey {
template <class primitive>
tree<primitive>::tree() {
value=NULL;
left=NULL;
right=NULL;
}
//adds an element to the tree
//If the element is a duplicate, it is not added
//and the function returns the index value of the duplicate tree element
//Otherwise the method returns a copy of the index
template <class primitive>
long tree<primitive>::add_member(primitive new_val, long new_ind) {
if (value==NULL) {
value=new primitive;
*value=new_val;
index=new_ind;
//cout<<*value<<"\n";
} else {
if (new_val == *value) {
return index;
} else if (new_val < *value) {
if (left==NULL) left=new tree;
//cout<<" ";
left->add(new_val, new_ind);
} else {
if (right==NULL) right=new tree;
//cout <<" ";
right->add(new_val, new_ind);
}
}
return index;
}
//adds an element to the tree
//If the element is a duplicate, it is not added
//and the function returns the index value of the duplicate tree element
//Otherwise the method returns a copy of the index
template <class primitive>
long tree<primitive>::add(primitive new_val, long new_ind) {
if (value==NULL) {
value=new primitive;
*value=new_val;
index=new_ind;
//cout<<*value<<"\n";
} else {
if (new_val < *value) {
if (left==NULL) left=new tree;
//cout<<" ";
left->add(new_val, new_ind);
} else {
if (right==NULL) right=new tree;
//cout <<" ";
right->add(new_val, new_ind);
}
}
return index;
}
template <class primitive>
//decomposes the tree into an array
//the size of the array must be known in advance
//so the number of elements in the tree must be kept track of
//externally
void tree<primitive>::decompose(primitive *data_arr, long *index_arr, long n, long &i) {
if (left!=NULL) {
left->decompose(data_arr, index_arr, n, i);
}
if (i >=n) return;
data_arr[i]=*value;
index_arr[i]=index;
i++;
if (right!=NULL) {
right->decompose(data_arr, index_arr, n, i);
}
}
template <class primitive>
tree<primitive>::~tree() {
if (left != NULL) delete left;
if (right != NULL) delete right;
if (value != NULL) delete value;
}
//now to test the tree:
template <class primitive>
primitive *treesort(primitive *data, long n, long **index) {
primitive *sorted_data;
long *sorted_index;
tree<primitive> sorter;
long i;
sorted_data=new primitive[n];
sorted_index=new long[n];
for (i=0; i<n; i++) {
//cout<<data[i];
sorter.add(data[i], i);
}
i=0;
sorter.decompose(sorted_data, sorted_index, n, i);
//for (i=0;i<n;i++) cout<<sorted_index[i]<<" ";
//cout<<"\n";
*index=sorted_index;
return sorted_data;
}
template class tree<float>;
template class tree<int32_t>;
template class tree<int64_t>;
template class tree<double>;
template class tree<time_class>;
template class tree<string_petey>;
} //end namespace libpetey
#if TEST
#define N 5L
int main() {
float data[N]={4.0, 2.0, 3.0, 1.0, 6.0};
float *sorted;
long *index;
long i;
for (i=0;i<N;i++) cout<<data[i]<<" ";
cout<<"\n";
sorted=sort(data, N, &index);
for (i=0;i<N;i++) cout<<sorted[i]<<" ";
cout<<"\n";
for (i=0;i<N;i++) cout<<index[i]<<" ";
cout<<"\n";
// cout<<index;
delete sorted;
delete index;
return 1;
}
#endif