// YahooTree.cpp: implementation of the YahooTree class.
//
//////////////////////////////////////////////////////////////////////

#include "YahooTree.h"

istream & operator>>(istream & is, YahooTree &t)
{
	YahooNode * current = t.root;
	stack<YahooNode *> ancestors;

	for (YahooNode * n = new YahooNode; is >> *n; n = new YahooNode){
		if (t.root == 0) { //the first node is the root
				t.root = n;
				ancestors.push(n);
				continue;
		}
		for (YahooNode * c = ancestors.top(); !(ancestors.empty()); ancestors.pop(),
			c = ancestors.top()){
			if (n->childOf(*c)) {
				c->addChild(n);
				ancestors.push(n);
				break; 
			}
		};

	}

	delete n; //that last node was never added to the tree
	return is;	
}

void YahooTree::printAsHTML(map<string, URL> & m)
{
	if (root != 0)
		root->printAsHTML(m);
}