コード例 #1
0
        /// <summary>
        /// we can use a binary tree to represent prefix free code where 0 means go to the left child and 1 means go to the right child.
        /// Let c.freq denote the frequency of c in the file and let dt(c) denote the depth of c's leaf in the tree.
        /// The number of bits required to encode a file is
        /// sum(c.freq*dt(c))
        ///
        ///
        /// this runs at O(nlgn)
        /// </summary>
        public static Character <T> Build(List <Tuple <T, double> > characters)
        {
            if (characters == null || characters.Count <= 0)
            {
                throw new ArgumentNullException("argument cann't be null or empty");
            }

            int n = characters.Count;

            Character <T>[] arrs = new Character <T> [n];
            //build a min priority queue using minheap
            for (int i = 0; i < n; i++)
            {
                arrs[i] = new Character <T>(characters[i].Item1, characters[i].Item2);
            }

            MinHeap <Character <T> > heap = MinHeap <Character <T> > .BuildMinHeap(arrs);

            for (int i = 0; i < n - 1; i++)
            {
                Character <T> combine = new Character <T>();
                Character <T> left    = heap.Pop();
                Character <T> right   = heap.Pop();
                combine.Left  = left;
                combine.Right = right;
                combine.Freq  = left.Freq + right.Freq;
                heap.Insert(combine);
            }

            return(heap.Peek());
        }
コード例 #2
0
 /** initialize data structure */
 public MedianFinder()
 {
     lowHeap = new MaxHeap(new List <int>());
     lowHeap.BuildMaxHeap();
     highHeap = new MinHeap(new List <int>());
     highHeap.BuildMinHeap();
     even = true;
 }
コード例 #3
0
        public void BuildMinHeapGeneratesCorrectMinHeap()
        {
            heap.BuildMinHeap();
            var validData = new int[6] {
                0, 1, 2, 5, 4, 6
            };

            for (int i = 0; i < validData.Length; i++)
            {
                if (validData[i] != heap.Data[i])
                {
                    Assert.Fail("Heap has not generated correct Max Heap order");
                }
            }
        }
コード例 #4
0
 public void Init()
 {
     heap = new MinHeap <int>(heapData);
     heap.BuildMinHeap();
 }
コード例 #5
0
        private void button23_Click(object sender, EventArgs e)
        {
            Form1 f1 = (Form1)Application.OpenForms["Form1"];

            f1.GB_Status_AppendText_Nextline("--------------------------------", Color.Black);

            //MinHeap mh = new MinHeap(15);
            MinHeap <int> mh = new MinHeap <int>(15);

            mh.Insert(77);
            mh.Insert(7);
            mh.Insert(64);
            mh.Insert(21);
            mh.Insert(89);
            mh.Insert(1);
            mh.Insert(92);
            mh.Insert(17);
            mh.Insert(3);
            mh.Insert(30);
            mh.Insert(42);
            mh.Insert(50);
            mh.Insert(2);
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("1) mh.getMin() : " + mh.getMin(), Color.Red);
            mh.RemoveMin();
            mh.displayHeap();



            f1.GB_Status_AppendText_Nextline("--------------------------------", Color.Black);

            int[] arr = { 77, 7, 64, 21, 89, 1, 92, 17, 3, 30, 42, 50, 2 };
            mh.BuildMinHeap(arr);
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();

            f1.GB_Status_AppendText_Nextline("2) mh.getMin() : " + mh.getMin(), Color.Green);
            mh.RemoveMin();
            mh.displayHeap();
        }