当前位置: 首页>>代码示例>>C++>>正文


C++ BinaryTree::access_root方法代码示例

本文整理汇总了C++中BinaryTree::access_root方法的典型用法代码示例。如果您正苦于以下问题:C++ BinaryTree::access_root方法的具体用法?C++ BinaryTree::access_root怎么用?C++ BinaryTree::access_root使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BinaryTree的用法示例。


在下文中一共展示了BinaryTree::access_root方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: BuildLabelGTDirect

void BuildLabelGTDirect(Graph& g, BinaryTree& btree, const vector<set<int> >& nodedirectory, vector<vector<int> >& node_mapping_directory, vector<vector<VertexDistancePair> >& LabelGT_From, vector<vector<VertexDistancePair> >& LabelGT_To, vector<vertex_mapping_info>& vertex_mapping_table, vector<int>& vertex_to_nodeID){
	
	//calculate the number of vertices in the connected component (BFS)
	queue<int> q;
	if(btree.access_root()<0){
		cout<<"Btree has not been fully constructed. Exit."<<endl;
		exit(-1);
	}
	q.push(btree.access_root());
	while(q.size()>0){
		size_t j=q.front();
		q.pop();
		if (j<nodedirectory.size()){
			int i=0;
			for(set<int>::const_iterator sit=nodedirectory[j].begin(); sit!=nodedirectory[j].end(); sit++){
				if(g[*sit].active==true){
					vertex_to_nodeID[*sit]=j;
					vertex_mapping_table[*sit].level=btree[j].level;
					vertex_mapping_table[*sit].offset=i;
					node_mapping_directory[j].push_back(*sit);
					g[*sit].active=false;
					BFSLabelingDirect(g, *sit, LabelGT_From, 0);//0 means from and in edges;
					BFSLabelingDirect(g, *sit, LabelGT_To, 1);//1 means to and out edges;
					i++;
				}
			}
		}
		
		if (btree[j].left_child>=0){
			q.push(btree[j].left_child);
		}
		
		if (btree[j].right_child>=0){
			q.push(btree[j].right_child);
		}
	}
	//calculate the number of vertices in the connected component done
}
开发者ID:WNawaz,项目名称:TreeMap,代码行数:38,代码来源:TreeMapC.cpp

示例2: buildBinaryTree

void buildBinaryTree(GenericTree& btd, BinaryTree& btree, vector<int>& virtualnode_to_realnode){
	multimap<int, int> subsize_root;////The first int is subtree size, and the second is node ID.
	for(int current_node=0; current_node<btd.num_nodes(); current_node++){
		
		int current_node_level=btd[current_node].nodelevel;
		multimap<int, int> subsize_node;//The first int is subtree size, and the second is node ID.
		int parent=-1;
		int total_size=0;
		for(TreeEdgeList::iterator tit=btd.get_neighbors(current_node).begin(); tit!=btd.get_neighbors(current_node).end(); tit++){
			if(btd[tit->nodeID].nodelevel>current_node_level){
				subsize_node.insert(pair<int, int>(tit->subtree_size, tit->nodeID));
				total_size+=tit->subtree_size;
			}else if (btd[tit->nodeID].nodelevel==current_node_level){
				cout<<"Internal logic error 1 ocurrs on binary tree construction."<<endl;
			}else{//implies btd[tit->nodeID].nodelevel<current_node_level
				if (parent==-1){
					parent=tit->nodeID;
				}else{
					cout<<"Internal logic error 2 (multiple parents) ocurrs on binary tree construction."<<endl;
				}
			}
		}
		
		if (parent==-1){//implies root node
			subsize_root.insert(pair<int, int>(total_size+1, current_node));
		}
		
		btree[current_node].parent=parent;
		if(subsize_node.size()==1){
			btree[current_node].left_child=(subsize_node.begin())->second;
		}else if (subsize_node.size()==2){
			multimap<int, int>::iterator mit=subsize_node.begin();
			btree[current_node].left_child=mit->second;
			mit++;
			btree[current_node].right_child=mit->second;
		}else if (subsize_node.size()>2){ 
			while(subsize_node.size()>2){
				int first_size=(subsize_node.begin())->first;
				int first_node=(subsize_node.begin())->second;
				subsize_node.erase(subsize_node.begin());
				int second_size=(subsize_node.begin())->first;
				int second_node=(subsize_node.begin())->second;
				subsize_node.erase(subsize_node.begin());
				int new_node_id=btree.num_nodes();
				//add a virtual node
				btree.addNode(new_node_id);
				virtualnode_to_realnode.push_back(current_node);
				//add a virtual node done
				btree[new_node_id].left_child=first_node;
				btree[new_node_id].right_child=second_node;
				btree[first_node].parent=new_node_id;
				btree[second_node].parent=new_node_id;
				subsize_node.insert(pair<int, int>(first_size+second_size, new_node_id));
			}
			multimap<int, int>::iterator mit=subsize_node.begin();			
			btree[current_node].left_child=mit->second;
			btree[mit->second].parent=current_node;
			
			mit++;
			btree[current_node].right_child=mit->second;
			btree[mit->second].parent=current_node;
		}
		
	}

	int root=-1;
	if(subsize_root.size()==1){
		root=(subsize_root.begin())->second;
	}else if (subsize_root.size()>1){
		while(subsize_root.size()>1){
			int first_size=(subsize_root.begin())->first;
			int first_node=(subsize_root.begin())->second;
			subsize_root.erase(subsize_root.begin());
			int second_size=(subsize_root.begin())->first;
			int second_node=(subsize_root.begin())->second;
			subsize_root.erase(subsize_root.begin());
			int new_node_id=btree.num_nodes();
			//add a virtual node
			btree.addNode(new_node_id);
			virtualnode_to_realnode.push_back(-1);//If the root node is a virtual node, there is no real node can be associated with it.
			//add a virtual node done
			btree[new_node_id].left_child=first_node;
			btree[new_node_id].right_child=second_node;
			btree[first_node].parent=new_node_id;
			btree[second_node].parent=new_node_id;
			subsize_root.insert(pair<int, int>(first_size+second_size, new_node_id));
			root=new_node_id;
		}
	}
	
	if(root<0){
		cout<<"The btree is empty. Please check input."<<endl;
		return;
	}else{
		btree.access_root()=root;
		btree[root].level=0;
		int maximum_level=0;
		queue<int> q;
		q.push(root);
		while(q.size()>0){
//.........这里部分代码省略.........
开发者ID:WNawaz,项目名称:TreeMap,代码行数:101,代码来源:TreeMapC.cpp


注:本文中的BinaryTree::access_root方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。