Skip to content
Snippets Groups Projects
Node.java 2.14 KiB
Newer Older
  • Learn to ignore specific revisions
  • Lawrence Chung's avatar
    Lawrence Chung committed
    package search;
    
    
    Haley Glavina's avatar
    Haley Glavina committed
    // An abstract data type that represents a node in a Red Black Search Tree
    
    public class Node<Key extends Comparable<Key>, Value>{
    
    	private final Key key;
    
    	private Value val;
    	private Node<Key, Value> left, right;
    
    Haley Glavina's avatar
    Haley Glavina committed
    	private int n; 
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    	private boolean color;
    
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Constructor
    	 * @param key Used to assign order amongst the nodes/implement comparability
    	 * @param val The value stored at a node
    	 * @param n How many nodes are in the subtree beneath this node (inclusive)
    	 * @param color True represents a red connection between the current node and its parent, black represents false
    	 */
    
    	public Node(Key key, Value val, int n, boolean color){
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    		this.key = key;
    		this.val = val;
    		this.n = n;
    		this.color = color;
    	}
    	
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Getter for a node's key
    	 * @return The node's key
    	 */
    
    	public Key key(){
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    		return this.key;
    	}
    	
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Getter for a node's value
    	 * @return The node's value
    	 */
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    		return this.val;
    	} 
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Setter for a node's value
    	 * @param val New value being assigned
    	 */
    
    	public void val(Value val){
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    		this.val = val;
    	}
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Getter for a node's left child
    	 * @return The node's left child
    	 */
    
    	public Node<Key, Value> left(){
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    		return this.left;
    	}
    	
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Setter for a node's left child
    	 * @param left The node's new left child
    	 */
    
    	public void left(Node<Key, Value> left){
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    		this.left = left;
    	}
    	
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Getter for a node's right child
    	 * @return The node's right child
    	 */
    
    	public Node<Key, Value> right(){
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    		return this.right;
    	}
    	
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Setter for a node's right child
    	 * @param left The node's new right child
    	 */
    
    	public void right(Node<Key, Value> right){
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    		this.right = right;
    	}
    	
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Getter for the subtree size beneath the current node
    	 * @return Subtree size
    	 */
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    	public int n(){
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    		return this.n;
    	}
    	
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Setter for a subtree's size
    	 * @param n New subtree size
    	 */
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    	public void n(int n){
    		this.n = n;
    	}
    	
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Getter method for a node's color
    	 * @return The node's color
    	 */
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    	public boolean color(){
    		return this.color;
    	}
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    	
    
    Haley Glavina's avatar
    Haley Glavina committed
    	/**
    	 * Setter for a node's color
    	 * @param color New node color
    	 */
    
    Lawrence Chung's avatar
    Lawrence Chung committed
    	public void color(boolean color){
    		this.color = color;
    	}