コード例 #1
0
ファイル: TopM.cs プロジェクト: IonicaBizauKitchen/ASD2015
        public static void Main(string[] args)
        {
            int M = 5;
            MinPQ<int> pq = new MinPQ<int>(M + 1);

            //StreamReader fs = new StreamReader("tinyW.txt");
            //StreamReader fs = new StreamReader("tinyT.txt");
            //StreamReader fs = new StreamReader("largeW.txt");
            StreamReader fs = new StreamReader("largeT.txt");
            string line;
            while (!fs.EndOfStream)
            {
                line = fs.ReadLine();
                pq.insert(int.Parse(line));

                // eliminam valoarea minima din coada cu prioritate daca sunt M+1 elemente in coada
                if (pq.size() > M)
                    pq.delMin();
            } // cele mai mari M elemente sunt in coada

            // afisam elementele din coada cu prioritate in ordine inversa
            Stack<int> stack = new Stack<int>();
            foreach (var item in pq)
                stack.push(item);

            foreach (var item in stack)
            {
                Console.WriteLine(item);
            }
        }
コード例 #2
0
    // build the Huffman trie given frequencies
    private static Node buildTrie(int[] freq) {

        // initialze priority queue with singleton trees
        MinPQ<Node> pq = new MinPQ<Node>();
        for (char i = 0; i < R; i++)
            if (freq[i] > 0)
                pq.insert(new Node(i, freq[i], null, null));

        // special case in case there is only one character with a nonzero frequency
        if (pq.size() == 1) {
            if (freq['\0'] == 0) pq.insert(new Node('\0', 0, null, null));
            else                 pq.insert(new Node('\1', 0, null, null));
        }

        // merge two smallest trees
        while (pq.size() > 1) {
            Node left  = pq.delMin();
            Node right = pq.delMin();
            Node parent = new Node('\0', left.freq + right.freq, left, right);
            pq.insert(parent);
        }
        return pq.delMin();
    }
コード例 #3
0
    /**
     *  Reads a sequence of transactions from standard input; takes a
     *  command-line integer m; prints to standard output the m largest
     *  transactions in descending order.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int m = Integer.parseInt(args[0]); 
        MinPQ<Transaction> pq = new MinPQ<Transaction>(m+1);

        while (StdIn.hasNextLine()) {
            // Create an entry from the next line and put on the PQ. 
            String line = StdIn.readLine();
            Transaction transaction = new Transaction(line);
            pq.insert(transaction); 

            // remove minimum if m+1 entries on the PQ
            if (pq.size() > m) 
                pq.delMin();
        }   // top m entries are on the PQ

        // print entries on PQ in reverse order
        Stack<Transaction> stack = new Stack<Transaction>();
        for (Transaction transaction : pq)
            stack.push(transaction);
        for (Transaction transaction : stack)
            StdOut.println(transaction);
    }