1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.avltree;
21
22
23
24
25
26
27
28 public class LinkedAvlMapNode<K, V>
29 {
30
31 K key;
32
33
34 SingletonOrOrderedSet<V> value;
35
36
37 LinkedAvlMapNode<K, V> left;
38
39
40 LinkedAvlMapNode<K, V> right;
41
42
43 LinkedAvlMapNode<K, V> next;
44
45
46 LinkedAvlMapNode<K, V> previous;
47
48 int depth;
49 int index;
50
51 boolean isLeft;
52 int height = 1;
53
54
55
56
57
58
59
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 }