Exemple #1
0
        private bool HeapReq(TreapElement e)
        {
            TreapElement l = null, r = null;

            if (e == null)
            {
                return(true);
            }

            if (e.left != null)
            {
                l = (TreapElement)e.left;
            }

            if (e.right != null)
            {
                r = (TreapElement)e.right;
            }

            if ((l == null || l.heap > e.heap) && (r == null || r.heap > e.heap))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public override void LeftRotation()
        {
            TreapElement r = (TreapElement)right;

            int tmp = heap;

            heap   = r.heap;
            r.heap = tmp;

            base.LeftRotation();
        }
        public override void RightRotation()
        {
            TreapElement l = (TreapElement)left;

            int tmp = heap;

            heap   = l.heap;
            l.heap = tmp;


            base.RightRotation();
        }
Exemple #4
0
        public bool Insert(int element)
        {
            int        heap = Convert.ToInt32((Console.ReadLine()));
            InfoReturn res  = base.Insert(data, new TreapElement(element, heap));

            if (res.success)
            {
                data = (TreapElement)res.newRoot;
            }

            DownHeap(element);

            return(res.success);
        }
Exemple #5
0
        public bool Delete(int element)
        {
            // Pruefen, ob Element Exist
            if (!base.Search(data, element)) //Element nicht gefunden
            {
                return(false);
            }

            // Element zu Blatt machen
            UpHeap(element);

            // Element loeschen
            InfoReturn res = base.Delete(data, element);

            if (res.success)
            {
                data = (TreapElement)res.newRoot;
            }

            return(res.success);
        }
Exemple #6
0
        // Support methods
        private void DownHeap(int element)
        {
            SearchResult s       = data.SearchElement(element);
            TreapElement current = (TreapElement)s.previous;
            bool         isLeft;

            while (!HeapReq(current))
            {
                isLeft = (current.left == s.found) ? true : false;

                if (isLeft)
                {
                    current.RightRotation();
                }
                else
                {
                    current.LeftRotation();
                }

                s       = data.SearchElement(element);
                current = (TreapElement)s.previous;
            }
        }
Exemple #7
0
        private void UpHeap(int element)
        {
            SearchResult s = data.SearchElement(element);
            TreapElement current = (TreapElement)s.found;
            TreapElement l = (TreapElement)current.left, r = (TreapElement)current.right;

            while (current.left != null && current.right != null)
            {
                if (l != null && l.heap <= r.heap)
                {
                    current.RightRotation();
                }
                else
                {
                    current.LeftRotation();
                }


                s       = data.SearchElement(element);
                current = (TreapElement)s.found;
                l       = (TreapElement)current.left;
                r       = (TreapElement)current.right;
            }
        }