MeshKit  1.0
BinaryTree.cpp
Go to the documentation of this file.
00001 #include "meshkit/BinaryTree.hpp"
00002 
00003 using namespace Jaal;
00004 
00006 
00007 void BinaryTree::deleteAll()
00008 {
00009      TNodeMap ::iterator it;
00010      for (it = tnodemap.begin(); it != tnodemap.end(); ++it)
00011           delete it->second;
00012      tnodemap.clear();
00013 }
00014 
00016 
00017 void BinaryTree::clear()
00018 {
00019      tnodemap.clear();
00020      levelnodes.clear();
00021 }
00022 
00024 void BinaryTree::relinkAll()
00025 {
00026      TNodeMap::iterator it;
00027      for (it = tnodemap.begin(); it != tnodemap.end(); ++it) {
00028           BinaryNode *tnode = it->second;
00029           tnode->relinkAll();
00030      }
00031 }
00032 
00034 
00035 int BinaryTree::getHeight() const
00036 {
00037      map<int, BNodeList>::const_iterator it;
00038 
00039      int h = 0;
00040      for (it = levelnodes.begin(); it != levelnodes.end(); ++it)
00041           h = max(h, it->first);
00042 
00043      return h + 1;
00044 }
00045 
00047 
00048 size_t BinaryTree::getSize() const
00049 {
00050      return tnodemap.size();
00051 }
00053 
00054 void BinaryTree::bfs_traverse(BinaryNode *parent, BNodeList &nextnodes)
00055 {
00056      PNode dualnode = parent->getDualNode();
00057 
00058      if (dualnode->isVisited())
00059           return;
00060 
00061      NodeSequence neighs;
00062      dualnode->getRelations( neighs );
00063 
00064      int nextlevel = parent->getLevelID() + 1;
00065 
00066      for (size_t i = 0; i < neighs.size(); i++) {
00067           dualnode = neighs[i];
00068           if (!dualnode->isVisited()) {
00069                BinaryNode *tnode = tnodemap[dualnode];
00070                if (tnode->getParent() == NULL) {
00071                     int lid = max(nextlevel, tnode->getLevelID());
00072                     tnode->setLevelID(lid);
00073                     tnode->setParent(parent);
00074                     parent->addChild(tnode);
00075                }
00076                nextnodes.push_back(tnode);
00077           }
00078      }
00079 
00080      dualnode = parent->getDualNode();
00081      dualnode->setVisitMark(1);
00082 }
00083 
00085 
00086 void BinaryTree::dfs_traverse(BinaryNode *parent, BNodeList &nextnodes)
00087 {
00088      PNode dualnode = parent->getDualNode();
00089 
00090      if (dualnode->isVisited())
00091           return;
00092 
00093      vector<BinaryNode*> neighQ;
00094      NodeSequence neighs;
00095      dualnode->getRelations( neighs );
00096 
00097      int nextlevel = parent->getLevelID() + 1;
00098 
00099      int nSize = neighs.size();
00100      for (int i = 0; i < nSize; i++) {
00101           dualnode = neighs[i];
00102           if (!dualnode->isVisited()) {
00103                BinaryNode *tnode = tnodemap[dualnode];
00104                if (tnode->getParent() == NULL) {
00105                     int lid = max(nextlevel, tnode->getLevelID());
00106                     tnode->setLevelID(lid);
00107                     tnode->setParent(parent);
00108                     parent->addChild(tnode);
00109                }
00110                neighQ.push_back(tnode);
00111           }
00112      }
00113 
00114      int nQ = neighQ.size();
00115      for (int i = 0; i < nQ; i++)
00116           nextnodes.push_front(neighQ[nQ - i - 1]);
00117 
00118      dualnode = parent->getDualNode();
00119      dualnode->setVisitMark(1);
00120 }
00121 
00123 
00124 void BinaryTree::dfs_traverse(BinaryNode *parent)
00125 {
00126      BNodeList listnodes;
00127      listnodes.push_back(parent);
00128 
00129      while (!listnodes.empty()) {
00130           BinaryNode *thisnode = listnodes.front();
00131           listnodes.pop_front();
00132           dfs_traverse(thisnode, listnodes);
00133      }
00134 }
00135 
00137 
00138 void BinaryTree::bfs_traverse(BinaryNode *parent)
00139 {
00140      BNodeList listnodes;
00141      listnodes.push_back(parent);
00142 
00143      while (!listnodes.empty()) {
00144           BinaryNode *thisnode = listnodes.front();
00145           listnodes.pop_front();
00146           bfs_traverse(thisnode, listnodes);
00147      }
00148 }
00149 
00151 
00152 const BNodeList &BinaryTree::getLevelNodes(int level) const
00153 {
00154      map<int, BNodeList>::const_iterator it;
00155 
00156      it = levelnodes.find(level);
00157      if (it == levelnodes.end())
00158           return emptylist;
00159 
00160      return it->second;
00161 }
00163 
00164 void BinaryTree::build(BinaryNode *r)
00165 {
00166      assert( dgraph);
00167 
00168      dgraph->setAdjTable(0, 0);
00169 
00170      int numnodes = dgraph->getSize(0);
00171 
00172      for (int i = 0; i < numnodes; i++) {
00173           Vertex *dualnode = dgraph->getNode(i);
00174           dualnode->setVisitMark(0);
00175           BinaryNode *tnode = new BinaryNode(dualnode);
00176           assert(tnode);
00177           tnodemap[dualnode] = tnode;
00178      }
00179 
00180      srand( time(NULL)); // Initialize random number
00181 
00182      int mindegree = numnodes;
00183      if (r == NULL) {
00184           for (int i = 0; i < numnodes; i++) {
00185                Vertex *dualnode = dgraph->getNode(i);
00186                int degree = dualnode->getNumRelations(0);
00187                if (degree < mindegree) mindegree = degree;
00188           }
00189 
00190           cout << " Root Degree : " << mindegree << endl;
00191 
00192           int offset = rand()%numnodes;
00193           assert( offset >=0 && offset < numnodes );
00194           Vertex *rootnode = NULL;
00195           for (int i = 0; i < numnodes; i++) {
00196                int j =  (offset + i)%numnodes;
00197                assert( j >=0 && j < numnodes );
00198                Vertex *dualnode = dgraph->getNode(j);
00199                int degree = dualnode->getNumRelations(0);
00200                if( degree == mindegree) {
00201                     rootnode = dualnode;
00202                     break;
00203                }
00204           }
00205 
00206           assert(rootnode);
00207           root = tnodemap[rootnode];
00208      } else
00209           root = r;
00210 
00211      assert(root != NULL);
00212 
00213      root->setLevelID(0);
00214 
00215      if (treetype == BREADTH_FIRST_TREE)
00216           bfs_traverse( root);
00217 
00218      if (treetype == DEPTH_FIRST_TREE)
00219           dfs_traverse( root);
00220 
00221      for (int i = 0; i < numnodes; i++) {
00222           Vertex *dualnode = dgraph->getNode(i);
00223           assert(dualnode->isVisited());
00224           BinaryNode *tnode = tnodemap[dualnode];
00225           int lid = tnode->getLevelID();
00226           levelnodes[lid].push_back(tnode);
00227      }
00228 
00229      cout << " Binary Tree Build Completed : #Nodes : " << getSize() << endl;
00230 }
00231 
00233 
00234 void BinaryTree::saveAs(const string &fname)
00235 {
00236      string filename = fname + ".dot";
00237      ofstream ofile(filename.c_str(), ios::out);
00238 
00239      ofile << "digraph G { " << endl;
00240      ofile << "size = \"8,8\" " << endl;
00241 
00242      TNodeMap::const_iterator it;
00243 
00244      for (it = tnodemap.begin(); it != tnodemap.end(); ++it) {
00245           BinaryNode *tnode = it->second;
00246           if (tnode->isMatched())
00247                ofile << tnode->getID() << "[style=filled, fillcolor=green]" << endl;
00248           else
00249                ofile << tnode->getID() << "[style=filled, fillcolor=red]" << endl;
00250      }
00251 
00252      for (it = tnodemap.begin(); it != tnodemap.end(); ++it) {
00253           BinaryNode *tnode = it->second;
00254           int numChildren = tnode->getNumChildren();
00255           for (int j = 0; j < numChildren; j++) {
00256                ofile << tnode->getID() << "->" << tnode->getChild(j)->getID();
00257                if (isMatched(tnode, tnode->getChild(j)))
00258                     ofile << "[color=blue]";
00259                else
00260                     ofile << "[color=red]";
00261                ofile << ";" << endl;
00262           }
00263 
00264      }
00265 
00266      ofile << " } " << endl;
00267 }
00268 
00270 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines