SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DS_Tree.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
20
21
22
23 #ifndef __DS_TREE_H
24 #define __DS_TREE_H
25
26 #include "Export.h"
27 #include "DS_List.h"
28 #include "DS_Queue.h"
29 #include "memoryoverride.h"
30
33 namespace DataStructures
34 {
35 template <class TreeType>
36 class RAK_DLL_EXPORT Tree
37 {
38 public:
39 Tree();
40 Tree(TreeType &inputData);
41 ~Tree();
42 void LevelOrderTraversal(DataStructures::List<Tree*> &output);
43 void AddChild(TreeType &newData);
44 void DeleteDecendants(void);
45
46 TreeType data;
47 DataStructures::List<Tree *> children;
48 };
49
50 template <class TreeType>
51 Tree<TreeType>::Tree()
52 {
53
54 }
55
56 template <class TreeType>
57 Tree<TreeType>::Tree(TreeType &inputData)
58 {
59 data=inputData;
60 }
61
62 template <class TreeType>
63 Tree<TreeType>::~Tree()
64 {
65 DeleteDecendants();
66 }
67
68 template <class TreeType>
69 void Tree<TreeType>::LevelOrderTraversal(DataStructures::List<Tree*> &output)
70 {
71 unsigned i;
72 Tree<TreeType> *node;
73 DataStructures::Queue<Tree<TreeType>*> queue;
74
75 for (i=0; i < children.Size(); i++)
76 queue.Push(children[i]);
77
78 while (queue.Size())
79 {
80 node=queue.Pop();
81 output.Insert(node, _FILE_AND_LINE_);
82 for (i=0; i < node->children.Size(); i++)
83 queue.Push(node->children[i]);
84 }
85 }
86
87 template <class TreeType>
88 void Tree<TreeType>::AddChild(TreeType &newData)
89 {
90 children.Insert(SLNet::OP_NEW<Tree>(newData, _FILE_AND_LINE_));
91 }
92
93 template <class TreeType>
94 void Tree<TreeType>::DeleteDecendants(void)
95 {
96 /*
97 DataStructures::List<Tree*> output;
98 LevelOrderTraversal(output);
99 unsigned i;
100 for (i=0; i < output.Size(); i++)
101 SLNet::OP_DELETE(output[i], _FILE_AND_LINE_);
102 */
103
104 // Already recursive to do this
105 unsigned int i;
106 for (i=0; i < children.Size(); i++)
107 SLNet::OP_DELETE(children[i], _FILE_AND_LINE_);
108 }
109 }
110
111 #endif