//key es el color
		//value es el número de repeticiones
		private Hashtable GenerateHistogram(CCFTree tree, Bitmap original)
		{
			ArrayList colores = tree.ExportToArray();

			Hashtable output = new Hashtable();
			Hashtable closest = new Hashtable();
			Color entry, pixel;

			int i,j;
			for(i=0;i<original.Width;i++)
			{
				for(j=0;j<original.Height;j++)
				{
					pixel = original.GetPixel(i,j);

					if (closest.ContainsKey(pixel))
					{
						entry = (Color)(closest[pixel]);
					}
					else
					{
						entry = FindNearest( pixel, colores );
						closest.Add(pixel,entry);
					}

					if (!output.Contains(entry))
						{output.Add(entry, 1);}
					else
						{output[entry] = (int)(output[entry]) + 1;}
				}
			}

			return output;
		}
		public CCFTreeEnumerator(CCFTree root): base()
		{
			_root = root;
			Reset();
		}
		public void SplitFather(CCFTree child)
		{
			// generate a new node newBranch and add newChild to newBrach;
			CCFTree newBranch = CCFTree.MakeEmptyLeaf(L,B,T);
			newBranch.AddChild(child);
			
			if (IsRoot)
			{
				//generate a new root newRoot, add father and newBranch to newRoot
				CCFTree     root         = this;
				CCFTree     tempTree     = CCFTree.MakeEmptyLeaf(L,B,T);
				ArrayList   tempChildren = new ArrayList();
				IEnumerator enumerator;

				enumerator = root.GetChildrenEnumerator();
				enumerator.Reset();
				while (enumerator.MoveNext())
					{tempChildren.Add(enumerator.Current);}
				root.Clear();
				enumerator = tempChildren.GetEnumerator();
				enumerator.Reset();
				while (enumerator.MoveNext())
					{tempTree.AddChild((CCFTree)enumerator.Current);}
				//tempTree has now the children of this
			
				root.AddChild(tempTree );
				root.AddChild(newBranch);
			}
			else
			{
				if (Father.CountChildren < B)
				{
					// add newBranch to father's father
					Father.AddChild(newBranch);
				}
				else
				{
					// call split_father(father's father, newBranch);
					Father.SplitFather(newBranch);
				}
			}
		}
		public void FindClosestLeafAndEntry(Color pixel, out CCFTree resultLeaf, out Color resultEntry)
		{
			CCFTree closestLeaf		= this;
			Color   closestEntry	= FindClosestEntryInLeaf(pixel);
			long	closestDistance	= CalculateDistance(closestEntry,pixel);

			if (CountChildren > 0)
			{
				CCFTree currentLeaf		= null;
				Color   currentEntry	= Color.Empty;
				long	currentDistance	= long.MaxValue;

				CCFTree enumeratorLeaf	= null;
				IEnumerator enumerator = GetChildrenEnumerator();
				enumerator.Reset();
				while (enumerator.MoveNext())
				{
//					currentLeaf     = (CCFTree)enumerator.Current;
//					currentEntry    = currentLeaf.FindClosestEntryInLeaf(pixel);
//					currentDistance = CalculateDistance(currentEntry,pixel);
					enumeratorLeaf = (CCFTree)enumerator.Current;
					enumeratorLeaf.FindClosestLeafAndEntry(pixel,out currentLeaf,out currentEntry);

					if (!currentEntry.Equals(Color.Empty))
					{
						if (closestEntry.Equals(Color.Empty))
						{
							closestEntry    = currentEntry;
							closestDistance = currentDistance;
							closestLeaf     = currentLeaf;
						}
						else
						{
							currentDistance = CalculateDistance(currentEntry,pixel);

							if (currentDistance < closestDistance)
							{
								closestEntry    = currentEntry;
								closestDistance = currentDistance;
								closestLeaf     = currentLeaf;
							}
						}
					}
				}
			}

			resultLeaf  = closestLeaf;
			resultEntry = closestEntry;
		}
		public void AddChild(CCFTree child)
		{
			if (child._Father != null) {child._Father.RemoveChild(this);}
			_Children.Add(child);
			child._Father = this;
		}
		public void RemoveChild(CCFTree child)
		{
			_Children.Remove(child);
			child._Father = null;
		}