Location: PHPKode > scripts > LLRB Tree > llrb-tree/LLRBNode.php
<?php

/*
 * 		LLRBNode is copyright � 2009. EarthWalk Software.
 * 		Licensed under the Academic Free License version 3.0
 *      Refer to the file named Copyright provided with the source.
 */
/**
 * 
 * LLRBNode.
 * 
 * LLRBNode implements the bNode abstract class to create a LLRB node container (object).
 * @author Jay Wheeler.
 * @version 1.0
 * @copyright � 2009. EarthWalk Software.
 * @license refer to Copyright file provided with the source.
 * @package LLRBTree.
 * @subpackage LLRBNode.
 */
class LLRBNode extends bNode
{
	const RED = true;
	const BLACK = false;

	const LEFT = true;
	const RIGHT = false;

	/**
	 * __construct.
	 * 
	 * Create the class object.
	 * @param $key object key.
	 * @param $data (optional) object data.
	 * @return node object.
	 */
	public function __construct($key, $data=null)
	{
		parent::__construct($key, $data, self::RED);
	}

	/**
	 * key.
	 * 
	 * Set/get the node key.
	 * @param $key the key to set, null to query only.
	 * @return $key
	 */
	public function key($key=null)
	{
		if ($key !== null)
		{
			$this->nkey = $key;
		}
		return $this->nkey;
	}

	/**
	 * data.
	 * 
	 * Set/get the node data value.
	 * @param $data the value of the data to set, null to query only.
	 * @return $data
	 */
	public function data($data=null)
	{
		if ($data !== null)
		{
			$this->ndata = $data;
		}
		return $this->ndata;
	}

	/**
	 * setNull.
	 * 
	 * Set the node data value to null.
	 * @return null
	 */
	public function setNull()
	{
		$this->ndata = null;
	}

	/**
	 * compare.
	 * 
	 * Compare the supplied with the nodes key.  Returns 0 if equal, 1 if >, -1 if <
	 * @param $keyNode contains the key to be compared
	 * @return integer 0 = equal, 1 = >, -1 = <
	 */
	public function compare(self $keyNode)
	{
		return (($keyNode->nkey == $this->nkey) ? 0 : (($keyNode->nkey > $this->nkey) ? 1 : -1));
	}

	/**
	 * left.
	 * 
	 * Set/get the left child pointer.
	 * @param $link link to the left child (null to query).
	 * @return $link to the left pointer.
	 */
	public function left($link=false)
	{
		if ($link !== false)
		{
			$this->nleft = $link;
		}
		return $this->nleft;
	}

	/**
	 * right.
	 * 
	 * Set/get pointer to the right child.
	 * @param $link (optional) pointer to the right child, null to query only.
	 * @return unknown_type
	 */
	public function right($link=false)
	{
		if ($link !== false)
		{
			$this->nright = $link;
		}
		return $this->nright;
	}

	/**
	 * flag.
	 * 
	 * Set/get the color flag.
	 * @param $flag = RED or BLACK
	 * @return $flag = flag setting
	 */
	public function flag($flag=null)
	{
		if ($flag !== null)
		{
			$this->nflag = $flag;
		}
		return $this->nflag;
	}

}
Return current item: LLRB Tree