数据结构——斜堆

原文:http://www.cnblogs.com/skywang12345/p/3638552.html

斜堆的介绍

斜堆(Skew heap)也叫自适应堆(self-adjusting heap),它是左倾堆的一个变种。和左倾堆一样,它通常也用于实现优先队列;作为一种自适应的左倾堆,它的合并操作的时间复杂度也是O(lg n)。
它与左倾堆的差别是:
(01) 斜堆的节点没有"零距离"这个属性,而左倾堆则有。
(02) 斜堆的合并操作和左倾堆的合并操作算法不同。

斜堆的合并操作
(01) 如果一个空斜堆与一个非空斜堆合并,返回非空斜堆。
(02) 如果两个斜堆都非空,那么比较两个根节点,取较小堆的根节点为新的根节点。将"较小堆的根节点的右孩子"和"较大堆"进行合并。
(03) 合并后,交换新堆根节点的左孩子和右孩子。
        第(03)步是斜堆和左倾堆的合并操作差别的关键所在,如果是左倾堆,则合并后要比较左右孩子的零距离大小,若右孩子的零距离 > 左孩子的零距离,则交换左右孩子;最后,在设置根的零距离。

 

斜堆的基本操作

1. 基本定义

 

    public class SkewHeap<T> where T : IComparable<T>
    {
        private SkewNode<T> mRoot;    // 根结点

        private class SkewNode<T> where T : IComparable<T>
        {
            public T key;                // 关键字(键值)
            public SkewNode<T> left;    // 左孩子
            public SkewNode<T> right;    // 右孩子

            public SkewNode(T key, SkewNode<T> left, SkewNode<T> right)
            {
                this.key = key;
                this.left = left;
                this.right = right;
            }

            public override string ToString()
            {
                return "key:" + key;
            }
        }
    }

 

SkewNode是斜堆对应的节点类。
SkewHeap是斜堆类,它包含了斜堆的根节点,以及斜堆的操作。

 

2. 合并

 

        /*
         * 合并"斜堆x"和"斜堆y"
         */
        private SkewNode<T> merge(SkewNode<T> x, SkewNode<T> y)
        {
            if (x == null) return y;
            if (y == null) return x;

            // 合并x和y时,将x作为合并后的树的根;
            // 这里的操作是保证: x的key < y的key
            if (x.key.CompareTo(y.key) > 0)
            {
                SkewNode<T> temp = x;
                x = y;
                y = temp;
            }

            // 将x的右孩子和y合并,
            // 合并后直接交换x的左右孩子,而不需要像左倾堆一样考虑它们的npl。
            SkewNode<T> tmp = merge(x.right, y);
            x.right = x.left;
            x.left = tmp;

            return x;
        }

        public void merge(SkewHeap<T> other)
        {
            this.mRoot = merge(this.mRoot, other.mRoot);
        }

 

merge(x, y)是内部接口,作用是合并x和y这两个斜堆,并返回得到的新堆的根节点。
merge(other)是外部接口,作用是将other合并到当前堆中。


3. 添加

 

        /* 
         * 新建结点(key),并将其插入到斜堆中
         *
         * 参数说明:
         *     key 插入结点的键值
         */
        public void insert(T key)
        {
            SkewNode<T> node = new SkewNode<T>(key, null, null);

            // 如果新建结点失败,则返回。
            if (node != null)
                this.mRoot = merge(this.mRoot, node);
        }

 

insert(key)的作用是新建键值为key的节点,并将其加入到当前斜堆中。

 

4. 删除

 

        /* 
         * 删除根结点
         * 
         * 返回值:
         *     返回被删除的节点的键值
         */
        public T remove()
        {
            if (this.mRoot == null)
                return default(T);

            T key = this.mRoot.key;
            SkewNode<T> l = this.mRoot.left;
            SkewNode<T> r = this.mRoot.right;

            this.mRoot = null;          // 删除根节点
            this.mRoot = merge(l, r);   // 合并左右子树

            return key;
        }

 

remove()的作用是删除斜堆的最小节点。

 

注意关于斜堆的"前序遍历"、"中序遍历"、"后序遍历"、"打印"、"销毁"等接口就不再单独介绍了。后文的源码中有给出它们的实现代码,Please RTFSC(Read The Fucking Source Code)!

 

斜堆的实现(完整源码)

