00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __CS_CSTREENODE_H__
00020 #define __CS_CSTREENODE_H__
00021
00022 #include "csutil/csvector.h"
00023
00027 class csTreeNode
00028 {
00029 public:
00030
00032 bool IsLeaf ()
00033 { return children.Length () == 0; }
00034
00036 void RemoveChild (csTreeNode *child)
00037 { int idx = children.Find (child); if (idx != -1) children.Delete (idx); }
00038
00040 void AddChild (csTreeNode *child)
00041 { children.Push (child); child->parent = this; }
00042
00044 csTreeNode (csTreeNode *theParent=NULL)
00045 { parent=theParent; if (parent) parent->children.Push (this); }
00046
00047 virtual ~csTreeNode ()
00048 {
00049 int i;
00050 for(i=children.Length ()-1; i>=0; i--)
00051 delete (csTreeNode*)children.Get (i);
00052 if (parent)
00053 parent->RemoveChild (this);
00054 }
00055
00063 csTreeNode *DSF (bool (*TreeFunc)(csTreeNode *node, void* param, bool stopOnSuccess),
00064 bool (*SelBranch)(csTreeNode *node), void* param, bool stopOnSuccess)
00065 {
00066 csTreeNode *foundNode = NULL;
00067 int i=0;
00068 bool dive;
00069 if (TreeFunc (this, param, stopOnSuccess))
00070 foundNode = this;
00071 while (i<children.Length () && !(foundNode && stopOnSuccess))
00072 {
00073 dive = (SelBranch == NULL) || SelBranch ((csTreeNode*)children.Get (i));
00074 if (dive)
00075 foundNode = ((csTreeNode*)children.Get (i))->DSF (TreeFunc, SelBranch,
00076 param, stopOnSuccess);
00077 i++;
00078 }
00079 return foundNode;
00080 }
00081
00089 csTreeNode *BSF (bool (*TreeFunc)(csTreeNode *node, void* param, bool stopOnSuccess),
00090 bool (*SelBranch)(csTreeNode *node), void* param, bool stopOnSuccess)
00091 {
00092 csTreeNode *node, *foundNode = NULL;
00093 csVector fifo;
00094
00095 fifo.Push (this);
00096 while (fifo.Length () > 0 && !(foundNode && stopOnSuccess))
00097 {
00098 node = (csTreeNode*)fifo.Get (0); fifo.Delete (0);
00099 if (TreeFunc (node, param, stopOnSuccess))
00100 foundNode = node;
00101 if (!node->IsLeaf () && (SelBranch==NULL || SelBranch (node)))
00102 {
00103 int i;
00104 for (i=0; i < node->children.Length (); i++ )
00105 fifo.Push (node->children.Get (i));
00106 }
00107 }
00108 fifo.DeleteAll ();
00109 return foundNode;
00110 }
00111
00112 public:
00113 csTreeNode *parent;
00114 csVector children;
00115 };
00116
00117 #endif // __CS_CSTREENODE_H__