|
| 1 | +package AVLTree; |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * Represents an AVL leave a an AVLTree. |
| 6 | + * |
| 7 | + * @author Guillermo Facundo Colunga |
| 8 | + * @version 2.1 |
| 9 | + * @param <T> Type of data that the AVLNode will contain. |
| 10 | + */ |
| 11 | +public class AVLNode<T extends Comparable<T>> { |
| 12 | + |
| 13 | + private T element; |
| 14 | + private AVLNode<T> left, right; |
| 15 | + private int height; |
| 16 | + |
| 17 | + /** |
| 18 | + * AVLNode Constructor. Only requires an element for the current AVLNode. |
| 19 | + * The left child and the right child will be set to null. |
| 20 | + * |
| 21 | + * @param element to be set in the current AVLNode. |
| 22 | + * @throws Exception if element is null. |
| 23 | + */ |
| 24 | + public AVLNode( T element ) { |
| 25 | + element( element ); |
| 26 | + left( null ); |
| 27 | + right( null ); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * AVLNode Constructor. Requires the element for the current node, the |
| 32 | + * AVLNode of the left and the one for the right. |
| 33 | + * |
| 34 | + * @param element for the current node |
| 35 | + * @param left AVLNode |
| 36 | + * @param right AVLNode |
| 37 | + * @throws Exception Exception if element, left or right is null. |
| 38 | + */ |
| 39 | + public AVLNode( T element, AVLNode<T> left, AVLNode<T> right ) { |
| 40 | + element( element ); |
| 41 | + left( left ); |
| 42 | + right( right ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the actual value of the node. Notice that if the current value is |
| 47 | + * null will return null. |
| 48 | + * |
| 49 | + * @return the value of the node. In other words the element that the node |
| 50 | + * contains. |
| 51 | + */ |
| 52 | + public T element() { |
| 53 | + return this.element; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets the value of the node. Because of the implementation this method |
| 58 | + * will accept null values. |
| 59 | + * |
| 60 | + * @param element to be set. |
| 61 | + */ |
| 62 | + public void element( T element ) { |
| 63 | + this.element = element; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Return the height of the subtree starting at the node. |
| 68 | + * |
| 69 | + * @return the height as an Integer. |
| 70 | + */ |
| 71 | + public int height() { |
| 72 | + updateHeight(); |
| 73 | + return this.height; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Setter for the height property. |
| 78 | + * |
| 79 | + * @param height to be set as new in the node. |
| 80 | + */ |
| 81 | + public void height( int height ) { |
| 82 | + if (height < 0) |
| 83 | + throw new IllegalArgumentException( "The height cannot be negative" ); |
| 84 | + this.height = height; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns the AVLNode that is placed at the left. So its value is less than |
| 89 | + * the actual node. If there is no left child will return null. |
| 90 | + * |
| 91 | + * @return the AVLNode that is placed at the left. Null if there isn't. |
| 92 | + */ |
| 93 | + public AVLNode<T> left() { |
| 94 | + return this.left; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Sets the AVLNode that is placed at the left. Because of the |
| 99 | + * implementation this method will accept null values. |
| 100 | + * |
| 101 | + * @param left the node to be set. |
| 102 | + */ |
| 103 | + public void left( AVLNode<T> left ) { |
| 104 | + this.left = left; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns the AVLNode that is placed at the right. So its value is grater |
| 109 | + * than the actual node. If there is no left child will return null. |
| 110 | + * |
| 111 | + * @return the AVLNode that is placed at the right. Null if there isn't. |
| 112 | + */ |
| 113 | + public AVLNode<T> right() { |
| 114 | + return this.right; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Sets the AVLNode that is placed at the left. Because of the |
| 119 | + * implementation this method will accept null values. |
| 120 | + * |
| 121 | + * @param right the node to be set. |
| 122 | + */ |
| 123 | + public void right( AVLNode<T> right ) { |
| 124 | + this.right = right; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * toString default method. Returns as an String the value of the element. |
| 129 | + * |
| 130 | + * @return the value of the node. |
| 131 | + */ |
| 132 | + @Override public String toString() { |
| 133 | + return this.element().toString() + "(" + balance() + ")"; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Prints the value of the toString method. |
| 138 | + */ |
| 139 | + public void print() { |
| 140 | + System.out.println( this.toString() ); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Updates the height of the tree from a given root or node. |
| 145 | + */ |
| 146 | + protected void updateHeight() { |
| 147 | + // If the node doesn't have children, its height is just 0 |
| 148 | + if (left() == null && right() == null) |
| 149 | + height( 0 ); |
| 150 | + |
| 151 | + // If it has a child on one side, its height is its child's height plus |
| 152 | + // one |
| 153 | + else if (left() != null && right() == null) |
| 154 | + height( 1 + left().height() ); |
| 155 | + |
| 156 | + else if (left() == null && right() != null) |
| 157 | + height( 1 + right().height() ); |
| 158 | + |
| 159 | + // If it has one child on each side, its height will be the greatest of |
| 160 | + // its childrens's height plus one |
| 161 | + else if (left().height() > right().height()) |
| 162 | + height( 1 + left().height() ); |
| 163 | + else |
| 164 | + height( 1 + right().height() ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Calculates the balance factor of the node. |
| 169 | + * |
| 170 | + * @return the balance factor of the node. |
| 171 | + */ |
| 172 | + public int balance() { |
| 173 | + return calculateBalance(); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * By means of the height returns the BF of the node. Three cases, the node |
| 178 | + * has no subtrees, has one subtree or has two subtrees. |
| 179 | + * |
| 180 | + * @return |
| 181 | + */ |
| 182 | + private int calculateBalance() { |
| 183 | + |
| 184 | + // If the node doesn't have children, its BF is just 0 |
| 185 | + if (left() == null && right() == null) |
| 186 | + return 0; |
| 187 | + // If it has a child on one side |
| 188 | + else if (right() == null) |
| 189 | + return -1 - left().height(); |
| 190 | + else if (left() == null && right() != null) |
| 191 | + return 1 + right().height(); |
| 192 | + // If it has one child on each side |
| 193 | + else |
| 194 | + return ( right().height() - left().height() ); |
| 195 | + } |
| 196 | +} |
0 commit comments