Node.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Node<T>:
  3. * Node of a Search Heap of type T.
  4. *
  5. * Designed and written by Jonathan E. Landrum.
  6. *
  7. * Copyright (c) 2014-2015 Jonathan E. Landrum. All Rights Reserved.
  8. */
  9. public class Node<T> {
  10. // T objects stored in this Node
  11. private T[] arr = new T[4];
  12. private int numElements;
  13. // Pointers to children of this Node
  14. private Node<T> parent = null;
  15. private Node<T> leftSibling = null;
  16. private Node<T> rightSibling = null;
  17. private Node<T> leftChild = null;
  18. private Node<T> rightChild = null;
  19. /*
  20. * Constructors
  21. */
  22. public Node() { numElements = 0; }
  23. public Node(T minimum) {
  24. min = minimum;
  25. numElements = 1;
  26. }
  27. public Node(T minimum, T maximum) {
  28. min = minimum;
  29. max = maximum;
  30. numElements = 2;
  31. }
  32. public Node(T minimum, T median, T maximum) {
  33. min = minimum;
  34. med = median;
  35. max = maximum;
  36. numElements = 3;
  37. }
  38. /*
  39. * Getters
  40. */
  41. public T getMin() { return min; }
  42. public T getMed() { return med; }
  43. public T getMax() { return max; }
  44. public Node<T> getParent() { return parent; }
  45. public Node<T> getLeftSibling() { return leftSibling; }
  46. public Node<T> getRightSibling() { return rightSibling; }
  47. public Node<T> getLeftChild() { return leftChild; }
  48. public Node<T> getRightChild() { return rightChild; }
  49. public int getNumElements() { return numElements; }
  50. /*
  51. * Setters
  52. */
  53. public void setParent(Node<T> obj) { parent = obj; }
  54. public void setLeftSibling(Node<T> obj) { leftSibling = obj; }
  55. public void setRightSibling(Node<T> obj) { rightSibling = obj; }
  56. public void setLeftChild(Node<T> obj) { leftChild = obj; }
  57. public void setRightChild(Node<T> obj) { rightChild = obj; }
  58. public void setNumElements(int n) { numElements = n; }
  59. /*
  60. * Booleans
  61. */
  62. public boolean isRoot() { return parent == null; }
  63. public boolean isFull() {
  64. return (this.hasMin() && this.hasMed() && this.hasMax());
  65. }
  66. public boolean isLeftChild() { return this == parent.getLeftChild(); }
  67. public boolean isRightChild() { return this == parent.getRightChild(); }
  68. public boolean hasMin() { return min != null; }
  69. public boolean hasMed() { return med != null; }
  70. public boolean hasMax() { return max != null; }
  71. public boolean hasParent() { return parent != null; }
  72. public boolean hasLeftSibling() { return leftSibling != null; }
  73. public boolean hasRightSibling() { return rightSibling != null; }
  74. public boolean hasLeftChild() { return leftChild != null; }
  75. public boolean hasRightChild() { return rightChild != null; }
  76. // /*
  77. // * Comparable
  78. // */
  79. // public int compareTo(Node<T> node) {
  80. // return this.getMax().compareTo(node.getMin());
  81. // }
  82. /*
  83. * toString
  84. */
  85. public String toString() {
  86. String res = "";
  87. if (this.hasMin()) {
  88. res += "(" + this.getMin();
  89. if (this.hasMed()) {
  90. res += ", " + this.getMed();
  91. }
  92. if (this.hasMax()) {
  93. res += ", " + this.getMax();
  94. }
  95. res += ")";
  96. }
  97. return res;
  98. }
  99. }