forked from kbeathanabhotla/DSAAJ2-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chap04.text.01.TreeMemoryUsageTest.java
163 lines (138 loc) · 5.42 KB
/
Chap04.text.01.TreeMemoryUsageTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//Chap04.text.01.TreeMemoryUsageTest.java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class TreeMemoryUsageTest {
public static final int CHILDREN_NUMBER = 2000;
public static void main(String... args) {
TreeNodeDirectLinksArray.test();
TreeNodeDirectLinksArrayPreAllocated.test();
TreeNodeDirectLinksArrayList.test();
TreeNodeNextSibling.test();
TripleTree.test();
TripleTree.test2();
}
public static <T extends Serializable> void printObjectSize(T t) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(t);
oos.close();
System.out.println(t.getClass().getName() + ": " + baos.size());
} catch (IOException e) {
e.printStackTrace();
}
}
}
class TripleTree implements Serializable {
int data;
TripleTree a, b, c;
TripleTree(int d) {
data = d;
}
static void test() {
int d = 0;
TripleTree root = new TripleTree(d++);
root.a = new TripleTree(d++);
root.b = new TripleTree(d++);
root.c = new TripleTree(d++);
root.a.a = new TripleTree(d++);
root.a.b = new TripleTree(d++);
root.a.c = new TripleTree(d++);
root.b.a = new TripleTree(d++);
root.b.b = new TripleTree(d++);
root.b.c = new TripleTree(d++);
root.c.a = new TripleTree(d++);
root.c.b = new TripleTree(d++);
root.c.c = new TripleTree(d++);
System.out.print(d);
TreeMemoryUsageTest.printObjectSize(root);
}
static void test2() {
int d = 0;
TreeNodeNextSibling root = new TreeNodeNextSibling(d++);
root.firstChild = new TreeNodeNextSibling(d++);
root.firstChild.nextSibling = new TreeNodeNextSibling(d++);
root.firstChild.nextSibling.nextSibling = new TreeNodeNextSibling(d++);
root.firstChild.firstChild = new TreeNodeNextSibling(d++);
root.firstChild.firstChild.nextSibling = new TreeNodeNextSibling(d++);
root.firstChild.firstChild.nextSibling.nextSibling = new TreeNodeNextSibling(d++);
root.firstChild.nextSibling.firstChild = new TreeNodeNextSibling(d++);
root.firstChild.nextSibling.firstChild.nextSibling = new TreeNodeNextSibling(d++);
root.firstChild.nextSibling.firstChild.nextSibling.nextSibling = new TreeNodeNextSibling(d++);
root.firstChild.nextSibling.nextSibling.firstChild = new TreeNodeNextSibling(d++);
root.firstChild.nextSibling.nextSibling.firstChild.nextSibling = new TreeNodeNextSibling(d++);
root.firstChild.nextSibling.nextSibling.firstChild.nextSibling.nextSibling = new TreeNodeNextSibling(d++);
System.out.print(d);
TreeMemoryUsageTest.printObjectSize(root);
}
}
class TreeNodeNextSibling implements Serializable {
int data;
TreeNodeNextSibling firstChild;
TreeNodeNextSibling nextSibling;
TreeNodeNextSibling(int d) {
data = d;
}
static void test() {
int d = 1;
TreeNodeNextSibling root = new TreeNodeNextSibling(d++);
root.firstChild = new TreeNodeNextSibling(d++);
TreeNodeNextSibling temp = root.firstChild;
for (int i = 0; i < TreeMemoryUsageTest.CHILDREN_NUMBER - 1; i++) {
temp.nextSibling = new TreeNodeNextSibling(d++);
temp = temp.nextSibling;
}
TreeMemoryUsageTest.printObjectSize(root);
}
}
class TreeNodeDirectLinksArray implements Serializable {
int data;
TreeNodeDirectLinksArray[] children;
TreeNodeDirectLinksArray(int d) {
data = d;
}
static void test() {
int d = 1;
TreeNodeDirectLinksArray root = new TreeNodeDirectLinksArray(d++);
root.children = new TreeNodeDirectLinksArray[TreeMemoryUsageTest.CHILDREN_NUMBER];
for (int i = 0; i < TreeMemoryUsageTest.CHILDREN_NUMBER; i++) {
root.children[i] = new TreeNodeDirectLinksArray(d++);
}
TreeMemoryUsageTest.printObjectSize(root);
}
}
class TreeNodeDirectLinksArrayPreAllocated implements Serializable {
int data;
TreeNodeDirectLinksArrayPreAllocated[] children;
TreeNodeDirectLinksArrayPreAllocated(int d) {
data = d;
children = new TreeNodeDirectLinksArrayPreAllocated[TreeMemoryUsageTest.CHILDREN_NUMBER];
}
static void test() {
int d = 1;
TreeNodeDirectLinksArrayPreAllocated root = new TreeNodeDirectLinksArrayPreAllocated(d++);
for (int i = 0; i < TreeMemoryUsageTest.CHILDREN_NUMBER; i++) {
root.children[i] = new TreeNodeDirectLinksArrayPreAllocated(d++);
}
TreeMemoryUsageTest.printObjectSize(root);
}
}
class TreeNodeDirectLinksArrayList implements Serializable {
int data;
ArrayList<TreeNodeDirectLinksArrayList> children;
TreeNodeDirectLinksArrayList(int d) {
data = d;
children = new ArrayList<TreeNodeDirectLinksArrayList>();
}
static void test() {
int d = 1;
TreeNodeDirectLinksArrayList root = new TreeNodeDirectLinksArrayList(d++);
for (int i = 0; i < TreeMemoryUsageTest.CHILDREN_NUMBER; i++) {
root.children.add(new TreeNodeDirectLinksArrayList(d++));
}
TreeMemoryUsageTest.printObjectSize(root);
}
}