001package cnslab.cnsmath; 002public class RBTNode<T extends Comparable> 003{ 004 005 /** 006 * The satellite data in the node. 007 * 008 */ 009 public T data; 010 011 /** 012 * The color node of current node. 013 * 014 * @see #setBlack() 015 * @see #setRed() 016 */ 017 018 protected boolean isRed; 019 020 /** 021 * The parent node of current node. 022 * 023 * @see #parent() 024 * @see #parentTo 025 */ 026 protected RBTNode<T> p; 027 028 /** 029 * The left child node of current node. 030 * 031 * @see #left() 032 * @see #leftTo 033 */ 034 protected RBTNode<T> left; 035 036 037 /** 038 * The right child node of current node. 039 * 040 * @see #right() 041 * @see #rightTo 042 */ 043 protected RBTNode<T> right; 044 045 /** 046 * Sets the data to o. 047 * 048 * @param o The new data of the node. 049 */ 050 public void objectTo(T o) 051 { 052 this.data = o; 053 } 054 055 /** 056 * Returns the satellite data of the node. 057 * 058 * @return The satellite object of the node. 059 */ 060 public T object() 061 { 062 return this.data; 063 } 064 065 public RBTNode(T data) 066 { 067 this.data=data; 068 this.isRed = true ; 069 p = null; 070 left = null; 071 right = null; 072 } 073 074 public RBTNode() 075 { 076 this.data=null; 077 this.isRed = true ; 078 p = null; 079 left = null; 080 right = null; 081 } 082 083 084 /** 085 * Returns the parent of the node. 086 * 087 * @return The parent of the node. 088 */ 089 public RBTNode<T> parent() 090 { 091 return this.p; 092 } 093 094 /** 095 * Returns the right child of the node. 096 * 097 * @return The right child of the node. 098 */ 099 public RBTNode<T> right() 100 { 101 return this.right; 102 } 103 104 /** 105 * Returns the left child of the node. 106 * 107 * @return The left child of the node. 108 */ 109 public RBTNode<T> left() 110 { 111 return this.left; 112 } 113 114 /** 115 * Returns the node as a <code>String</code>. 116 * 117 * @return The node as a <code>String</code>. 118 */ 119 public String toString() 120 { 121 String color; 122 if(isRed) 123 color="red"; 124 else 125 color="black"; 126 //return new String("data: "+data+", color: "+color+", parent: "+p+";"); 127 return new String("data: "+data+", color: "+color+" ;"); 128 } 129 /** 130 * Sets the color of the node to black. 131 * 132 */ 133 public void setBlack() 134 { 135 this.isRed = false; 136 } 137 138 /** 139 * color of the node to black. 140 * 141 */ 142 public boolean isBlack() 143 { 144 return !(this.isRed); 145 } 146 147 148 /** 149 * Sets the color of the node to red. 150 * 151 */ 152 public void setRed() 153 { 154 this.isRed = true; 155 } 156 157 /** 158 * Sets the parent node to parent. 159 * 160 * @param parent The new parent node of the node. 161 */ 162 public void parentTo(RBTNode<T> parent) 163 { 164 this.p = parent; 165 } 166 167 /** 168 * Sets the right child node to parent. 169 * 170 * @param r The new right child node of the node. 171 */ 172 public void rightTo(RBTNode<T> r) 173 { 174 this.right = r; 175 } 176 177 /** 178 * Sets the left child node to parent. 179 * 180 * @param l The new left child node of the node. 181 */ 182 public void leftTo(RBTNode<T> l) 183 { 184 this.left = l; 185 } 186}