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
00031 bool IsLeaf ()
00032 { return children.Length () == 0; }
00033
00034 void RemoveChild (csTreeNode *child)
00035 { int idx = children.Find (child); if (idx != -1) children.Delete (idx); }
00036
00037 void AddChild (csTreeNode *child)
00038 { children.Push (child); child->parent = this; }
00039
00040 csTreeNode (csTreeNode *theParent=NULL)
00041 { parent=theParent; if (parent) parent->children.Push (this); }
00042
00043 virtual ~csTreeNode ()
00044 {
00045 int i;
00046 for(i=children.Length ()-1; i>=0; i--)
00047 delete (csTreeNode*)children.Get (i);
00048 if (parent)
00049 parent->RemoveChild (this);
00050 }
00051
00059 csTreeNode *DSF (bool (*TreeFunc)(csTreeNode *node, csSome param, bool stopOnSuccess),
00060 bool (*SelBranch)(csTreeNode *node), csSome param, bool stopOnSuccess)
00061 {
00062 csTreeNode *foundNode = NULL;
00063 int i=0;
00064 bool dive;
00065 if (TreeFunc (this, param, stopOnSuccess))
00066 foundNode = this;
00067 while (i<children.Length () && !(foundNode && stopOnSuccess))
00068 {
00069 dive = (SelBranch == NULL) || SelBranch ((csTreeNode*)children.Get (i));
00070 if (dive)
00071 foundNode = ((csTreeNode*)children.Get (i))->DSF (TreeFunc, SelBranch,
00072 param, stopOnSuccess);
00073 i++;
00074 }
00075 return foundNode;
00076 }
00077
00085 csTreeNode *BSF (bool (*TreeFunc)(csTreeNode *node, csSome param, bool stopOnSuccess),
00086 bool (*SelBranch)(csTreeNode *node), csSome param, bool stopOnSuccess)
00087 {
00088 csTreeNode *node, *foundNode = NULL;
00089 csVector fifo;
00090
00091 fifo.Push (this);
00092 while (fifo.Length () > 0 && !(foundNode && stopOnSuccess))
00093 {
00094 node = (csTreeNode*)fifo.Get (0); fifo.Delete (0);
00095 if (TreeFunc (node, param, stopOnSuccess))
00096 foundNode = node;
00097 if (!node->IsLeaf () && (SelBranch==NULL || SelBranch (node)))
00098 {
00099 int i;
00100 for (i=0; i < node->children.Length (); i++ )
00101 fifo.Push (node->children.Get (i));
00102 }
00103 }
00104 fifo.DeleteAll ();
00105 return foundNode;
00106 }
00107
00108 public:
00109 csTreeNode *parent;
00110 csVector children;
00111 };
00112
00113 #endif // __CS_CSTREENODE_H__