using UnityEngine; using System.Collections; using System.Collections.Generic; using System; public class TreeMap { TreeMapNode root; Comparer comparator; public TreeMap(IComparer icompere) { this.root = new EmptyNode (); } public TreeMap() { this.root = new EmptyNode (); this.comparator = Comparer.Default; } public TreeMap(TreeMapNode root) { this.root = root; } public TreeMap(TreeMapNode root, Comparer comparator) { this.root = root; this.comparator = comparator; } public TreeMapNode getRoot() { return root; } public V get(K key) { return root.get(key, this.comparator); } public TreeMap put(K key, V value) { if(isEmpty()) { TreeMapNode newRoot = new BlackNode (key, value, new EmptyNode (), new EmptyNode ()); return new TreeMap (newRoot, this.comparator); } TreeMapNode newEntry = root.put (key, value, this.comparator); TreeMapNode newRoots = new BlackNode (newEntry.getKey (), newEntry.getValue (), newEntry.lefts (), newEntry.rights ()); return new TreeMap (newRoots, this.comparator); } public bool isEmpty() { return !root.isNotEmpty (); } public TreeMap delete(K key) { if (key.Equals(default(K))) { return this; } rebuildNode rootRebuildNode = root.delete (key, null, this.comparator, Rotate.N); if (!rootRebuildNode.notEmpty ()) { return this; } TreeMapNode roots = rootRebuildNode.getNode (); if (!root.isNotEmpty ()) { return new TreeMap (new EmptyNode (), this.comparator); } TreeMapNode newRoot = new BlackNode (roots.getKey (), roots.getValue (), roots.lefts (), roots.rights ()); return new TreeMap (newRoot, this.comparator); } // like to iterator and IEmurator. public IEnumerator keys() { return new iterators (); } public void checkDepth() { root.checkDepth (0, 0); Debug.Log ("-----------------------------------"); } public class iterators : IEnumerator { Stack> nodeStack = new Stack>(); TreeMapNode currentNode = new TreeMap().getRoot(); public List appLines { get; set; } private int position; public bool MoveNext() { return currentNode != null; } public K Current{ get { K key = currentNode.getKey (); if (currentNode.lefts ().isNotEmpty ()) { nodeStack.Push (currentNode); currentNode = currentNode.lefts (); return key; } else if (currentNode.rights ().isNotEmpty ()) { currentNode = currentNode.rights (); return key; } else if (nodeStack.Count == 0) { currentNode = null; return key; } do { currentNode = nodeStack.Pop ().rights (); if (currentNode.isNotEmpty ()) { return key; } }while (nodeStack.Count != 0); return key; } } object IEnumerator.Current { get { return appLines; } } public void Dispose() { ((IEnumerator)this.appLines).Dispose (); } public void Reset() { ((IEnumerator)this.appLines).Reset(); } } }