예제 #1
0
        void Add(TreeItem <T> currentNode, T data)
        {
            int result = Comporator.Compare(currentNode.Element, data);

            if (result > 0 && currentNode.LeftChild == null)
            {
                TreeItem <T> newNode = new TreeItem <T>()
                {
                    Element = data
                };
                currentNode.LeftChild = newNode;
            }
            else if (result > 0)
            {
                Add(currentNode.LeftChild, data);
            }
            else if (result < 0 && currentNode.RightChild == null)
            {
                TreeItem <T> newNode = new TreeItem <T>()
                {
                    Element = data
                };
                currentNode.RightChild = newNode;
            }
            else if (result < 0)
            {
                Add(currentNode.RightChild, data);
            }
        }
예제 #2
0
            //Visit all nodes within a range
            internal List <SweepEvent> doVisit(SweepEvent lo, SweepEvent hi, Comporator compare, List <SweepEvent> list, Visit visit, RBNode node)
            {
                var l = compare(lo, node.key);
                var h = compare(hi, node.key);

                if (l <= 0)
                {
                    if (node.left != null)
                    {
                        var v1 = doVisit(lo, hi, compare, list, visit, node.left);
                        if (v1 != null)
                        {
                            return(v1);
                        }
                    }
                    if (h > 0)
                    {
                        var v2 = visit(node.key, node.value, list);
                        if (v2 != null)
                        {
                            return(v2);
                        }
                    }
                }
                if (h > 0 && node.right != null)
                {
                    return(doVisit(lo, hi, compare, list, visit, node.right));
                }
                return(null);
            }
예제 #3
0
            internal void _down(int pos)
            {
                List <SweepEvent> data    = this.data;
                Comporator        compare = this.compare;
                int len = this.length;

                while (true)
                {
                    int left  = 2 * pos + 1,
                        right = left + 1,
                        min   = pos;

                    if (left < len && compare(data[left], data[min]) < 0)
                    {
                        min = left;
                    }
                    if (right < len && compare(data[right], data[min]) < 0)
                    {
                        min = right;
                    }

                    if (min == pos)
                    {
                        return;
                    }

                    swap(data, min, pos);
                    pos = min;
                }
            }
예제 #4
0
            //Build a tree
            internal RedBlackTree(Comporator _compare)
            {
                //*if (_compare != null)
                this.compare = _compare;
                ///*else
                //    this.compare = defaultCompare;

                this.root = null;
            }
예제 #5
0
            //Default comparison function
            //internal int? defaultCompare<T>(T a, T b)
            //{
            //    if ( a is long && b is long)
            //    {
            //        long a1 = (long)Convert.ChangeType(a, typeof(long));
            //        long b1 = (long)Convert.ChangeType(b, typeof(long));

            //        return ((a1 < b1) ? -1 : ((a1 > b1) ? 1 : 0));
            //    }
            //    return null;
            //}

            internal RedBlackTree(Comporator _compare, RBNode _root)
            {
                //if( _compare != null )
                this.compare = _compare;
                //else
                //    this.compare = defaultCompare;

                this.root = _root;
            }
예제 #6
0
파일: Program.cs 프로젝트: KarpukDm/Sorting
        private static void RunAlgorithm(List<IComparable> values, IAlgorithm<IComparable, IList<IComparable>> algorithm)
        {
            Comporator<IComparable> comparator = new Comporator<IComparable>();

            List<IComparable> v = new List<IComparable>();
            v.AddRange(values);
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            var result = algorithm.Sort(v, comparator);
            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            PrintResult(result, ts);
        }
예제 #7
0
            internal TinyQueue(List <SweepEvent> data, Comporator compare)
            {
                //if (!(this instanceof TinyQueue))
                //    return new TinyQueue(data, compare);

                this.data    = data ?? new List <SweepEvent>();;
                this.length  = this.data.Count;
                this.compare = compare;// ?? defaultCompare;

                if (data != null)
                {
                    for (int i = (int)Math.Floor((double)this.length / 2.0); i >= 0; i--)
                    {
                        this._down(i);
                    }
                }
            }
예제 #8
0
            internal void _up(int pos)
            {
                List <SweepEvent> data    = this.data;
                Comporator        compare = this.compare;

                while (pos > 0)
                {
                    int parent = (int)Math.Floor((double)(pos - 1) / 2.0);
                    if (compare(data[pos], data[parent]) < 0)
                    {
                        swap(data, parent, pos);
                        pos = parent;
                    }
                    else
                    {
                        break;
                    }
                }
            }
