Esempio n. 1
0
        public RbNode <T> Find(T key)
        {
            var current = Root;

            while (current != TNull)
            {
                if (RbNode <T> .IsEqualTo(key, current.Key))
                {
                    return(current);
                }

                current = RbNode <T> .IsGreaterThan(key, current.Key) ? current.Right : current.Left;
            }

            return(null);
        }
Esempio n. 2
0
        public void Insert(T key)
        {
            var x = new RbNode <T>()
            {
                Key   = key,
                Red   = true,
                Left  = TNull,
                Right = TNull,
            };

            if (Root != TNull)
            {
                var y = Root;
                var p = Root;

                while (y != TNull)
                {
                    p = y;
                    y = RbNode <T> .IsGreaterThan(x.Key, y.Key) ? y.Right : y.Left;
                }

                x.Parent = p;
                if (RbNode <T> .IsGreaterThan(x.Key, p.Key))
                {
                    x.Parent.Right = x;
                }
                else
                {
                    x.Parent.Left = x;
                }

                InsertFix(x);
            }
            else
            {
                Root     = x;
                Root.Red = false;
            }

            Count++;
        }