Return to Snippet

Revision: 21617
at December 16, 2009 14:22 by rvachere


Updated Code
package
{
	import flash.display.Sprite;
	
	/**********************************************************************************************\
	* BinaryTreeNode Class - Designed to give generic object functionality to a binary tree        *
	* node type.                                                                                   *
	* @author : Richard Vacheresse /|\ http://www.rvacheresse.com /|\                              *
	* Licensed for free Commercial and Private use creative commons license agreement.             *
	* The provided code is in an "as-is" state. Richard Vacheresse makes no warranties             *
	* regarding the provided code, and disclaims liability for damages resulting from its use.     *
	* I am also not responsible for baby Smurfs being born only on a blue moon; the way your       *
	* father looks really creeepy like at one of your friends; or for your better half being home  *
	* late last Tuesday night, but man-oh-man she can sure dance. I am most importantly not        *
	* responsible for you reading this far in docs, really?                                        *
	* @version 1.0                                                                                 *
	\**********************************************************************************************/
	public class BinaryTreeNode extends Sprite
	{
		private var element:Sprite;
		private var left:BinaryTreeNode;
		private var right:BinaryTreeNode;
		
		/**
		* BinaryTreeNode(obj:Object):void Constructor. Creates a new
		* tree node with the specified data element being passed to it.
		* @param - Sprite Object
		**/
		public function BinaryTreeNode(obj:Sprite):void
		{
			element = obj;
			left = null;
			right = null;
			trace("\nBinary Tree Node Created");
		}
		
	    /**
        * numChildren():int - Returns the number of non-null children of this node.
        * This method may be able to be written more efficiently.
        * @return  the integer number of non-null children of this node 
        **/
		public function getNumChildren():int
		{
			var children:int = 0;
			
			if(left != null)
				children = (1 + left.getNumChildren());
				
			else if(right != null)
				children += (1 + right.getNumChildren());
			
			return children;
		}
		
		/**
		* toString():String function - Returns a string representation of the
		* node and its children.
		* @return - String Object
		**/
		override public function toString():String
		{
			var result:String = "";
			
			result = ("\nBinary Tree Node: " + 
					  "\n-----------------"+ 
					  "\nThe Main Element is: " +
					  this.element.toString());
			
			if(this.getNumChildren() > 0)
			{
				try
				{
					if(this.left != null)
						result += ("\nThe Left is: " + this.left.toString());
					if(this.right != null)
						result += ("\nThe Right is: " + this.right.toString());
				}
				catch(er:Error)
				{
					trace("Error Caught: " + er);
				}
			}
			
			return result;
		}
		
		//$-------------------------------------------
	    //$              $-SETTERS-$
	    //$-------------------------------------------
				
		/**
		* setElement(spr:Sprite):void function - Sets the element property to passed value.
			**/
		public function setElement(spr:Sprite):void
		{
			this.element = spr;			
			trace("\nMain Element Set to: " + spr);
		}
		
		/**
		* setLeftNode(btNode:BinaryTreeNode):void function - Sets the left binary tree node
		* property to passed value.
		**/
		public function setLeftNode(btNode:BinaryTreeNode):void
		{
			this.left = btNode;
			trace("\nLeft Node Set To: " + btNode);
		}		
		
		/**
		* setRightNode(btNode:BinaryTreeNode):void function - Sets the right binary tree node
		* property to passed value.
		**/
		public function setRightNode(btNode:BinaryTreeNode):void
		{
			this.right = btNode;
			trace("\nRight Node Set To: " + btNode);
		}
		
		//$-------------------------------------------
	    //$              $-GETTERS-$
	    //$-------------------------------------------
		
		/**
		* getElement():Sprite function - Returns the element property value.
		* @return - Sprite Object
	    **/
		public function getElement():Sprite
		{
			return this.element;			
			trace("\nMain Element Returned: " + spr);
		}
		
		/**
		* getLeftNode():BinaryTreeNode function - Returns the left binary tree node
		* property.
		* @return - BinaryTreeNode Object
		**/
		public function getLeftNode():BinaryTreeNode
		{
			reuturn this.left;
			trace("\nLeft Node Returned: " + btNode);
		}		
		
		/**
		* getRightNode():BinaryTreeNode function - Returns the right binary tree node
		* property.
		* @return - BinaryTreeNode Object
		**/
		public function getRightNode():BinaryTreeNode
		{
			return this.right;
			trace("\nRight Node Returned: " + btNode);
		}
	}
}

Revision: 21616
at December 16, 2009 12:38 by rvachere


