KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > rbtreesizesequence > utils > rbtree > RBTreeNode


1 /**
2 * Copyright (c) 2003 Daffodil Software Ltd., India all rights reserved.
3 * This library is free software; you can redistribute it and/or modify
4 * it under the terms of version 2.1 of the GNU Lesser General Public License as
5 * published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */

16
17
18 package com.daffodilwoods.rbtreesizesequence.utils.rbtree;
19
20 import java.util.Comparator JavaDoc;
21 public class RBTreeNode implements Comparable JavaDoc{
22         public static final boolean BLACK = true;
23         public static final boolean RED = false;
24         public Object JavaDoc key;
25         public RBTreeNode left = null;
26         public RBTreeNode right = null;
27         public RBTreeNode parent;
28         public boolean color = BLACK;
29         private Comparator JavaDoc comparator;
30
31         public void setKey(Object JavaDoc key){
32                 this.key = key;
33         }
34
35         public RBTreeNode getLeft(){
36                 return left;
37         }
38
39         public RBTreeNode getRight(){
40                 return right;
41         }
42
43         public void setLeft(RBTreeNode node){
44                 left = node;
45         }
46         public void setRight(RBTreeNode node){
47                 right = node;
48         }
49         public void setParent(RBTreeNode node){
50                 parent = node;
51         }
52         public RBTreeNode getParent(){
53                 return parent;
54         }
55
56         public void setColor(boolean c){
57                 color = c;
58         }
59         public boolean getColor(){
60                 return color;
61         }
62
63         /**
64          * Make a new cell with given key, value, and parent, and with <tt>null</tt>
65          * child links, and BLACK color.
66          */

67
68         public RBTreeNode(Object JavaDoc key, RBTreeNode parent) {
69                 this.key = key;
70                 this.parent = parent;
71         }
72
73         public RBTreeNode(Object JavaDoc key, RBTreeNode parent, Comparator JavaDoc c) {
74                 this.key = key;
75                 this.parent = parent;
76                 this.comparator = c;
77         }
78
79
80         /**
81          * Returns the key.
82          *
83          * @return the key.
84          */

85         public Object JavaDoc getKey() {
86                 return key;
87         }
88
89         public boolean equals(Object JavaDoc o) {
90                 if (!(o instanceof RBTreeNode))
91                 return false;
92
93                 RBTreeNode e = (RBTreeNode)o;
94                 return key.equals(e.getKey());
95         }
96
97         public String JavaDoc toString() {
98                 return hashCode() + " - " + key.toString();
99         }
100
101         protected void show(){
102         }
103
104
105         public int compareTo(Object JavaDoc obj){
106
107                 if(key == obj) return 0;
108                 return (comparator==null ? ((Comparable JavaDoc)key).compareTo(((RBTreeNode)obj).getKey())
109                                  : comparator.compare(key, obj));
110         }
111
112 }
113
Popular Tags