topical media & game development 
  
 
 
 
 
  
    
    
  
lib-as-de-polygonal-ds-BinaryTreeNode.ax
lib-as-de-polygonal-ds-BinaryTreeNode.ax
(swf
) 
[ flash
]
flex
  
 Copyright (c) Michael Baczynski 2007
 http://lab.polygonal.de/ds/
 This software is distributed under licence. Use of this software
 implies agreement with all terms and conditions of the accompanying
 software licence.
  
  package de.polygonal.ds
  {
          
 A binary tree node from which you can build a binary tree.
 
 <p>A Binary Tree is a simplified tree structure in which every node is
 only allowed to have up to two children nodes, which are called
 the left and right child.</p>
  
          public class @ax-lib-as-de-polygonal-ds-BinaryTreeNode
          {
                  
 Performs a <i>preorder traversal</i> on a tree.
 This processes the current tree node before its children.
 
	 parameter:  node    The node to start from.
	 parameter:  process A process function applied to each traversed node.
  
                  public static function preorder(node:@ax-lib-as-de-polygonal-ds-BinaryTreeNode, process:Function):void
                  {
                          if (node)
                          {
                                  process(node);
                                  
                                  if (node.left)
                                          @ax-lib-as-de-polygonal-ds-BinaryTreeNode.preorder(node.left, process);
                                  
                                  if (node.right)
                                          @ax-lib-as-de-polygonal-ds-BinaryTreeNode.preorder(node.right, process);
                          }
                  }
                  
                  
 Performs an <i>inorder traversal</i> on a tree.
 This processes the current node in between the children nodes.
 
	 parameter:  node    The node to start from.
	 parameter:  process A process function applied to each traversed node.
  
                  public static function inorder(node:@ax-lib-as-de-polygonal-ds-BinaryTreeNode, process:Function):void
                  {
                          if (node)
                          {
                                  if (node.left)
                                          @ax-lib-as-de-polygonal-ds-BinaryTreeNode.inorder(node.left, process);
                                  
                                  process(node);
                                  
                                  if (node.right)
                                          @ax-lib-as-de-polygonal-ds-BinaryTreeNode.inorder(node.right, process);
                          }
                  }
                  
                  
 Performs a <i>postorder traversal</i> on a tree.
 This processes the current node after its children nodes.
 
	 parameter:  node    The node to start from.
	 parameter:  process A process function applied to each traversed node.
  
                  public static function postorder(node:@ax-lib-as-de-polygonal-ds-BinaryTreeNode, process:Function):void
                  {
                          if (node)
                          {
                                  if (node.left)
                                          @ax-lib-as-de-polygonal-ds-BinaryTreeNode.postorder(node.left, process);
                                  
                                  if (node.right)
                                          @ax-lib-as-de-polygonal-ds-BinaryTreeNode.postorder(node.right, process);
                                  
                                  process(node);
                          }
                  }
                  
                  
 The left child node being referenced.
  
                  public var left:@ax-lib-as-de-polygonal-ds-BinaryTreeNode;
                  
                  
 The right child node being referenced.
  
                  public var right:@ax-lib-as-de-polygonal-ds-BinaryTreeNode;
                  
                  
 The parent node being referenced.
  
                  public var parent:@ax-lib-as-de-polygonal-ds-BinaryTreeNode;
                  
                  
 The node's data.
  
                  public var data:*;
                  
                  
 Creates an empty node.
 
	 parameter:  obj The data to store inside the node.
  
                  public function @ax-lib-as-de-polygonal-ds-BinaryTreeNode(obj:*)
                  {
                          this.data = data;
                          parent = left = right = null;
                  }
                  
                  
 Writes data into the left child.
 
	 parameter:  obj The data.
  
                  public function setLeft(obj:*):void
                  {
                          if (!left)
                          {
                                  left = new @ax-lib-as-de-polygonal-ds-BinaryTreeNode(obj);
                                  left.parent = this;
                          }
                          else
                                  left.data = data;
                  }
                  
                  
 Writes data into the right child.
 
	 parameter:  obj The data.
  
                  public function setRight(obj:*):void
                  {
                          if (!right)
                          {
                                  right = new @ax-lib-as-de-polygonal-ds-BinaryTreeNode(obj);
                                  right.parent = this;
                          }
                          else
                                  right.data = data;
                  }
                  
                  
 Checks if this node is left of its parent.
 
	 returns:  True if this node is left, otherwise false.
  
                  public function isLeft():Boolean
                  {
                          return this == parent.left;
                  }
                  
                  
                  
 Check if this node is a right of its parent.
 
	 returns:  True if this node is right, otherwise false.
  
                  public function isRight():Boolean
                  {
                          return this == parent.right;
                  }
                  
                  
 Recursively calculates the depth of a tree.
 
	 returns:  The depth of the tree.
  
                  public function getDepth(node:@ax-lib-as-de-polygonal-ds-BinaryTreeNode = null):int
                  {
                          var left:int = -1, right:int = -1;
                          
                          if (node == null) node = this;
                          
                          if (node.left)
                                  left = getDepth(node.left);
                          
                          if (node.right)
                                  right = getDepth(node.right);
                          
                          return (Math.max(left, right) + 1);
                  }
                  
                  
 Recursively counts the total number
 of nodes including this node.
  
                  public function count():int
                  {
                          var c:int = 1;
                          
                          if (left)
                                  c += left.count();
                          
                          if (right)
                                  c += right.count();
                          
                          return c;
                  }
                  
                  
 Recursively clears the tree by deleting
 all child nodes underneath the node
 the method is called on.
  
                  public function destroy():void
                  {
                          if (left)
                                  left.destroy();
                          
                          left = null;
                          
                          if (right)
                                  right.destroy();
                          
                          right = null;
                  }
                  
                  
 Returns a string representing the current object.
  
                  public function toString():String
                  {
                          return "[@ax-lib-as-de-polygonal-ds-BinaryTreeNode, data= " + data + "]";
                  }
          }
  }
  
  
(C) Æliens 
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. 
In case of other copyright issues, contact the author.