斜堆的实现文件

 

    public class SkewHeap<T> where T : IComparable<T>
    {
        private SkewNode<T> mRoot;    // 根结点

        private class SkewNode<T> where T : IComparable<T>
        {
            public T key;                // 关键字(键值)
            public SkewNode<T> left;    // 左孩子
            public SkewNode<T> right;    // 右孩子

            public SkewNode(T key, SkewNode<T> left, SkewNode<T> right)
            {
                this.key = key;
                this.left = left;
                this.right = right;
            }

            public override string ToString()
            {
                return "key:" + key;
            }
        }

        public SkewHeap()
        {
            mRoot = null;
        }

        /*
         * 前序遍历"斜堆"
         */
        private void preOrder(SkewNode<T> heap)
        {
            if (heap != null)
            {
                Console.WriteLine(heap.key + " ");
                preOrder(heap.left);
                preOrder(heap.right);
            }
        }

        public void preOrder()
        {
            preOrder(mRoot);
        }

        /*
         * 中序遍历"斜堆"
         */
        private void inOrder(SkewNode<T> heap)
        {
            if (heap != null)
            {
                inOrder(heap.left);
                Console.WriteLine(heap.key + " ");
                inOrder(heap.right);
            }
        }

        public void inOrder()
        {
            inOrder(mRoot);
        }

        /*
         * 后序遍历"斜堆"
         */
        private void postOrder(SkewNode<T> heap)
        {
            if (heap != null)
            {
                postOrder(heap.left);
                postOrder(heap.right);
                Console.WriteLine(heap.key + " ");
            }
        }

        public void postOrder()
        {
            postOrder(mRoot);
        }

        /*
         * 合并"斜堆x"和"斜堆y"
         */
        private SkewNode<T> merge(SkewNode<T> x, SkewNode<T> y)
        {
            if (x == null) return y;
            if (y == null) return x;

            // 合并x和y时,将x作为合并后的树的根;
            // 这里的操作是保证: x的key < y的key
            if (x.key.CompareTo(y.key) > 0)
            {
                SkewNode<T> temp = x;
                x = y;
                y = temp;
            }

            // 将x的右孩子和y合并,
            // 合并后直接交换x的左右孩子,而不需要像左倾堆一样考虑它们的npl。
            SkewNode<T> tmp = merge(x.right, y);
            x.right = x.left;
            x.left = tmp;

            return x;
        }

        public void merge(SkewHeap<T> other)
        {
            this.mRoot = merge(this.mRoot, other.mRoot);
        }

        /* 
         * 新建结点(key),并将其插入到斜堆中
         *
         * 参数说明:
         *     key 插入结点的键值
         */
        public void insert(T key)
        {
            SkewNode<T> node = new SkewNode<T>(key, null, null);

            // 如果新建结点失败,则返回。
            if (node != null)
                this.mRoot = merge(this.mRoot, node);
        }

        /* 
         * 删除根结点
         * 
         * 返回值:
         *     返回被删除的节点的键值
         */
        public T remove()
        {
            if (this.mRoot == null)
                return default(T);

            T key = this.mRoot.key;
            SkewNode<T> l = this.mRoot.left;
            SkewNode<T> r = this.mRoot.right;

            this.mRoot = null;          // 删除根节点
            this.mRoot = merge(l, r);   // 合并左右子树

            return key;
        }

        /*
         * 销毁斜堆
         */
        private void destroy(SkewNode<T> heap)
        {
            if (heap == null)
                return;

            if (heap.left != null)
                destroy(heap.left);
            if (heap.right != null)
                destroy(heap.right);

            heap = null;
        }

        public void clear()
        {
            destroy(mRoot);
            mRoot = null;
        }

        /*
         * 打印"斜堆"
         *
         * key        -- 节点的键值 
         * direction  --  0,表示该节点是根节点;
         *               -1,表示该节点是它的父结点的左孩子;
         *                1,表示该节点是它的父结点的右孩子。
         */
        private void print(SkewNode<T> heap, T key, int direction)
        {

            if (heap != null)
            {

                if (direction == 0)    // heap是根节点
                    Console.WriteLine("{0} is root\n", heap.key);
                else                // heap是分支节点
                    Console.WriteLine("{0} is {1}'s {2} child\n", heap.key, key, direction == 1 ? "right" : "left");

                print(heap.left, heap.key, -1);
                print(heap.right, heap.key, 1);
            }
        }

        public void print()
        {
            if (mRoot != null)
                print(mRoot, mRoot.key, 0);
        }
    }

 

测试程序

 

        public void testSkewHeap()
        {
            int[] a = { 10, 40, 24, 30, 36, 20, 12, 16 };
            int[] b = { 17, 13, 11, 15, 19, 21, 23 };
            SkewHeap<int> ha = new SkewHeap<int>();
            SkewHeap<int> hb = new SkewHeap<int>();

            Console.WriteLine("== 斜堆(ha)中依次添加: ");
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write("{0} ", a[i]);
                ha.insert(a[i]);
            }
            Console.WriteLine("\n== 斜堆(ha)的详细信息: \n");
            ha.print();


            Console.WriteLine("\n== 斜堆(hb)中依次添加: ");
            for (int i = 0; i < b.Length; i++)
            {
                Console.WriteLine("{0} ", b[i]);
                hb.insert(b[i]);
            }
            Console.WriteLine("\n== 斜堆(hb)的详细信息: \n");
            hb.print();

            // 将"斜堆hb"合并到"斜堆ha"中。
            ha.merge(hb);
            Console.WriteLine("\n== 合并ha和hb后的详细信息: \n");
            ha.print();
        }

运行结果:

== 斜堆(ha)中依次添加: 10 40 24 30 36 20 12 16 
== 斜堆(ha)的详细信息: 
10 is root
16 is 10's   left child
20 is 16's   left child
30 is 20's   left child
40 is 30's   left child
12 is 10's  right child
24 is 12's   left child
36 is 24's   left child

== 斜堆(hb)中依次添加: 17 13 11 15 19 21 23 
== 斜堆(hb)的详细信息: 
11 is root
13 is 11's   left child
17 is 13's   left child
23 is 17's   left child
19 is 13's  right child
15 is 11's  right child
21 is 15's   left child

== 合并ha和hb后的详细信息: 
10 is root
11 is 10's   left child
12 is 11's   left child
15 is 12's   left child
21 is 15's   left child
24 is 12's  right child
36 is 24's   left child
13 is 11's  right child
17 is 13's   left child
23 is 17's   left child
19 is 13's  right child
16 is 10's  right child
20 is 16's   left child
30 is 20's   left child
40 is 30's   left child

 

 
 
 
posted @ 2019-05-09 23:57  luanxm  阅读(885)  评论(0编辑  收藏  举报