Node.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Mixture of my stuff and stuff from the 216 book. Mostly mine.
  3. *
  4. * Node:
  5. * Represents a node in an AVL Tree implementation of a Dictionary.
  6. */
  7. public class Node<T> {
  8. private int leftHeight;
  9. private int rightHeight;
  10. private boolean isLeft = false, isRight = false;
  11. protected Node<T> left, right, parent;
  12. protected T key, element;
  13. /**
  14. * Constructor
  15. */
  16. Node (T k) {
  17. key = k;
  18. element = null;
  19. left = null;
  20. right = null;
  21. parent = null;
  22. this.setLeftHeight(0);
  23. this.setRightHeight(0);
  24. }
  25. Node (T k, T e) {
  26. key = k;
  27. element = e;
  28. left = null;
  29. right = null;
  30. parent = null;
  31. this.setLeftHeight(0);
  32. this.setRightHeight(0);
  33. }
  34. /**
  35. * Booleans
  36. */
  37. public boolean isLeaf () {
  38. return (left == null && right == null);
  39. }
  40. public boolean hasLeft () {
  41. return (left != null);
  42. }
  43. public boolean hasRight () {
  44. return (right != null);
  45. }
  46. public boolean isLeftChild () {
  47. return (isLeft);
  48. }
  49. public boolean isRightChild () {
  50. return (isRight);
  51. }
  52. /**
  53. * Getters
  54. */
  55. public int getBalance () {
  56. return (rightHeight - leftHeight);
  57. }
  58. public T getElement () {
  59. return element;
  60. }
  61. public T getKey () {
  62. return key;
  63. }
  64. public Node<T> getLeft () {
  65. return left;
  66. }
  67. public Node<T> getRight () {
  68. return right;
  69. }
  70. public int getLeftHeight () {
  71. return leftHeight;
  72. }
  73. public int getRightHeight () {
  74. return rightHeight;
  75. }
  76. public int getHeight () {
  77. return (Math.max(leftHeight,rightHeight));
  78. }
  79. public int getNumChildren () {
  80. int children = 0;
  81. if (left != null)
  82. children = 1 + left.getNumChildren();
  83. if (right != null)
  84. children = children + 1 + right.getNumChildren();
  85. return children;
  86. }
  87. public Node<T> getParent () {
  88. return parent;
  89. }
  90. /**
  91. * Setters
  92. */
  93. public void setLeft (Node<T> child) {
  94. left = child;
  95. if (child != null)
  96. child.setIsLeft();
  97. }
  98. public void setRight (Node<T> child) {
  99. right = child;
  100. if (child != null)
  101. child.setIsRight();
  102. }
  103. public void setIsLeft () {
  104. isLeft = true;
  105. }
  106. public void setIsRight () {
  107. isRight = true;
  108. }
  109. public void unsetIsLeft () {
  110. isLeft = false;
  111. }
  112. public void unsetIsRight () {
  113. isRight = false;
  114. }
  115. public void setLeftHeight (int h) {
  116. leftHeight = h;
  117. }
  118. public void setRightHeight (int h) {
  119. rightHeight = h;
  120. }
  121. public void setKey (T k) {
  122. key = k;
  123. }
  124. public void setElement (T e) {
  125. element = e;
  126. }
  127. public void setParent (Node<T> p) {
  128. parent = p;
  129. }
  130. /**
  131. * toString
  132. */
  133. public String toString () {
  134. return (key + " " + element);
  135. }
  136. } // End Node