Beispiel #1
0
void HeatTree::SetBranchVal(string infile)	{
	ifstream is(infile.c_str());
	if (!is)	{
		cerr << "error in heattree: did not find " << infile << '\n';
		exit(1);
	}

	// int n;
	// is >> n;
	// for (int i=0; i<n; i++)	{
	bool cont = true;
	while (cont)	{
		string name1, name2;
		string tmp;
		// double tmp;
		is >> name1 >> name2 >> tmp;
		cerr << name1 << '\t' << name2 << '\t' << tmp << '\n';
		if (name1 != "END")	{
		const Link* link = GetLCA(name1,name2);
		if (! link)	{
			cerr << "error in heattree: did not find common ancestor of " << name1 << " and " << name2 << '\n';
			exit(1);
		}
		if (! link->isRoot())	{
			// cerr << link << '\t' << name1 << '\t' << name2 << '\t' << tmp << '\n';
			branchval[link->GetBranch()] = tmp;
		}
		}
		else	{
			cerr << "end\n";
			cont = false;
		}
	}
	withbranchval = true;
}
Beispiel #2
0
struct TNode* GetLCA(struct TNode* root,struct TNode* lNode,struct TNode* rNode,int* isLeftSet,int* isRightSet){
if(!root){
return NULL;
}
if(root==lNode || root==rNode){
    if(root==lNode){
    (*isLeftSet)=1;
    }
    if(root==rNode){
    (*isRightSet)=1;
    }
return root;
}

struct TNode* lResult=GetLCA(root->left,lNode,rNode,isLeftSet,isRightSet);
struct TNode* rResult=GetLCA(root->right,lNode,rNode,isLeftSet,isRightSet);

if(lResult && rResult){
return root;
}

return lResult!=NULL?lResult:rResult;

}
Beispiel #3
0
void HeatTree::SetExternalNodeVal(string infile)	{
	ifstream is(infile.c_str());
	if (!is)	{
		cerr << "error in heattree: did not find " << infile << '\n';
		exit(1);
	}
	int n;
	is >> n;
	for (int i=0; i<n; i++)	{
		string name1, name2;
		double tmp;
		is >> name1 >> name2 >> tmp;
		const Link* link = GetLCA(name1,name2);
		if (! link)	{
			cerr << "error in heattree: did not find common ancestor of " << name1 << " and " << name2 << '\n';
			exit(1);
		}
		cerr << link << '\t' << name1 << '\t' << name2 << '\n';
		extnodeval[link->GetNode()] = tmp;
	}
	withexternalnodeval = true;
}
Beispiel #4
0
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
int isLeftSet=0;
int isRightSet=0;
struct TNode* tmpNode=(struct TNode*)malloc(sizeof(struct TNode));
tmpNode->data=10;
tmpNode->left=NULL;
tmpNode->right=NULL;
struct TNode* ancestor=GetLCA(root,root->left->left,tmpNode,&isLeftSet,&isRightSet);
if(isLeftSet && isRightSet){
printf("\nAncestor : %d\n",ancestor->data);
}else{
printf("\nNULL\n");
}

return 0;
}