Currently, my problem is that at even depths (besides the zeroth) the program seems to ignore the matching condition for the median and sets the median as values outside of the data set. Below is my code and some sample output, the first section of the output is a repeat of the input data (testing to see if it is taken in correctly). Here is the code:
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
//Point Type
typedef struct{
float xvar;
float yvar;
}coor;
//Tree Structure
struct treeNode{
struct treeNode *leftPtr;
struct treeNode *rightPtr;
vector<coor> data;
coor nodecoor;
int depth;
//int size;
};
void searchNode(struct treeNode *&, coor, int );
void insertNode(struct treeNode *, vector<coor>, int depth);
void deleteNode(struct treeNode *&, coor);
//tree printing
void postOrder(struct treeNode* treePtr);
//Functions
vector<coor> readdata(float *a_hilines, char* filename);
float median(vector<coor> data, int on);
float dist(coor, coor);
//program begin
int main(int argc, char* argv[]){
float *a_hlines;
treeNode* rootPtr=NULL;
vector<coor> data = readdata(a_hlines,argv[1]);
data.pop_back();
coor testvalue=data[1];
//now build tree
insertNode(rootPtr,data,0);
searchNode(rootPtr,testvalue,0);
postOrder(rootPtr);
return 0;
}
//function definations
//load data:
vector<coor> readdata(float *a_hilines, char* filename){
ifstream infile;
infile.open(filename);
char var[30];
vector<coor> inputs;
coor temp;
while(!infile.eof()){
infile>>var;
temp.xvar = atof(var);
infile>>var;
temp.yvar = atof(var);
inputs.push_back(temp);
}
return inputs;
}
//Median Finding function
float median(vector<coor> raw, int on){
float medi;
vector<float> temp;
if(on%2==0){
for(int i=0; i<(raw.size());i++){
temp.push_back(raw[i].xvar);
}
}
if(on%2!=0){
for(int i=0; i<(raw.size());i++){
temp.push_back(raw[i].yvar);
}
}
sort(temp.begin(),temp.end());
// if(temp.size()%2==1){
medi=temp[(raw.size())/2];
// }//else{
//medi=(temp[temp.size()/2]+temp[(temp.size()/2-1)])/2;
//}
temp.clear();
raw.clear();
return medi;
}
//Search Tree
void searchNode(treeNode *&treePtr, coor value, int flip){
if(treePtr !=NULL){
coor test;
test=treePtr->nodecoor;
if(test.xvar==value.xvar && test.yvar==value.yvar){
cout << "found" << " {" << value.xvar << ", " << value.yvar << "} " << "\n";
}
else{
if(flip==0){
if(value.xvar < treePtr->nodecoor.xvar){
searchNode((treePtr->leftPtr),value,1);
cout << "testing" << "\n";
}
else{
searchNode((treePtr->rightPtr),value,1);
}
}
if(flip==1){
if(value.yvar < treePtr->nodecoor.yvar){
searchNode((treePtr->leftPtr),value,0);
}
else{
searchNode((treePtr->rightPtr),value,0);
}
}
}
}
if(treePtr==NULL){
cout << "not_found {" << value.xvar << ", "<< value.yvar << "}" << "\n"; }
}
void postOrder(treeNode *treePtr){
coor data={0,0};
if(treePtr != NULL){
postOrder(treePtr->leftPtr);
postOrder(treePtr->rightPtr);
data=treePtr->nodecoor;
cout << data.xvar << data.yvar << "\n";
//cout << "( " << data.xvar << data.yvar ",) \n";
}
}
//Tree Insert Node
void insertNode(treeNode *treePtr, vector<coor> value, int depth){
float condition;
coor holder;
coor data;
vector<coor> temp1;
vector<coor> temp2;
if(depth==0){
condition=median(value, depth);
depth++;
for(int i=0; i<value.size();i++){
cout << i << " {" << value[i].xvar << ", " << value[i].yvar << "} \n";
if(value[i].xvar==condition){
treePtr=new treeNode;
treePtr->nodecoor=value[i];
holder=value[i];
// value.erase(value.begin()+(i-1));
}
else{
if(value[i].xvar<condition){
temp1.push_back(value[i]);
}
if(value[i].xvar>condition){
temp2.push_back(value[i]);
}
}
}
// value.clear();
cout << "condition " << condition << " {" << holder.xvar << ", " << holder.yvar << "} " <<" left size " << temp1.size() << " right size " << temp2.size()<<"\n";
if(temp1.size()>0){
insertNode((treePtr->leftPtr),temp1,depth);
temp1.clear();
}
if(temp2.size()>0){
insertNode((treePtr->rightPtr),temp2,depth);
temp2.clear();
}
}
// condition=median(value, initial);
if((depth%2)==0 && depth>0){
condition=median(value, depth);
depth++;
for(int i=0;i<i++;i<value.size()){
//check median-xvar, if equal set to node value, and delete from vector, else if less left else right
if(value[i].xvar==condition){
treePtr=new treeNode;
treePtr->nodecoor=value[i];
cout << condition << " " << value[i].xvar;
// value.erase(value.begin()+(i-1));
}
else{
if(value[i].xvar<condition){
temp1.push_back(value[i]);
}
if(value[i].xvar>condition){
temp2.push_back(value[i]);
}
}
}
// value.clear();
cout << "condition " << condition << " {" << holder.xvar << ", " << holder.yvar << "} " <<" left size " << temp1.size() << " right size " << temp2.size()<<"\n";
if(temp1.size()>0){
insertNode((treePtr->leftPtr),temp1,depth);
temp1.clear();
}
if(temp2.size()){
insertNode((treePtr->rightPtr),temp2,depth);
temp2.clear();
}
}
if((depth%2)!=0 && depth>0){
condition=median(value, depth);
depth++;
for(int i=0; i<value.size();i++){
if(value[i].yvar==condition){
treePtr=new treeNode;
treePtr->nodecoor=value[i];
holder=value[i];
}
else{
if(value[i].yvar<condition){
temp1.push_back(value[i]);
}
if(value[i].yvar>condition){
temp2.push_back(value[i]);
}
}
}
// value.clear();
cout << "condition " << condition << " {" << holder.xvar << ", " << holder.yvar << "} " <<" left size " << temp1.size() << " right size " << temp2.size()<<"\n";
if(temp1.size()>0){
insertNode((treePtr->leftPtr),temp1,depth);
temp1.clear();
}
if(temp2.size()>0){
insertNode((treePtr->rightPtr),temp2,depth);
temp2.clear();
}
}
}
Here is the test input file:
1 10 8 .01 88 .44 33 551 1893 782 .0001 787 433 11 7806 97895 543678 .0000000001
And the sample output:
0 {1, 10}
1 {8, 0.01}
2 {88, 0.44}
3 {33, 551}
4 {1893, 782}
5 {0.0001, 787}
6 {433, 11}
7 {7806, 97895}
8 {543678, 1e-10}
condition 88 {88, 0.44} left size 4 right size 4
condition 551 {33, 551} left size 2 right size 1
condition 8 {-2.99787e+38, 4.59163e-41} left size 0 right size 0
condition 10 {1, 10} left size 1 right size 0
condition 8 {-2.99777e+38, 4.59163e-41} left size 0 right size 0
condition 0.01 {8, 0.01} left size 0 right size 0
condition 0.0001 {-2.99787e+38, 4.59163e-41} left size 0 right size 0
condition 787 {0.0001, 787} left size 0 right size 0
condition 782 {1893, 782} left size 2 right size 1
condition 543678 {-2.99787e+38, 4.59163e-41} left size 0 right size 0
condition 11 {433, 11} left size 1 right size 0
condition 543678 {-2.99777e+38, 4.59163e-41} left size 0 right size 0
condition 1e-10 {543678, 1e-10} left size 0 right size 0
condition 7806 {-2.99787e+38, 4.59163e-41} left size 0 right size 0
condition 97895 {7806, 97895} left size 0 right size 0
condition 11 {433, 11} left size 4 right size 4
condition 88 {-2.99797e+38, 4.59163e-41} left size 0 right size 0
condition 0.44 {88, 0.44} left size 2 right size 1
condition 543678 {-2.99787e+38, 4.59163e-41} left size 0 right size 0
condition 0.01 {8, 0.01} left size 1 right size 0
condition 543678 {-2.99777e+38, 4.59163e-41} left size 0 right size 0
condition 1e-10 {543678, 1e-10} left size 0 right size 0
condition 1 {-2.99787e+38, 4.59163e-41} left size 0 right size 0
condition 10 {1, 10} left size 0 right size 0
condition 1893 {-2.99797e+38, 4.59163e-41} left size 0 right size 0
condition 787 {0.0001, 787} left size 2 right size 1
condition 1893 {-2.99787e+38, 4.59163e-41} left size 0 right size 0
condition 782 {1893, 782} left size 1 right size 0
condition 33 {-2.99777e+38, 4.59163e-41} left size 0 right size 0
condition 551 {33, 551} left size 0 right size 0
condition 7806 {-2.99787e+38, 4.59163e-41} left size 0 right size 0
condition 97895 {7806, 97895} left size 0 right size 0
not_found {8, 0.01}
Thanks for your help.

New Topic/Question
Reply




MultiQuote




|