hello,
I'm trying to create a IntervalTree : public Tree { }; Node is a nested class of Tree; I hope this code will be clear; I have some question about it. Beyond the fact that it create a ultra-not-balanced tree (I don't remember how these trees are called...maybe they are a name); but the questions aren't on the tree structure but other:
IntervalTree::IntervalTree(vector<Interval>& vec) : Tree() {
vector<Interval>::iterator it;
//createRoot( new Node(vec[0]) );
Tree::Node* r = 0;
int i=0;
for (it = vec.begin(); it != vec.end(); ++it) {
if ( r == 0) { createRoot( new Node( *it ) ); r = getRoot(); continue; }//1
i %=2;
if (i == 0) {
r->setLeft( new Tree::Node( *it ) );
r = r->getLeft();
}
else if (i == 1) {
r->setRight( new Tree::Node( *it ) );
r = r->getRight();
}
++i;
}
}
IntervalTree::IntervalTree(vector<Interval>& vec) : Tree() {
vector<Interval>::iterator it;
//createRoot( new Node(vec[0]) );
Tree::Node* r = 0;
int i=0;
for (it = vec.begin(); it != vec.end(); ++it) {
if ( r == 0) { createRoot( new Node( *it ) ); r = getRoot(); continue; }//1
i %=2;
if (i == 0) {
r->setLeft( new Tree::Node( *it ) );
r = r->getLeft();
}
else if (i == 1) {
r->setRight( new Tree::Node( *it ) );
r = r->getRight();
}
++i;
}
}
To copy to clipboard, switch view to plain text mode
Is good the use of setLeft() and right() and the line //1 ? Any simple suggest ?
Maybe was better put the createRoot in the call of Tree() at begin?
Bookmarks