Updated Code
package
{
	import flash.display.Sprite;
	
	/**********************************************************************************************\
	* BinaryTreeNode Class - Designed to give generic object functionality to a binary tree        *
	* node type.                                                                                   *
	* @author : Richard Vacheresse /|\ http://www.rvacheresse.com /|\                              *
	* Licensed for free Commercial and Private use creative commons license agreement.             *
	* The provided code is in an "as-is" state. Richard Vacheresse makes no warranties             *
	* regarding the provided code, and disclaims liability for damages resulting from its use.     *
	* I am also not responsible for baby Smurfs being born only on a blue moon; the way your       *
	* father looks really creeepy like at one of your friends; or for your better half being home  *
	* late last Tuesday night, but man-oh-man she can sure dance. I am most importantly not        *
	* responsible for you reading this far in docs, really?                                        *
	* @version 1.0                                                                                 *
	\**********************************************************************************************/
	public class BinaryTreeNode extends Sprite
	{
		private var element:Sprite;
		private var left:BinaryTreeNode;
		private var right:BinaryTreeNode;
		
		/**
		* BinaryTreeNode(obj:Object):void Constructor. Creates a new
		* tree node with the specified data element being passed to it.
		* @param - Sprite Object
		**/
		public function BinaryTreeNode(obj:Sprite):void
		{
			element = obj;
			left = null;
			right = null;
			trace("\nBinary Tree Node Created");
		}
		
	    /**
        * numChildren():int - Returns the number of non-null children of this node.
        * This method may be able to be written more efficiently.
        * @return  the integer number of non-null children of this node 
        **/
		public function getNumChildren():int
		{
			var children:int = 0;
			
			if(left != null)
				children = (1 + left.getNumChildren());
				
			else if(right != null)
				children += (1 + right.getNumChildren());
			
			return children;
		}
		
		/**
		* toString():String function - Returns a string representation of the
		* node and its children.
		* @return - String Object
		**/
		override public function toString():String
		{
			var result:String = "";
			
			result = ("\nBinary Tree Node: " + 
					  "\n-----------------"+ 
					  "\nThe Main Element is: " +
					  this.element.toString());
			
			if(this.getNumChildren() > 0)
			{
				try
				{
					if(this.left != null)
						result += ("\nThe Left is: " + this.left.toString());
					if(this.right != null)
						result += ("\nThe Right is: " + this.right.toString());
				}
				catch(er:Error)
				{
					trace("Error Caught: " + er);
				}
			}
			
			return result;
		}
		
		/**
		* setElement(spr:Sprite):void function - Sets the element property to passed value.
		**/
		public function setElement(spr:Sprite):void
		{
			this.element = spr;			
			trace("\nMain Element Set to: " + spr);
		}
		
		/**
		* setLeftNode(btNode:BinaryTreeNode):void function - Sets the left binary tree node
		* property to passed value.
		**/
		public function setLeftNode(btNode:BinaryTreeNode):void
		{
			this.left = btNode;
			trace("\nLeft Node Set To: " + btNode);
		}		
		
		/**
		* setRightNode(btNode:BinaryTreeNode):void function - Sets the right binary tree node
		* property to passed value.
		**/
		public function setRightNode(btNode:BinaryTreeNode):void
		{
			this.right = btNode;
			trace("\nRight Node Set To: " + btNode);
		}
	}
}

Revision: 21615
at December 16, 2009 12:36 by rvachere


Initial Code
package
{
	import flash.display.Sprite;
	
	/**********************************************************************************************\
	* BinaryTreeNode Class - Designed to give generic object functionality to a binary tree        *
	* node type.                                                                                   *
	* @author : Richard Vacheresse /|\ http://www.rvacheresse.com /|\                              *
	* Licensed for free Commercial and Private use creative commons license agreement.             *
	* The provided code is in an "as-is" state. Richard Vacheresse makes no warranties             *
	* regarding the provided code, and disclaims liability for damages resulting from its use.     *
	* I am also not responsible for baby Smurfs being born only on a blue moon; the way your       *
	* father looks really creeepy like at one of your friends; or for your better half being home  *
	* late last Tuesday night, but man-oh-man she can sure dance. I am most importantly not        *
	* responsible for you reading this far in docs, really?   
	* @version 1.0                                                                                 *
	\**********************************************************************************************/
	public class BinaryTreeNode extends Sprite
	{
		private var element:Sprite;
		private var left:BinaryTreeNode;
		private var right:BinaryTreeNode;
		
		/**
		* BinaryTreeNode(obj:Object):void Constructor. Creates a new
		* tree node with the specified data element being passed to it.
		* @param - Sprite Object
		**/
		public function BinaryTreeNode(obj:Sprite):void
		{
			element = obj;
			left = null;
			right = null;
			trace("\nBinary Tree Node Created");
		}
		
	    /**
        * numChildren():int - Returns the number of non-null children of this node.
        * This method may be able to be written more efficiently.
        * @return  the integer number of non-null children of this node 
        **/
		public function getNumChildren():int
		{
			var children:int = 0;
			
			if(left != null)
				children = (1 + left.getNumChildren());
				
			else if(right != null)
				children += (1 + right.getNumChildren());
			
			return children;
		}
		
		/**
		* toString():String function - Returns a string representation of the
		* node and its children.
		* @return - String Object
		**/
		override public function toString():String
		{
			var result:String = "";
			
			result = ("\nBinary Tree Node: " + 
					  "\n-----------------"+ 
					  "\nThe Main Element is: " +
					  this.element.toString());
			
			if(this.getNumChildren() > 0)
			{
				try
				{
					if(this.left != null)
						result += ("\nThe Left is: " + this.left.toString());
					if(this.right != null)
						result += ("\nThe Right is: " + this.right.toString());
				}
				catch(er:Error)
				{
					trace("Error Caught: " + er);
				}
			}
			
			return result;
		}
		
		/**
		* setElement(spr:Sprite):void function - Sets the element property to passed value.
		**/
		public function setElement(spr:Sprite):void
		{
			this.element = spr;			
			trace("\nMain Element Set to: " + spr);
		}
		
		/**
		* setLeftNode(btNode:BinaryTreeNode):void function - Sets the left binary tree node
		* property to passed value.
		**/
		public function setLeftNode(btNode:BinaryTreeNode):void
		{
			this.left = btNode;
			trace("\nLeft Node Set To: " + btNode);
		}		
		
		/**
		* setRightNode(btNode:BinaryTreeNode):void function - Sets the right binary tree node
		* property to passed value.
		**/
		public function setRightNode(btNode:BinaryTreeNode):void
		{
			this.right = btNode;
			trace("\nRight Node Set To: " + btNode);
		}
	}
}

Initial URL


Initial Description


Initial Title
AS3 BianryTreeNode Class - For Use With Tree Data Collections

Initial Tags
data

Initial Language
ActionScript 3