DS_Tree.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * 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. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file DS_Tree.h
  11. /// \internal
  12. /// \brief Just a regular tree
  13. ///
  14. #ifndef __DS_TREE_H
  15. #define __DS_TREE_H
  16. #include "Export.h"
  17. #include "DS_List.h"
  18. #include "DS_Queue.h"
  19. #include "RakMemoryOverride.h"
  20. /// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures
  21. /// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish.
  22. namespace DataStructures
  23. {
  24. template <class TreeType>
  25. class RAK_DLL_EXPORT Tree
  26. {
  27. public:
  28. Tree();
  29. Tree(TreeType &inputData);
  30. ~Tree();
  31. void LevelOrderTraversal(DataStructures::List<Tree*> &output);
  32. void AddChild(TreeType &newData);
  33. void DeleteDecendants(void);
  34. TreeType data;
  35. DataStructures::List<Tree *> children;
  36. };
  37. template <class TreeType>
  38. Tree<TreeType>::Tree()
  39. {
  40. }
  41. template <class TreeType>
  42. Tree<TreeType>::Tree(TreeType &inputData)
  43. {
  44. data=inputData;
  45. }
  46. template <class TreeType>
  47. Tree<TreeType>::~Tree()
  48. {
  49. DeleteDecendants();
  50. }
  51. template <class TreeType>
  52. void Tree<TreeType>::LevelOrderTraversal(DataStructures::List<Tree*> &output)
  53. {
  54. unsigned i;
  55. Tree<TreeType> *node;
  56. DataStructures::Queue<Tree<TreeType>*> queue;
  57. for (i=0; i < children.Size(); i++)
  58. queue.Push(children[i]);
  59. while (queue.Size())
  60. {
  61. node=queue.Pop();
  62. output.Insert(node, _FILE_AND_LINE_);
  63. for (i=0; i < node->children.Size(); i++)
  64. queue.Push(node->children[i]);
  65. }
  66. }
  67. template <class TreeType>
  68. void Tree<TreeType>::AddChild(TreeType &newData)
  69. {
  70. children.Insert(RakNet::OP_NEW<Tree>(newData, _FILE_AND_LINE_));
  71. }
  72. template <class TreeType>
  73. void Tree<TreeType>::DeleteDecendants(void)
  74. {
  75. /*
  76. DataStructures::List<Tree*> output;
  77. LevelOrderTraversal(output);
  78. unsigned i;
  79. for (i=0; i < output.Size(); i++)
  80. RakNet::OP_DELETE(output[i], _FILE_AND_LINE_);
  81. */
  82. // Already recursive to do this
  83. unsigned int i;
  84. for (i=0; i < children.Size(); i++)
  85. RakNet::OP_DELETE(children[i], _FILE_AND_LINE_);
  86. }
  87. }
  88. #endif
粤ICP备19079148号