View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.server.core.avltree;
21  
22  
23  /**
24   * A linked AVL tree node with support to store value along with a key.
25   *
26   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
27   */
28  public class LinkedAvlMapNode<K, V>
29  {
30      /** The key stored in the node */
31      K key;
32  
33      /** the value stored in the node */
34      SingletonOrOrderedSet<V> value;
35  
36      /** The left child */
37      LinkedAvlMapNode<K, V> left;
38  
39      /** The right child */
40      LinkedAvlMapNode<K, V> right;
41  
42      /** The next node, superior to the current node */
43      LinkedAvlMapNode<K, V> next;
44  
45      /** The previous node, inferior to the current node */
46      LinkedAvlMapNode<K, V> previous;
47  
48      int depth;
49      int index;
50  
51      boolean isLeft;
52      int height = 1;
53  
54  
55      /**
56       * Creates a new instance of LinkedAvlNode, containing a given value.
57       *
58       * @param theKey the stored key on the topmost node
59       * @param theValue The stored value on the topmost node
60       */
61      public LinkedAvlMapNode( K theKey, V theValue )
62      {
63          key = theKey;
64          value = new SingletonOrOrderedSet<>( theValue );
65          left = null;
66          right = null;
67      }
68  
69  
70      public void setLeft( LinkedAvlMapNode<K, V> left )
71      {
72          this.left = left;
73      }
74  
75  
76      public void setRight( LinkedAvlMapNode<K, V> right )
77      {
78          this.right = right;
79      }
80  
81  
82      public LinkedAvlMapNode<K, V> getNext()
83      {
84          return next;
85      }
86  
87  
88      public LinkedAvlMapNode<K, V> getPrevious()
89      {
90          return previous;
91      }
92  
93  
94      public LinkedAvlMapNode<K, V> getLeft()
95      {
96          return left;
97      }
98  
99  
100     public LinkedAvlMapNode<K, V> getRight()
101     {
102         return right;
103     }
104 
105 
106     public K getKey()
107     {
108         return key;
109     }
110 
111 
112     public SingletonOrOrderedSet<V> getValue()
113     {
114         return value;
115     }
116 
117 
118     public boolean isLeaf()
119     {
120         return ( right == null && left == null );
121     }
122 
123 
124     public int getDepth()
125     {
126         return depth;
127     }
128 
129 
130     public void setDepth( int depth )
131     {
132         this.depth = depth;
133     }
134 
135 
136     public int getHeight()
137     {
138         return height;
139     }
140 
141 
142     public void setNext( LinkedAvlMapNode<K, V> next )
143     {
144         this.next = next;
145     }
146 
147 
148     public void setPrevious( LinkedAvlMapNode<K, V> previous )
149     {
150         this.previous = previous;
151     }
152 
153 
154     public int computeHeight()
155     {
156 
157         if ( right == null && left == null )
158         {
159             height = 1;
160             return height;
161         }
162 
163         int lh, rh;
164 
165         if ( isLeft )
166         {
167             lh = ( left == null ? -1 : left.computeHeight() );
168             rh = ( right == null ? -1 : right.getHeight() );
169         }
170         else
171         {
172             rh = ( right == null ? -1 : right.computeHeight() );
173             lh = ( left == null ? -1 : left.getHeight() );
174         }
175 
176         height = 1 + Math.max( lh, rh );
177 
178         return height;
179     }
180 
181 
182     public int getBalance()
183     {
184         int lh = ( left == null ? 0 : left.computeHeight() );
185         int rh = ( right == null ? 0 : right.computeHeight() );
186 
187         return ( rh - lh );
188     }
189 
190 
191     public int getIndex()
192     {
193         return index;
194     }
195 
196 
197     public void setIndex( int index )
198     {
199         this.index = index;
200     }
201 
202 
203     @Override
204     public String toString()
205     {
206         return "[" + key + ", [" + value + "]" + "]";
207     }
208 
209 }