AS3 BianryTreeNode类-用于树数据集合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3 BianryTreeNode类-用于树数据集合相关的知识,希望对你有一定的参考价值。

  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(" Binary 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 = (" Binary Tree Node: " +
  65. " -----------------"+
  66. " The Main Element is: " +
  67. this.element.toString());
  68.  
  69. if(this.getNumChildren() > 0)
  70. {
  71. try
  72. {
  73. if(this.left != null)
  74. result += (" The Left is: " + this.left.toString());
  75. if(this.right != null)
  76. result += (" The 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(" Main 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(" Left 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(" Right 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(" Main 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(" Left 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(" Right Node Returned: " + btNode);
  154. }
  155. }
  156. }

以上是关于AS3 BianryTreeNode类-用于树数据集合的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 用于格式化日期的AS3类,如PHP函数日期

用于格式化日期(如PHP函数日期)的AS3类

用于导入的 AS3 SoundChannel/Sound HZ 限制

剑指Offer - 面试题27:二叉树的镜像

ActionScript 3 AS3:创建用于存储JSON数据的对象数组

AS3:创建用于存储JSON数据的对象数组