예제 #9
0
            //Insert a new item into the tree
            internal RedBlackTree insert(SweepEvent key, SweepEvent value)
            {
                Comporator cmp = this.compare;
                //Find point to insert new node at
                RBNode        n1      = this.root;
                List <RBNode> n_stack = new List <RBNode>();
                List <int?>   d_stack = new List <int?>();

                while (n1 != null)
                {
                    int?d = cmp(key, n1.key);
                    n_stack.Add(n1);
                    d_stack.Add(d);
                    if (d <= 0)
                    {
                        n1 = n1.left;
                    }
                    else
                    {
                        n1 = n1.right;
                    }
                }

                //Rebuild path to leaf node
                n_stack.Add(new RBNode(RBTreeNodeColor.RED, key, value, null, null, 1));
                for (int i = n_stack.Count - 2; i > -1; i--)
                {
                    RBNode n_i = n_stack[i];
                    if (d_stack[i] <= 0)
                    {
                        n_stack[i] = new RBNode(n_i.color, n_i.key, n_i.value, n_stack[i + 1], n_i.right, n_i.count + 1);
                    }
                    else
                    {
                        n_stack[i] = new RBNode(n_i.color, n_i.key, n_i.value, n_i.left, n_stack[i + 1], n_i.count + 1);
                    }
                }

                //Rebalance tree using rotations
                for (int i = n_stack.Count - 1; i > 1; i--)
                {
                    RBNode p = n_stack[i - 1];
                    RBNode n = n_stack[i];
                    if (p.color == RBTreeNodeColor.BLACK || n.color == RBTreeNodeColor.BLACK)
                    {
                        break;
                    }

                    RBNode pp = n_stack[i - 2];
                    if (pp.left == p)
                    {
                        if (p.left == n)
                        {
                            RBNode y = pp.right;
                            if (y != null && y.color == RBTreeNodeColor.RED)
                            {
                                p.color  = RBTreeNodeColor.BLACK;
                                pp.right = y.repaint(RBTreeNodeColor.BLACK);
                                pp.color = RBTreeNodeColor.RED;
                                i--;
                            }
                            else
                            {
                                pp.color       = RBTreeNodeColor.RED;
                                pp.left        = p.right;
                                p.color        = RBTreeNodeColor.BLACK;
                                p.right        = pp;
                                n_stack[i - 2] = p;
                                n_stack[i - 1] = n;
                                pp.recount();
                                p.recount();
                                if (i >= 3)
                                {
                                    RBNode ppp = n_stack[i - 3];
                                    if (ppp.left == pp)
                                    {
                                        ppp.left = p;
                                    }
                                    else
                                    {
                                        ppp.right = p;
                                    }
                                }
                                break;
                            }
                        }
                        else
                        {
                            RBNode y = pp.right;
                            if (y != null && y.color == RBTreeNodeColor.RED)
                            {
                                p.color  = RBTreeNodeColor.BLACK;
                                pp.right = y.repaint(RBTreeNodeColor.BLACK);
                                pp.color = RBTreeNodeColor.RED;
                                i--;
                            }
                            else
                            {
                                p.right        = n.left;
                                pp.color       = RBTreeNodeColor.RED;
                                pp.left        = n.right;
                                n.color        = RBTreeNodeColor.BLACK;
                                n.left         = p;
                                n.right        = pp;
                                n_stack[i - 2] = n;
                                n_stack[i - 1] = p;
                                pp.recount();
                                p.recount();
                                n.recount();
                                if (i >= 3)
                                {
                                    RBNode ppp = n_stack[i - 3];
                                    if (ppp.left == pp)
                                    {
                                        ppp.left = n;
                                    }
                                    else
                                    {
                                        ppp.right = n;
                                    }
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (p.right == n)
                        {
                            RBNode y = pp.left;
                            if (y != null && y.color == RBTreeNodeColor.RED)
                            {
                                p.color  = RBTreeNodeColor.BLACK;
                                pp.left  = y.repaint(RBTreeNodeColor.BLACK);
                                pp.color = RBTreeNodeColor.RED;
                                i--;
                            }
                            else
                            {
                                pp.color       = RBTreeNodeColor.RED;
                                pp.right       = p.left;
                                p.color        = RBTreeNodeColor.BLACK;
                                p.left         = pp;
                                n_stack[i - 2] = p;
                                n_stack[i - 1] = n;
                                pp.recount();
                                p.recount();
                                if (i >= 3)
                                {
                                    RBNode ppp = n_stack[i - 3];
                                    if (ppp.right == pp)
                                    {
                                        ppp.right = p;
                                    }
                                    else
                                    {
                                        ppp.left = p;
                                    }
                                }
                                break;
                            }
                        }
                        else
                        {
                            RBNode y = pp.left;
                            if (y != null && y.color == RBTreeNodeColor.RED)
                            {
                                p.color  = RBTreeNodeColor.BLACK;
                                pp.left  = y.repaint(RBTreeNodeColor.BLACK);
                                pp.color = RBTreeNodeColor.RED;
                                i--;
                            }
                            else
                            {
                                p.left         = n.right;
                                pp.color       = RBTreeNodeColor.RED;
                                pp.right       = n.left;
                                n.color        = RBTreeNodeColor.BLACK;
                                n.right        = p;
                                n.left         = pp;
                                n_stack[i - 2] = n;
                                n_stack[i - 1] = p;
                                pp.recount();
                                p.recount();
                                n.recount();
                                if (i >= 3)
                                {
                                    RBNode ppp = n_stack[i - 3];
                                    if (ppp.right == pp)
                                    {
                                        ppp.right = n;
                                    }
                                    else
                                    {
                                        ppp.left = n;
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
                //Return new tree
                n_stack[0].color = RBTreeNodeColor.BLACK;
                return(new RedBlackTree(cmp, n_stack[0]));
            }