StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StiTreeNode.cxx
1 #include <assert.h>
2 #include "StiTreeNode.h"
3 #include "Factory.h"
4 
5 
6 //______________________________________________________________________________
10 {
11  reset();
12 }
13 //______________________________________________________________________________
14 void StiTreeNode::reset()
15 {
16  parent = 0; children[0]=0; children[1]=0;
17 }
18 //______________________________________________________________________________
27 //______________________________________________________________________________
28 void StiTreeNode::remove(int childIndex)
29 {
30  StiTreeNode *node =0;
31  if (childIndex>=0) { //remove children
32  node =children[childIndex];children[childIndex]=0;
33  if (!childIndex) {children[0]=children[1]; children[1]=0;}
34  if (!node) return;
35  if (node->parent!=this) return;
36  node->remove(0);
37 
38  } else {//remove parent
39  node = parent; parent=0;
40  if (!node) return;
41  node->remove(0);
42  node->remove(0);
43  node->remove(-1);
44  }
45  node->reset();
46  BFactory::Free(node);
47 }
48 //______________________________________________________________________________
49 StiTreeNode *StiTreeNode::disconnect(int all)
50 {
51  StiTreeNode *node =0;
52  node = parent; parent=0;
53  if (!node) return 0;
54  assert(node->children[0]==this);
55  assert(!children[1]);
56  node->children[0]=0;
57  if (all) return node;
58  node->children[0]=children[0];
59  if (children[0]) children[0]->parent=node;
60  children[0]=0;
61  return node;
62 }
63 
64 //______________________________________________________________________________
72 //______________________________________________________________________________
74 {
75  parent = newParent;
76 }
77 
78 //______________________________________________________________________________
82 //______________________________________________________________________________
84 {
85  return parent;
86 }
87 
94 {
95  return children[index];
96 }
97 
98 //______________________________________________________________________________
102 //______________________________________________________________________________
104 {
105  int n=0;
106  if(children[0]) n++;
107  if(children[1]) n++;
108  return n;
109 }
110 
111 //______________________________________________________________________________
113 // node's tree. Returns 0 if this node is the last node of the
114 // traversal. This is an inefficient way to traverse the entire tree; use
115 // an enumeration, instead.
116 //
117 // @see #preorderEnumeration
118 // @return the node that follows this node in a preorder traversal, or
119 // 0 if this node is last
120 //______________________________________________________________________________
122 {
123  return children[0];
124 }
125 
126 
127 //______________________________________________________________________________//
128 //Returns the node that precedes this node in a preorder traversal of
129 // this node's tree. Returns 0 if this node is the first node of the
130 // traveral -- the root of the tree. This is an inefficient way to
131 // traverse the entire tree; use an enumeration, instead.
132 //
133 // @see #preorderEnumeration
134 // @return the node that precedes this node in a preorder traversal, or
135 // 0 if this node is the first
136 //______________________________________________________________________________
137 StiTreeNode * StiTreeNode::getPrevNode() const
138 {
139  return parent;
140 }
141 //______________________________________________________________________________
142 const StiTreeNode& StiTreeNode::operator=(const StiTreeNode& node)
143 {
144  reset();
145  parent = node.parent;
146  return *this;
147 }
148 //______________________________________________________________________________
149 void StiTreeNode::add(StiTreeNode * newChild,int direction)
150 {
151  if (direction==0) {
152  assert(!children[1]);
153  children[1]=children[0];
154  children[0]=newChild;
155  newChild->setParent(this);
156 
157  } else {
158 
159  assert(!getParent());
160  setParent(newChild);
161  newChild->children[0]=this;
162  newChild->children[1]=0;
163  }
164 }
165 //______________________________________________________________________________
166 StiTreeNode *StiTreeNode::getLastNode() const
167 {
168  const StiTreeNode *node = this;
169  const StiTreeNode *next = 0;
170  while ((next = node->getNextNode())) {
171  node=next;
172  assert(node!=this);
173  }
174  return (StiTreeNode*)node;
175 }
176 //______________________________________________________________________________
177 StiTreeNode *StiTreeNode::getFirstNode() const
178 {
179  const StiTreeNode *node = this;
180  const StiTreeNode *back = 0;
181  while ((back = node->getPrevNode())) {
182  node=back;
183  assert(node!=this);
184  }
185  return (StiTreeNode*)node;
186 }
187 //______________________________________________________________________________
188 void StiTreeNode::cutTail(int direction)
189 {
190  direction = (direction)? -1:0;
191  remove(direction);
192 }
193 //______________________________________________________________________________
194 void StiTreeNode::remove(StiTreeNode **fstNode,StiTreeNode **lstNode)
195 {
196  assert(!children[1]);
197  StiTreeNode **kLeft = (parent )? &parent->children[0]:fstNode;
198  StiTreeNode **kRite = (children[0])? &children[0]->parent:lstNode;
199  *kLeft = children[0];
200  *kRite = parent;
201 }
202 
203 
204 
205 
206 
void setParent(StiTreeNode *newParent)
Definition: StiTreeNode.cxx:73
StiTreeNode * getNextNode() const
Returns the node that follows this node in a preorder traversal of this.
StiTreeNode * getChildAt(int index) const
Definition: StiTreeNode.cxx:93
StiTreeNode * getParent() const
Definition: StiTreeNode.cxx:83
int getChildCount() const
static void Free(void *obj)
Free an object for reuse.
Definition: Factory.h:73
void remove(int childIndex)
Definition: StiTreeNode.cxx:28