AS3 BianryTreeNode Class - For Use With Tree Data Collections


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.Sprite;
  4.  
  5. /**********************************************************************************************\
  6. * BinaryTreeNode Class - Designed to give generic object functionality to a binary tree *
  7. * node type. *
  8. * @author : Richard Vacheresse /|\ http://www.rvacheresse.com /|\ *
  9. * Licensed for free Commercial and Private use creative commons license agreement. *
  10. * The provided code is in an "as-is" state. Richard Vacheresse makes no warranties *
  11. * regarding the provided code, and disclaims liability for damages resulting from its use. *
  12. * I am also not responsible for baby Smurfs being born only on a blue moon; the way your *
  13. * father looks really creeepy like at one of your friends; or for your better half being home *
  14. * late last Tuesday night, but man-oh-man she can sure dance. I am most importantly not *
  15. * responsible for you reading this far in docs, really? *
  16. * @version 1.0 *
  17. \**********************************************************************************************/
  18. public class BinaryTreeNode extends Sprite
  19. {
  20. private var element:Sprite;
  21. private var left:BinaryTreeNode;
  22. private var right:BinaryTreeNode;
  23.  
  24. /**
  25. * BinaryTreeNode(obj:Object):void Constructor. Creates a new
  26. * tree node with the specified data element being passed to it.
  27. * @param - Sprite Object
  28. **/
  29. public function BinaryTreeNode(obj:Sprite):void
  30. {
  31. element = obj;
  32. left = null;
  33. right = null;
  34. trace("\nBinary Tree Node Created");
  35. }
  36.  
  37. /**
  38.   * numChildren():int - Returns the number of non-null children of this node.
  39.   * This method may be able to be written more efficiently.
  40.   * @return the integer number of non-null children of this node
  41.   **/
  42. public function getNumChildren():int
  43. {
  44. var children:int = 0;
  45.  
  46. if(left != null)
  47. children = (1 + left.getNumChildren());
  48.  
  49. else if(right != null)
  50. children += (1 + right.getNumChildren());
  51.  
  52. return children;
  53. }
  54.  
  55. /**
  56. * toString():String function - Returns a string representation of the
  57. * node and its children.
  58. * @return - String Object
  59. **/
  60. override public function toString():String
  61. {
  62. var result:String = "";
  63.  
  64. result = ("\nBinary Tree Node: " +
  65. "\n-----------------"+
  66. "\nThe Main Element is: " +
  67. this.element.toString());
  68.  
  69. if(this.getNumChildren() > 0)
  70. {
  71. try
  72. {
  73. if(this.left != null)
  74. result += ("\nThe Left is: " + this.left.toString());
  75. if(this.right != null)
  76. result += ("\nThe Right is: " + this.right.toString());
  77. }
  78. catch(er:Error)
  79. {
  80. trace("Error Caught: " + er);
  81. }
  82. }
  83.  
  84. return result;
  85. }
  86.  
  87. //$-------------------------------------------
  88. //$ $-SETTERS-$
  89. //$-------------------------------------------
  90.  
  91. /**
  92. * setElement(spr:Sprite):void function - Sets the element property to passed value.
  93. **/
  94. public function setElement(spr:Sprite):void
  95. {
  96. this.element = spr;
  97. trace("\nMain Element Set to: " + spr);
  98. }
  99.  
  100. /**
  101. * setLeftNode(btNode:BinaryTreeNode):void function - Sets the left binary tree node
  102. * property to passed value.
  103. **/
  104. public function setLeftNode(btNode:BinaryTreeNode):void
  105. {
  106. this.left = btNode;
  107. trace("\nLeft Node Set To: " + btNode);
  108. }
  109.  
  110. /**
  111. * setRightNode(btNode:BinaryTreeNode):void function - Sets the right binary tree node
  112. * property to passed value.
  113. **/
  114. public function setRightNode(btNode:BinaryTreeNode):void
  115. {
  116. this.right = btNode;
  117. trace("\nRight Node Set To: " + btNode);
  118. }
  119.  
  120. //$-------------------------------------------
  121. //$ $-GETTERS-$
  122. //$-------------------------------------------
  123.  
  124. /**
  125. * getElement():Sprite function - Returns the element property value.
  126. * @return - Sprite Object
  127. **/
  128. public function getElement():Sprite
  129. {
  130. return this.element;
  131. trace("\nMain Element Returned: " + spr);
  132. }
  133.  
  134. /**
  135. * getLeftNode():BinaryTreeNode function - Returns the left binary tree node
  136. * property.
  137. * @return - BinaryTreeNode Object
  138. **/
  139. public function getLeftNode():BinaryTreeNode
  140. {
  141. reuturn this.left;
  142. trace("\nLeft Node Returned: " + btNode);
  143. }
  144.  
  145. /**
  146. * getRightNode():BinaryTreeNode function - Returns the right binary tree node
  147. * property.
  148. * @return - BinaryTreeNode Object
  149. **/
  150. public function getRightNode():BinaryTreeNode
  151. {
  152. return this.right;
  153. trace("\nRight Node Returned: " + btNode);
  154. }
  155. }
  156. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.