001package algs33;
002import stdlib.*;
003import java.util.TreeMap;
004import algs32.BST;
005import com.javamex.classmexer.MemoryUtil;
006
007/* ***********************************************************************
008 *  Compilation:  javac -cp .:classmexer.jar XMemoryOfBSTs.java
009 *  Execution:    java  -cp .:classmexer.jar -javaagent:classmexer.jar XMemoryOfBSTs N
010 *  Dependencies: StdOut.java classmexer.jar
011 *
012 *
013 *************************************************************************/
014
015public class XMemoryOfBSTs {
016
017        public static void main(String[] args) {
018
019                int N = Integer.parseInt(args[0]);
020                //int START = 1000000;
021
022                StdOut.println("size of Integer");
023                Integer x = Integer.valueOf(123456);
024                StdOut.println(MemoryUtil.memoryUsageOf(x));
025                StdOut.println();
026                long SIZEOFINTEGER = MemoryUtil.memoryUsageOf(x);
027
028                TreeMap<Integer, Integer> st1 = new TreeMap<>();
029                StdOut.println("size of TreeMap<Integer, Integer> of given length");
030                for (int i = 0; i < 8; i++) {
031                        Integer key = Integer.valueOf(i);
032                        st1.put(key, key);
033                        StdOut.println(i + ": " + (MemoryUtil.deepMemoryUsageOf(st1) - SIZEOFINTEGER*i));
034                }
035                for (int i = 8; i < N; i++) {
036                        Integer key = Integer.valueOf(i);
037                        st1.put(key, key);
038                }
039                StdOut.println(N + ": " + (MemoryUtil.deepMemoryUsageOf(st1) - SIZEOFINTEGER*N));
040                StdOut.println();
041
042                BST<Integer, Integer> st2 = new BST<>();
043                StdOut.println("size of BST<Integer, Integer> of given length");
044                for (int i = 0; i < 8; i++) {
045                        Integer key = Integer.valueOf(i);
046                        st2.put(key, key);
047                        StdOut.println(i + ": " + (MemoryUtil.deepMemoryUsageOf(st2) - SIZEOFINTEGER*i));
048                }
049                for (int i = 8; i < N; i++) {
050                        Integer key = Integer.valueOf(i);
051                        st2.put(key, key);
052                }
053                StdOut.println(N + ": " + (MemoryUtil.deepMemoryUsageOf(st2) - SIZEOFINTEGER*N));
054                StdOut.println();
055
056
057                RedBlackBST<Integer, Integer> st3 = new RedBlackBST<>();
058                StdOut.println("size of RedBlackBST<Integer, Integer> of given length");
059                for (int i = 0; i < 8; i++) {
060                        Integer key = Integer.valueOf(i);
061                        st3.put(key, key);
062                        StdOut.println(i + ": " + (MemoryUtil.deepMemoryUsageOf(st3) - SIZEOFINTEGER*i));
063                }
064                for (int i = 8; i < N; i++) {
065                        Integer key = Integer.valueOf(i);
066                        st3.put(key, key);
067                }
068                StdOut.println(N + ": " + (MemoryUtil.deepMemoryUsageOf(st3) - SIZEOFINTEGER*N));
069                StdOut.println();
070
071        }
072
073}