<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog . sebastian martens &#187; ActionScript</title>
	<atom:link href="http://blog.sebastian-martens.de/tag/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sebastian-martens.de</link>
	<description></description>
	<lastBuildDate>Fri, 03 Feb 2012 15:55:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>ActionScript Data Structures</title>
		<link>http://blog.sebastian-martens.de/2009/05/actionscript-data-structures/</link>
		<comments>http://blog.sebastian-martens.de/2009/05/actionscript-data-structures/#comments</comments>
		<pubDate>Mon, 18 May 2009 21:12:19 +0000</pubDate>
		<dc:creator>Sebastian.Martens</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Binary Search Tree]]></category>
		<category><![CDATA[Datastructure]]></category>
		<category><![CDATA[Double Linked List]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[FlexUnit]]></category>
		<category><![CDATA[Garbadge Collector]]></category>
		<category><![CDATA[joa ebert]]></category>
		<category><![CDATA[Linked List]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pooling]]></category>

		<guid isPermaLink="false">http://blog.sebastian-martens.de/?p=143</guid>
		<description><![CDATA[In the good old Java world ( not that i&#8217;m that familiar with it ) real data structures are a well non secret. Not only in Java but in all older object oriented progaming languages exists different of Design Patterns for effecient data structures and fast access to data structures for different kind of use [...]]]></description>
			<content:encoded><![CDATA[<p>In the good old Java world ( not that i&#8217;m that familiar with it ) <em>real</em> data structures are a well non secret. Not only in Java but in all older object oriented progaming languages exists different of Design Patterns for effecient data structures and fast access to data structures for different kind of use cases.</p>
<p>With data structures i don&#8217;t mean an normal or byte array which might be <strong>the</strong> data structures an the flash  world, because you use it without waisting any thought of it ( me included ). in the very most situations were you used the Flash plattform right now this might be ok, because often you don&#8217;t have the mass of data. So the performance of the Flash Player is well enough.</p>
<p>But the possiblities of the Flash Plattform are growing ( very fast in the last time :) ) and more and more applications ( also real applications ) are written for the web ( or for both worlds if you use AIR ). <span id="more-143"></span></p>
<p>So you might now run in the situation to build an realy big application which has to handle a lot of data and you have to think about performance. One big thing in performance is the datastructure itself. Each application is different. Is it important for your app to write data fast or read it fast; which happens mor often; do you process all data always complete or do you just need a small part of it ?! For each use case there is for sure a very well data structure.</p>
<h3>Data Structure</h3>
<p>Example A: <a  href="http://blog.joa-ebert.com/" target="_blank">Joa Ebert</a> had a talk on Flash Forum Conference 09 (which also inspired me for the second part &#8211; see below &#8211; and made me happy to see that datstructures are already present in the flash world ) where he presented his data structure for the <a  href="http://www.hobnox.com/audiotool" target="_blank">audio tool</a>. When they output the audio stream they always have to process a (large) list of single audio-items ( one for each bit of the stereo 44.100kHz Audiostream ). -&gt; Fast access of elements (always complete and in the same order ) one by one. Best datastructure: Linked List.</p>
<p>What is a linked list ? A linked list is an list of very light weight elements where each element holds a reference to it next neightbour. In your app you only have one reference to the first node of this list and that loop over the list by setting the reference further (example for calculating the length of the list):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Returns the cardinality of the map - the number of elements
 * @return A value of &lt;code&gt;Int&lt;/code&gt;, the number of elemets
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> card<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> ref:SimplyLinkedListElement = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>;
	<span style="color: #000000; font-weight: bold;">var</span> card:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
	<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span> ref <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		ref = ref.<span style="color: #006600;">next</span>;
		++card;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> card;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>You also could insert a new element very fast ( if it is ok to put it on the first position ). You just have to change the reference to the root element to the new element and link the next reference of the new element to the old root element.</p>
<p>Example B: You have an application which often reads from its datastructure, but always only searches on random element. If you would use a linked list, in the worst case you have to look at each element if it is the searched one. Looking at each element is to expensive. One solution are Tree-Datastructures. The simplest is the Binary Tree. Again you have an root reference. But each element now have to references to the next elements. One to the right ( bigger element ) and one to the left ( smaller element ). If you insert an element in the tree you&#8217;ll look at the existing node. Is your new node larger, you will look at the right reference, is it smaller at the left reference of the node. Is the reference empty you could place it there.</p>
<p>If not you will look at the data again. In the worst case you now don&#8217;t need to look at each element because you have a structure and will find the fastest way by compare the keys. The search is logarithmic not linear.</p>
<h3>Garbadge Collector / Pooling</h3>
<p>The next big thing with datastructure and performance in the Flash Player is the Garbadge Collector ( which it don&#8217;t were that aware before Joas talk ). So the Garbadge Collector is realy slow. So we have to avoid that he is coming to action. How ? By reusing your elements or <em>pool</em> them.</p>
<p>The Garbadge Collector will free space in the memory if there are no more references to an allocted section. So you have to control the references. In your mass of objects you should not drop unused items, because the are not used for now, but pool them. You remove the reference from the main datastructure to the unused element and reference it within a list of unsed elements. When you now need a new element you don&#8217;t have to create a new one ( which is also realy expensive ) but you could take one element from you pool.</p>
<p>Find attached some of my older datastructures (LinkedList, DoubleLinkedList, BinarySearchTree) which i just updated so they will now use pooling for unused elements ( and of course there are also some simple FlexUnit Testcases included ! :) &#8211; <a  href="http://florian.salihovic.info/blog/?p=55">Florian Salihovic</a>).</p>
<p><a  href="http://blog.sebastian-martens.de/wp-content/uploads/2009/05/as3datastructures.zip">Download AS3DataStructure.zip</a></p>
<h3>Binary Search Tree</h3>
<p>Here is one longer example of an Binary Search Tree implementation. It&#8217;s not fully done yet, but most common methods are implemented and ready to use. I&#8217;m sure the is some optimaziation potential (please let me know if you find anything, then i will correct it).<br />
Because there are no real Generics in ActionScript this is an Example for a Map which uses an String value as Key and an Number value as data. This has of course to be adopted for your case:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">nonstatics</span>.<span style="color: #006600;">as3collection</span>.<span style="color: #006600;">map</span>.<span style="color: #006600;">MapAsBinarySearchTree</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Implements a map as binary search tree
	 *
	 * @author Sebastian Martens http://www.sebastian-martens.de || http://blog.sebastian-martens.de
	 * @copyright Creative Commons, free to use &quot;as is&quot;
	 * @date $Id: MapAsBinarySearchTree.as 21 2009-05-18 20:06:59Z dinnerout $
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MapAsBinarySearchTree
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/**
		 * root reference
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:MapAsBinarySearchTreeElement;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * root reference of pooled elements
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _poolRoot:MapAsBinarySearchTreeElement;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * cardinality of map
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _cardV:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * is vadinality valid
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _validCard:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * construction method
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MapAsBinarySearchTree<span style="color: #66cc66;">&#40;</span> n:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// build pool elements to avoid construction on runtime</span>
			<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span> n<span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPush</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MapAsBinarySearchTreeElement<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				--n;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #0066CC;">this</span>._cardV = <span style="color: #cc66cc;">0</span>;
			<span style="color: #0066CC;">this</span>._validCard = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Test if Map is an empty map
		 * @return A value of code&gt;Boolean&lt;/code&gt;, if map is empty
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isEmpty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> == <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Returns the cardinality of the map - the number of elements
		 * @return A value of &lt;code&gt;Int&lt;/code&gt;, the number of elemets
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> card<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>._validCard <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._cardV;
			<span style="color: #0066CC;">this</span>._cardV = <span style="color: #0066CC;">this</span>._card<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>._validCard = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._cardV;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * recursiv card() helper method
		 * @private
		 * @param node value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/int&gt; processed node
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _card<span style="color: #66cc66;">&#40;</span> node:MapAsBinarySearchTreeElement <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span> + <span style="color: #0066CC;">this</span>._card<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span> + <span style="color: #0066CC;">this</span>._card<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * returns an element by its key
		 * @param key value of &lt;code&gt;String&lt;/code&gt;, search key
		 * @return value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt;
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getElement<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:MapAsBinarySearchTreeElement<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._getElement<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>, <span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * recursive helper method for getElement
		 * @private
		 * @param node value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt;, processing node
		 * @param key value of &lt;code&gt;String&lt;/code&gt;, search key
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _getElement<span style="color: #66cc66;">&#40;</span> node:MapAsBinarySearchTreeElement, <span style="color: #0066CC;">key</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:MapAsBinarySearchTreeElement<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> l:MapAsBinarySearchTreeElement;
			<span style="color: #000000; font-weight: bold;">var</span> r:MapAsBinarySearchTreeElement;
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #0066CC;">key</span> == <span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> node;
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&gt;</span> node.<span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&#41;</span>?<span style="color: #0066CC;">this</span>._getElement<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">r</span>, <span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">this</span>._getElement<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">l</span>, <span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * checkes if an value v is in the map
		 * @param v a value of &lt;code&gt;Number&lt;/code&gt;,element value to search for
		 * @return true if value is containing in one element
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> containsValue<span style="color: #66cc66;">&#40;</span> v:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._containsValue<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>, v <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * recursive helper method for containsValue
		 * @private
		 * @param node value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/int&gt; processed node
		 * @param v value of &lt;code&gt;Number&lt;/code&gt;, search value
		 * @return value of &lt;code&gt;Boolean&lt;/code&gt;, value found in tree
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _containsValue<span style="color: #66cc66;">&#40;</span> node:MapAsBinarySearchTreeElement, v:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">value</span> == v <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> v <span style="color: #66cc66;">&gt;</span> node.<span style="color: #006600;">value</span> <span style="color: #66cc66;">&#41;</span>?<span style="color: #0066CC;">this</span>._containsValue<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">r</span>, v <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">this</span>._containsValue<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">l</span>, v <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Test if this map equals to another map ( equals means same elements not same structure )
		 * @param map value of &lt;code&gt;MapAsBinarySearchTree&lt;/code&gt; to test this map with
		 * @return a value of &lt;code&gt;Boolean&lt;/code&gt;
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isEqualTo<span style="color: #66cc66;">&#40;</span> map:MapAsBinarySearchTree <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span> == map <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> map.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>map.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> map.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">card</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">!</span>= map.<span style="color: #006600;">card</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._isEqualTo<span style="color: #66cc66;">&#40;</span> map, <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * recursive helper method for isEqualTo
		 * @param map value of &lt;code&gt;MapAsBinarySearchTree&lt;/code&gt;, map to compare with
		 * @param node value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/node&gt;, processing node of this map
		 * @return false if element not in reference tree
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _isEqualTo<span style="color: #66cc66;">&#40;</span> map:MapAsBinarySearchTree, node:MapAsBinarySearchTreeElement <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #000000; font-weight: bold;">var</span> item:MapAsBinarySearchTreeElement = map.<span style="color: #006600;">getElement</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>item <span style="color: #66cc66;">||</span> item.<span style="color: #006600;">value</span><span style="color: #66cc66;">!</span>=node.<span style="color: #006600;">value</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._isEqualTo<span style="color: #66cc66;">&#40;</span> map, node.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>._isEqualTo<span style="color: #66cc66;">&#40;</span> map, node.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * inserts a new value into the map
		 * @param k value of &lt;code&gt;String&lt;/code&gt;, key
		 * @param v value of &lt;code&gt;Number&lt;/code&gt;, value
		 * @return this
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">insert</span><span style="color: #66cc66;">&#40;</span> k:<span style="color: #0066CC;">String</span>, v:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span>:MapAsBinarySearchTree<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// invalidate cardinality</span>
			<span style="color: #0066CC;">this</span>._validCard = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// empty map</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> item:MapAsBinarySearchTreeElement = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				item.<span style="color: #006600;">value</span> = v;
				item.<span style="color: #0066CC;">key</span> = k;
				item.<span style="color: #006600;">l</span> = <span style="color: #000000; font-weight: bold;">null</span>;
				item.<span style="color: #006600;">r</span> = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
				<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> = item;
				<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// non empty map</span>
			<span style="color: #0066CC;">this</span>._insert<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>, k, v <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * recursive helper method for insert method
		 * @param node value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt;, processed node
		 * @param k value of &lt;code&gt;String&lt;/code&gt;, key
		 * @param v value of &lt;code&gt;Number&lt;/code&gt;, value
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _insert<span style="color: #66cc66;">&#40;</span> node:MapAsBinarySearchTreeElement, k:<span style="color: #0066CC;">String</span>, v:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// existing keys will be updated</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #0066CC;">key</span> == k <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				node.<span style="color: #006600;">value</span> = v;
				<span style="color: #b1b100;">return</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">var</span> item:MapAsBinarySearchTreeElement;
&nbsp;
			<span style="color: #808080; font-style: italic;">// insert left or right</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> k <span style="color: #66cc66;">&gt;</span> node.<span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// right node exist</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					<span style="color: #0066CC;">this</span>._insert<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">r</span>, k, v <span style="color: #66cc66;">&#41;</span>;
&nbsp;
				<span style="color: #808080; font-style: italic;">// new node at right leave</span>
				<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
					item = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					item.<span style="color: #0066CC;">key</span> = k;
					item.<span style="color: #006600;">value</span> = v;
					item.<span style="color: #006600;">l</span> = <span style="color: #000000; font-weight: bold;">null</span>;
					item.<span style="color: #006600;">r</span> = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
					node.<span style="color: #006600;">r</span> = item;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// left node exists</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					<span style="color: #0066CC;">this</span>._insert<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">l</span>, k, v <span style="color: #66cc66;">&#41;</span>;
&nbsp;
				<span style="color: #808080; font-style: italic;">// new node at left leave</span>
				<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
					item = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					item.<span style="color: #0066CC;">key</span> = k;
					item.<span style="color: #006600;">value</span> = v;
					item.<span style="color: #006600;">l</span> = <span style="color: #000000; font-weight: bold;">null</span>;
					item.<span style="color: #006600;">r</span> = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
					node.<span style="color: #006600;">l</span> = item;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * returns if the map contains an element with k
		 * @param k value of &lt;code&gt;String&lt;/code&gt;, key to search for
		 * @return true if mao contains key
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> contains<span style="color: #66cc66;">&#40;</span> k:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._contains<span style="color: #66cc66;">&#40;</span> k, <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * recursive helper method for contains
		 * @param k value of &lt;code&gt;String&lt;/code&gt;, key to search for
		 * @param node value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt;, start node
		 * @return
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _contains<span style="color: #66cc66;">&#40;</span> k:<span style="color: #0066CC;">String</span>, node:MapAsBinarySearchTreeElement <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #0066CC;">key</span>==k <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> k <span style="color: #66cc66;">&gt;</span> node.<span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._contains<span style="color: #66cc66;">&#40;</span> k, node.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> k <span style="color: #66cc66;">&lt;</span> node.<span style="color: #0066CC;">key</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._contains<span style="color: #66cc66;">&#40;</span> k, node.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Traverse the tree complete</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _containsF<span style="color: #66cc66;">&#40;</span> k:<span style="color: #0066CC;">String</span>, node:MapAsBinarySearchTreeElement <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>node.<span style="color: #0066CC;">key</span>==k<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #0066CC;">this</span>._containsF<span style="color: #66cc66;">&#40;</span>k,node.<span style="color: #006600;">l</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #0066CC;">this</span>._containsF<span style="color: #66cc66;">&#40;</span>k,node.<span style="color: #006600;">r</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * returns the rightes leave of an part tree
		 * @private
		 * @param node value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt;, start node
		 * @return latest right leave from node
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _getRight<span style="color: #66cc66;">&#40;</span> node:MapAsBinarySearchTreeElement <span style="color: #66cc66;">&#41;</span>:MapAsBinarySearchTreeElement<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// already latest right node</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> node;
&nbsp;
			<span style="color: #808080; font-style: italic;">// find right</span>
			<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				node = node.<span style="color: #006600;">r</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">return</span> node;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * returns the most left leave of an part tree
		 * @private
		 * @param node value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt;, start node
		 * @return latest left leave from node
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _getLeft<span style="color: #66cc66;">&#40;</span> node:MapAsBinarySearchTreeElement <span style="color: #66cc66;">&#41;</span>:MapAsBinarySearchTreeElement<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// already latest right node</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> node;
&nbsp;
			<span style="color: #808080; font-style: italic;">// find right</span>
			<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				node = node.<span style="color: #006600;">l</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">return</span> node;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * finds the parent node of the element with key k
		 * @private
		 * @param k value of &lt;code&gt;String&lt;/code&gt;
		 * @param nod value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt;, processed element
		 * @return parent node of node with key k, null if not found
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _findKeyParent<span style="color: #66cc66;">&#40;</span> k:<span style="color: #0066CC;">String</span>, node:MapAsBinarySearchTreeElement <span style="color: #66cc66;">&#41;</span>:MapAsBinarySearchTreeElement<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//  root node to remove</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> node.<span style="color: #0066CC;">key</span> == k <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> node;
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&amp;&amp;</span> node.<span style="color: #006600;">l</span>.<span style="color: #0066CC;">key</span> == k<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&amp;&amp;</span> node.<span style="color: #006600;">r</span>.<span style="color: #0066CC;">key</span> == k<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> node;
&nbsp;
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>k <span style="color: #66cc66;">&gt;</span> node.<span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#41;</span>?<span style="color: #0066CC;">this</span>._findKeyParent<span style="color: #66cc66;">&#40;</span> k, node.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">this</span>._findKeyParent<span style="color: #66cc66;">&#40;</span> k, node.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * removes an element by its key
		 * @param k value of &lt;code&gt;String&lt;/code&gt;, key of element to remove
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> remove<span style="color: #66cc66;">&#40;</span> k:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:MapAsBinarySearchTree<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// empty list</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// search for parent node of key element</span>
			<span style="color: #000000; font-weight: bold;">var</span> searchN:MapAsBinarySearchTreeElement = <span style="color: #0066CC;">this</span>._findKeyParent<span style="color: #66cc66;">&#40;</span> k, <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> ref:MapAsBinarySearchTreeElement;
			<span style="color: #000000; font-weight: bold;">var</span> ref2:MapAsBinarySearchTreeElement;
&nbsp;
			<span style="color: #808080; font-style: italic;">// parent of key found, remove element</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> searchN <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// invalidate cardinality</span>
				<span style="color: #0066CC;">this</span>._validCard = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
				<span style="color: #808080; font-style: italic;">// remove root node</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> searchN == <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">key</span> == k <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					ref = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>;
&nbsp;
					<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> = <span style="color: #000000; font-weight: bold;">null</span>; <span style="color: #808080; font-style: italic;">// only root element</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPush</span><span style="color: #66cc66;">&#40;</span> ref <span style="color: #66cc66;">&#41;</span>;
						<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
					<span style="color: #66cc66;">&#125;</span>
&nbsp;
					<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">r</span>; <span style="color: #808080; font-style: italic;">// empty left tree</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPush</span><span style="color: #66cc66;">&#40;</span> ref <span style="color: #66cc66;">&#41;</span>;
						<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
					<span style="color: #66cc66;">&#125;</span>
&nbsp;
					<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">l</span>; <span style="color: #808080; font-style: italic;">// empty right tree</span>
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPush</span><span style="color: #66cc66;">&#40;</span> ref <span style="color: #66cc66;">&#41;</span>;
						<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
					<span style="color: #66cc66;">&#125;</span>
&nbsp;
					<span style="color: #808080; font-style: italic;">// both trees are filled</span>
					ref2 = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">r</span>;
					<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> = <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">l</span>;
&nbsp;
					<span style="color: #0066CC;">this</span>._getRight<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> <span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">r</span> = ref2;
&nbsp;
					<span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPush</span><span style="color: #66cc66;">&#40;</span> ref <span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
&nbsp;
				<span style="color: #808080; font-style: italic;">// remove node from tree middle</span>
				<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
					<span style="color: #808080; font-style: italic;">// found in left tree</span>
					<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> searchN.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&amp;&amp;</span> searchN.<span style="color: #006600;">l</span>.<span style="color: #0066CC;">key</span> == k <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
						ref = searchN.<span style="color: #006600;">l</span>;
&nbsp;
						searchN.<span style="color: #006600;">l</span> = searchN.<span style="color: #006600;">l</span>.<span style="color: #006600;">l</span>;
						<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> searchN.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
							<span style="color: #0066CC;">this</span>._getRight<span style="color: #66cc66;">&#40;</span> searchN.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">r</span> = ref.<span style="color: #006600;">r</span>;
						<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span> searchN.<span style="color: #006600;">l</span> = ref.<span style="color: #006600;">r</span>;
&nbsp;
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPush</span><span style="color: #66cc66;">&#40;</span> ref <span style="color: #66cc66;">&#41;</span>;
						<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
&nbsp;
					<span style="color: #808080; font-style: italic;">// found in right tree</span>
					<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
						ref = searchN.<span style="color: #006600;">r</span>;
&nbsp;
						searchN.<span style="color: #006600;">r</span> = searchN.<span style="color: #006600;">r</span>.<span style="color: #006600;">l</span>;
						<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> searchN.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
							<span style="color: #0066CC;">this</span>._getRight<span style="color: #66cc66;">&#40;</span> searchN.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">r</span> = ref.<span style="color: #006600;">r</span>;
						<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span> searchN.<span style="color: #006600;">r</span> = ref.<span style="color: #006600;">r</span>;
&nbsp;
						<span style="color: #0066CC;">this</span>.<span style="color: #006600;">poolPush</span><span style="color: #66cc66;">&#40;</span> ref <span style="color: #66cc66;">&#41;</span>;
						<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
&nbsp;
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * @return the ratio of left to right tree card
		 * large number means heavy weight on left side, negavtive numbers means weight on right side
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTreeBalance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">isEmpty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> l:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">this</span>._card<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> r:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">this</span>._card<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> l <span style="color: #66cc66;">&gt;</span> r <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> l <span style="color: #66cc66;">/</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">max</span><span style="color: #66cc66;">&#40;</span> r, <span style="color: #cc66cc;">0.01</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> r <span style="color: #66cc66;">&gt;</span> l <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span> r <span style="color: #66cc66;">/</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">max</span><span style="color: #66cc66;">&#40;</span> l, <span style="color: #cc66cc;">0.01</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * creates an string representation of the map
		 * @return String value of map
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._toString<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_root</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * recursice helper method for toString
		 * @param node value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt;, processed node
		 * @return string reprsentation
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _toString<span style="color: #66cc66;">&#40;</span> node:MapAsBinarySearchTreeElement <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>node <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&quot;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._toString<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">l</span> <span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">' {k:'</span> + node.<span style="color: #0066CC;">key</span> + <span style="color: #ff0000;">' v:'</span>+ node.<span style="color: #006600;">value</span> +<span style="color: #ff0000;">'} '</span> + <span style="color: #0066CC;">this</span>._toString<span style="color: #66cc66;">&#40;</span> node.<span style="color: #006600;">r</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Returns the cardinality of the pool - the number of elements
		 * @return A value of &lt;code&gt;Int&lt;/code&gt;, the number of elemets
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPoolSize<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> ref:MapAsBinarySearchTreeElement = <span style="color: #0066CC;">this</span>._poolRoot;
			<span style="color: #000000; font-weight: bold;">var</span> card:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
			<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span> ref <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				ref = ref.<span style="color: #006600;">r</span>;
				++card;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> card;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * removes all unused elements from pool
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> emptyPool<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._poolRoot = <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * pushed an element to the local pool
		 * @private
		 * @param n a value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt; Item to push into pool
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> poolPush<span style="color: #66cc66;">&#40;</span> n:MapAsBinarySearchTreeElement <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// empty pool</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>._poolRoot == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">this</span>._poolRoot = n;
				<span style="color: #0066CC;">this</span>._poolRoot.<span style="color: #006600;">r</span> = <span style="color: #000000; font-weight: bold;">null</span>;
				<span style="color: #0066CC;">this</span>._poolRoot.<span style="color: #006600;">l</span> = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// non empty pool, keep reference</span>
			<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
				n.<span style="color: #006600;">r</span> = <span style="color: #0066CC;">this</span>._poolRoot;
				<span style="color: #0066CC;">this</span>._poolRoot = n;
				<span style="color: #0066CC;">this</span>._poolRoot.<span style="color: #006600;">l</span> = <span style="color: #000000; font-weight: bold;">null</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * gets an objects from pool. if pool is empty element will be created
		 * @private
		 * @return a value of &lt;code&gt;MapAsBinarySearchTreeElement&lt;/code&gt; - single list item
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> poolPop<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:MapAsBinarySearchTreeElement<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// non empty pool, take first from pool</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>._poolRoot <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> res:MapAsBinarySearchTreeElement = <span style="color: #0066CC;">this</span>._poolRoot;
&nbsp;
				<span style="color: #0066CC;">this</span>._poolRoot = <span style="color: #0066CC;">this</span>._poolRoot.<span style="color: #006600;">r</span>;
				res.<span style="color: #006600;">r</span> = <span style="color: #000000; font-weight: bold;">null</span>; <span style="color: #808080; font-style: italic;">// empty reference to pool nodes</span>
				res.<span style="color: #006600;">l</span> = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
				<span style="color: #b1b100;">return</span> res;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// empty pool, create new element</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MapAsBinarySearchTreeElement<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><a  href="http://blog.sebastian-martens.de/wp-content/uploads/2009/05/as3datastructures.zip">Download AS3DataStructure.zip</a></p>
<p>&#8230; to be continued.</p>
<p>cheers,<br />
Sebastian</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sebastian-martens.de/2009/05/actionscript-data-structures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

