Node.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Node
  3. *
  4. * A node element of a Linked-List binary tree
  5. */
  6. public class Node {
  7. protected int element;
  8. protected Node parent = null, left = null, right = null;
  9. // Constructors
  10. public Node() { }
  11. public Node(int e) {
  12. element = e;
  13. }
  14. // Getters
  15. public int getElement() {
  16. return element;
  17. }
  18. public Node getParent() {
  19. return parent;
  20. }
  21. public Node getLeftSibling() {
  22. return this.hasParent() ? this.getParent().getLeftChild() : null;
  23. }
  24. public Node getRightSibling() {
  25. return this.hasParent() ? this.getParent().getRightChild() : null;
  26. }
  27. public Node getLeftChild() {
  28. return left;
  29. }
  30. public Node getRightChild() {
  31. return right;
  32. }
  33. /*public Node getLeftSibling() {
  34. int depth = 0;
  35. Node temp = this;
  36. boolean atTarget = false;
  37. while(!atTarget) {
  38. if (temp.hasParent()) {
  39. temp = temp.getParent();
  40. ++depth;
  41. } else {
  42. // Special case for the root of the tree and its left subtree
  43. temp = null;
  44. return temp;
  45. }
  46. if(temp.isRightChild()) {
  47. temp = temp.getParent();
  48. ++depth;
  49. atTarget = true;
  50. }
  51. }
  52. while(depth > 0) {
  53. if(temp.hasLeftChild()) {
  54. temp = temp.getLeftChild();
  55. --depth;
  56. } else {
  57. // Not enough left children to get to a left sibling of this node
  58. temp = null;
  59. return temp;
  60. }
  61. }
  62. return temp;
  63. }*/
  64. /*public Node getRightSibling() {
  65. int depth = 0;
  66. Node temp = this;
  67. boolean atTarget = false;
  68. while(!atTarget) {
  69. if (temp.hasParent()) {
  70. temp = temp.getParent();
  71. ++depth;
  72. } else {
  73. // Special case for the root of the tree and its right subtree
  74. temp = null;
  75. return temp;
  76. }
  77. if(temp.isRightChild() || temp.isRoot()) { atTarget = true; }
  78. }
  79. while(depth > 0) {
  80. if(temp.hasRightChild()) {
  81. temp = temp.getRightChild();
  82. --depth;
  83. } else {
  84. // Not enough left children to get to a left sibling of this node
  85. temp = null;
  86. return temp;
  87. }
  88. }
  89. if(temp == this) { temp = null; }
  90. return temp;
  91. }*/
  92. // Setters
  93. public void setElement(int e) {
  94. element = e;
  95. }
  96. public void setParent(Node n) {
  97. parent = n;
  98. }
  99. public void setLeftChild(Node n) {
  100. left = n;
  101. }
  102. public void setRightChild(Node n) {
  103. right = n;
  104. }
  105. // Booleans
  106. public boolean hasParent() {
  107. return this.getParent() != null;
  108. }
  109. public boolean hasSibling() {
  110. if(this.isLeftChild()) { return this.getParent().hasRightChild(); }
  111. else { return this.getParent().hasLeftChild(); }
  112. }
  113. public boolean hasLeftSibling() {
  114. if(this.isRightChild() && this.hasSibling()) { return true; }
  115. else { return false; }
  116. }
  117. public boolean hasRightSibling() {
  118. if(this.isLeftChild() && this.hasSibling()) { return true; }
  119. else { return false; }
  120. }
  121. public boolean hasLeftChild() {
  122. return this.getLeftChild() != null;
  123. }
  124. public boolean hasRightChild() {
  125. return this.getRightChild() != null;
  126. }
  127. public boolean isRoot() {
  128. return !this.hasParent();
  129. }
  130. public boolean isLeftChild() {
  131. return this.getParent() == null ? false : this.getParent().getLeftChild() == null ? false : this.getParent().getLeftChild() == this;
  132. }
  133. public boolean isRightChild() {
  134. return this.getParent() == null ? false : this.getParent().getRightChild() == null ? false : this.getParent().getRightChild() == this;
  135. }
  136. }