1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
///////////////////////////////////////////////////////////////////////////////
//  Description: Builds a binary tree from a dual graph of triangualated mesh.
//  Chaman Singh Verma
//  University of Wisconsin Madison, USA
//  Date 15th Jan 2010
//
//  License: Free to distribute and modify.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef BTREE_H
#define BTREE_H

#include "meshkit/Mesh.hpp"
#include "meshkit/DualGraph.hpp"
#include "meshkit/ObjectPool.hpp"

#ifdef USE_HASHMAP
#include <tr1/unordered_map>
#endif

//#include <ext/slist>
//typedef __gnu_cxx::slist<BinaryNode*> BNodeList;

namespace Jaal {

class BinaryNode;

typedef std::list<BinaryNode*> BNodeList;

class BinaryNode {
public:
     BinaryNode()
     {}

     BinaryNode(PNode n) {
          dualNode = n;
          levelID = -1;
          parent = NULL;
     }

     void setLevelID(int l) {
          levelID = l;
     }

     int getLevelID() const {
          return levelID;
     }

     bool isMatched() const {
          return dualNode->isRemoved();
     }

     void setMatchMark(char r) {
          dualNode->setStatus(r);
     }

     bool isRoot() const {
          if (parent == NULL)
               return 1;
          return 0;
     }

     bool isLeaf() const {
          if (getNumChildren() == 0)
               return 1;
          return 0;
     }

     int getDegree() const {
          int ncount = 0;
          if (parent)
               ncount = 1;
          ncount += getNumChildren();
          return ncount;
     }

     BinaryNode * getSibling() const {
          if (parent) {
               for (int i = 0; i < parent->getNumChildren(); i++) {
                    BinaryNode *child = parent->getChild(i);
                    if (child != this)
                         return child;
               }
          }
          return NULL;
     }

     void setParent(BinaryNode * p) {
          parent = p;
     }

     BinaryNode * getParent() const {
          return parent;
     }

     int getNumChildren() const {
          int ncount = 0;
          for (size_t i = 0; i < children.size(); i++)
               if (children[i].active)
                    ncount++;
          return ncount;
     }

     void addChild(BinaryNode * c) {
          ActiveNode activenode(c);
          if (find(children.begin(), children.end(), activenode) == children.end())
               children.push_back(activenode);
     }

     void removeChild(BinaryNode * child) {
          ActiveNode activenode(child);
          vector<ActiveNode>::iterator it;
          it = remove(children.begin(), children.end(), activenode);
          children.erase(it, children.end());
     }

     void unlinkChild(BinaryNode * child) {
          for (size_t i = 0; i < children.size(); i++) {
               if (children[i].node == child) {
                    children[i].active = 0;
                    return;
               }
          }
     }

     void relinkChild(BinaryNode * child) {
          for (size_t i = 0; i < children.size(); i++) {
               if (children[i].node == child) {
                    children[i].active = 1;
                    return;
               }
          }
     }

     void relinkAll() {
          for (size_t i = 0; i < children.size(); i++)
               children[i].active = 1;
     }

     BinaryNode * getChild(int cid) const {
          int index = -1;
          for (size_t i = 0; i < children.size(); i++) {
               if (children[i].active) {
                    index++;
                    if (index == cid)
                         return children[i].node;
               }
          }
          return NULL;
     }

     int getID() const {
          return dualNode->getID();
     }

     Vertex * getDualNode() const {
          return dualNode;
     }

private:
     struct ActiveNode {
          ActiveNode(BinaryNode *n) {
               active = 1;
               node = n;
          }
          bool active;
          bool operator ==(const ActiveNode &rhs) const {
               return node == rhs.node;
          }
          BinaryNode *node;
     };
     int levelID;
     PNode dualNode;
     BinaryNode* parent;

     vector<ActiveNode> children;
};

class BinaryTree {
public:

     const static int BREADTH_FIRST_TREE = 0;
     const static int DEPTH_FIRST_TREE   = 1;

     BinaryTree(DualGraph *g) {<--- Member variable 'BinaryTree::root' is not initialized in the constructor.
          dgraph = g;
          treetype = BREADTH_FIRST_TREE;
     }

     ~BinaryTree() {
          clear();
     }

     void setTreeType(int t) {
          treetype = t;
     }

     void build(BinaryNode *r = NULL);

     BinaryNode* getRoot() const {
          return root;
     }

     // Remove a given node from the tree. Unlink child and parents.
     //
     void removeNode(BinaryNode *node) {
          if (node) {
               BinaryNode *parv = node->getParent();
               if (parv)
                    parv->removeChild(node);
          }
     }

     void addNode(BinaryNode *tnode) {
          tnodemap[tnode->getDualNode()] = tnode;
     }

     void unlinkNode(BinaryNode *node) {
          if (node) {
               BinaryNode *parv = node->getParent();
               if (parv)
                    parv->unlinkChild(node);
          }
     }

     bool isMatched(const BinaryNode *u, const BinaryNode *v) const {
          Vertex *du = u->getDualNode();
          Vertex *dv = v->getDualNode();
          Vertex *umate = NULL;
          Vertex *vmate = NULL;
          du->getAttribute("DualMate", umate);
          dv->getAttribute("DualMate", vmate);
          if (umate == dv && vmate == du) return 1;
/*
          if (du->getDualMate() == dv && dv->getDualMate() == du)
               return 1;
*/
          return 0;
     }

     // Give nodes at a given level. Root is at level = 0,
     const BNodeList &getLevelNodes(int l) const;

     // Total Height of the tree...
     int getHeight() const;

     // How many nodes in the tree.
     size_t getSize() const;

     // Clear all the nodes created in the tree.
     void clear();

     void deleteAll();

     void relinkAll();

     // Save the tree in GraphViz data format...
     void saveAs(const string &s);

private:
     DualGraph *dgraph;
     BinaryNode *root;
     BNodeList emptylist;
     ObjectPool<BinaryNode> pool;

     int treetype;

#ifdef USE_HASHMAP
     typedef std::tr1::unordered_map<Vertex*, BinaryNode*>  TNodeMap;
#else
     typedef std::map<Vertex*, BinaryNode*> TNodeMap;
#endif
     TNodeMap  tnodemap;

     std::map<int, BNodeList> levelnodes;

     void bfs_traverse(BinaryNode *parent, BNodeList &nextnodes);
     void bfs_traverse(BinaryNode *parent);

     void dfs_traverse(BinaryNode *parent);
     void dfs_traverse(BinaryNode *parent, BNodeList &nextnodes);
};

} // namespace Jaal

#endif