Tree.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. ///----------------------------------------------------------------------------
  7. ///----------------------------------------------------------------------------
  8. ///
  9. /// class TreeNode
  10. ///
  11. /// General DataStructure for an N-ary tree.
  12. ///
  13. ///----------------------------------------------------------------------------
  14. ///----------------------------------------------------------------------------
  15. template<class T, int N>
  16. class TreeNode
  17. {
  18. // Data
  19. private:
  20. T value;
  21. TreeNode * children[N];
  22. TreeNode<T, N> * parent;
  23. // Constructor
  24. public:
  25. TreeNode(TreeNode<T, N> * parent = NULL)
  26. {
  27. this->parent = parent;
  28. for(int i = 0; i < N; i++)
  29. {
  30. this->children[i] = NULL;
  31. }
  32. }
  33. // Methods
  34. public:
  35. bool ChildExistsAt(int i)
  36. {
  37. return NULL != this->children[i];
  38. }
  39. TreeNode<T, N> * GetChildAt(int i)
  40. {
  41. return this->children[i];
  42. }
  43. void SetChildAt(int i, TreeNode<T, N> *node)
  44. {
  45. this->children[i] = node;
  46. }
  47. TreeNode<T, N> * GetParent()
  48. {
  49. return this->parent;
  50. }
  51. void SetParent(TreeNode<T, N>* parent)
  52. {
  53. this->parent = parent;
  54. }
  55. T * GetValue()
  56. {
  57. return &this->value;
  58. }
  59. void SetValue(const T value)
  60. {
  61. this->value = value;
  62. }
  63. };