-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtree.cpp
More file actions
150 lines (123 loc) · 3.79 KB
/
Copy pathtree.cpp
File metadata and controls
150 lines (123 loc) · 3.79 KB
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
#include "tree.h"
/***************************************************************************/
/** \file tree.cpp
\brief The CNode class implementation */
/***************************************************************************/
/** is this node the first child?
****************************************************************************/
bool CNode::IsFirstChild()
{
if (parentNode)
return (parentNode->childNode == this);
else
return false;
}
/***************************************************************************/
/** is this node the last child?
****************************************************************************/
bool CNode::IsLastChild()
{
if (parentNode)
return (parentNode->childNode->prevNode == this);
else
return false;
}
/***************************************************************************/
/** attach this node to a parent node
****************************************************************************/
void CNode::AttachTo(CNode *newParent)
{
// if this node is already attached to another node, then detach
if (parentNode)
Detach();
parentNode = newParent;
if (parentNode->childNode)
{
prevNode = parentNode->childNode->prevNode;
nextNode = parentNode->childNode;
parentNode->childNode->prevNode->nextNode = this;
parentNode->childNode->prevNode = this;
}
else
{
parentNode->childNode = this; // this is the first child
}
}
/***************************************************************************/
/** attach a child to this node
****************************************************************************/
void CNode::Attach(CNode *newChild)
{
// if the child node is already attached, then detach it
if (newChild->HasParent())
newChild->Detach();
newChild->parentNode = this;
if (childNode)
{
newChild->prevNode = childNode->prevNode;
newChild->nextNode = childNode;
childNode->prevNode->nextNode = newChild;
childNode->prevNode = newChild;
}
else
childNode = newChild;
}
/***************************************************************************/
/** detach node from parent
****************************************************************************/
void CNode::Detach()
{
// if this node is the first child of the parent (first in list)
// then the parent points to the next child in the list
if (parentNode && parentNode->childNode == this)
{
if (nextNode != this)
parentNode->childNode = nextNode;
else
parentNode->childNode = NULL; // no next child
}
// get rid of links
prevNode->nextNode = nextNode;
nextNode->prevNode = prevNode;
// now this node is not in the list
prevNode = this;
nextNode = this;
}
/***************************************************************************/
/** count the number of nodes
****************************************************************************/
int CNode::CountNodes()
{
if (childNode)
return childNode->CountNodes() + 1;
else
return 1;
}
/***************************************************************************/
/** constructor
****************************************************************************/
CNode::CNode() // setup node
{
parentNode = childNode = NULL;
prevNode = nextNode = this;
}
/***************************************************************************/
/** constructor
****************************************************************************/
CNode::CNode(CNode *node)
{
parentNode = childNode = NULL; // setup and attach this node to node
prevNode = nextNode = this;
AttachTo(node);
}
/***************************************************************************/
/** destructor
****************************************************************************/
CNode::~CNode()
{
Detach(); // detach from hierarchy
while (childNode) // delete all children
{
delete childNode;
}
}