added PTree class
git-svn-id: svn://anubis/gvsu@301 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
36c3b1e342
commit
12a4d54bcc
@ -1,9 +1,10 @@
|
||||
|
||||
CXX := mpiCC
|
||||
TARGET := maximum-parsimony
|
||||
OBJS := Sequence.o
|
||||
OBJS := maximum-parsimony.o
|
||||
OBJS += Sequence.o
|
||||
OBJS += ElementSet.o
|
||||
OBJS += maximum-parsimony.o
|
||||
OBJS += PTree.o
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
|
63
cs677/pa4/PTree.cc
Normal file
63
cs677/pa4/PTree.cc
Normal file
@ -0,0 +1,63 @@
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "PTree.h"
|
||||
#include "Sequence.h"
|
||||
using namespace std;
|
||||
|
||||
PTree::PTree(const vector<Sequence *> & leaves)
|
||||
{
|
||||
int num_leaves = leaves.size();
|
||||
if (num_leaves & (num_leaves - 1))
|
||||
{
|
||||
cerr << "Ptree::Ptree(): Number of initializing leaves is not a power of 2!!" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
vector<Sequence> * my_leaves = new vector<Sequence>(num_leaves);
|
||||
for (int i = 0; i < num_leaves; i++)
|
||||
{
|
||||
(*my_leaves)[i] = *leaves[i];
|
||||
}
|
||||
myLevels.push_back(my_leaves);
|
||||
for (int level_index = 1, level_size = num_leaves / 2;
|
||||
level_size;
|
||||
level_index++, level_size /= 2)
|
||||
{
|
||||
vector<Sequence> * level = new vector<Sequence>(level_size);
|
||||
for (int i = 0; i < level_size; i++)
|
||||
{
|
||||
(*level)[i] =
|
||||
(*myLevels[level_index-1])[2*i].parent(
|
||||
(*myLevels[level_index-1])[2*i+1]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PTree::~PTree()
|
||||
{
|
||||
for (int i = 0, sz = myLevels.size(); i < sz; i++)
|
||||
{
|
||||
delete myLevels[i];
|
||||
}
|
||||
}
|
||||
|
||||
void PTree::print(ostream & out, int level, int index,
|
||||
string prefix, string subprefix) const
|
||||
{
|
||||
out << prefix;
|
||||
out << myLevels[level][0][index];
|
||||
if (level > 0)
|
||||
{
|
||||
print(out, level - 1, index * 2, subprefix + "+-", subprefix + "| ");
|
||||
print(out, level - 1, index * 2 + 1, subprefix + "`-", subprefix + " ");
|
||||
}
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & out, const PTree & p)
|
||||
{
|
||||
p.print(out, p.myLevels.size() - 1, 0, "", "");
|
||||
return out;
|
||||
}
|
18
cs677/pa4/PTree.h
Normal file
18
cs677/pa4/PTree.h
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "Sequence.h"
|
||||
|
||||
class PTree
|
||||
{
|
||||
private:
|
||||
std::vector< std::vector< Sequence > * > myLevels;
|
||||
|
||||
public:
|
||||
PTree(const std::vector<Sequence *> & leaves);
|
||||
~PTree();
|
||||
void print(std::ostream & out, int level, int index,
|
||||
std::string prefix, std::string subprefix) const;
|
||||
|
||||
friend std::ostream & operator<<(std::ostream & out, const PTree & p);
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user