-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedianSum.java
More file actions
121 lines (94 loc) · 2.93 KB
/
Copy pathmedianSum.java
File metadata and controls
121 lines (94 loc) · 2.93 KB
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
package MyPractice;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class medianSum {
static int counter=0;
static int median=1;
static int nodesTraversed=0;
static int medianSum=0;
static ArrayList<Integer> nodesTraversedList = new ArrayList<>();
static LinkedList<Integer> queue = new LinkedList<>();
public static class BSTNode {
int data;
BSTNode left;
BSTNode right;
}
public static void main(String...args) {
// queue.add(3);
// queue.add(4);
// queue.add(2);
readFromFile();
int rootData = pollAndGetSize(queue);
BSTNode root = new BSTNode();
root.data=rootData;
medianSum=medianSum+rootData;
while (queue.size()>0)
{
int data = pollAndGetSize(queue);
insertNode(data,root);
int medianSize = isEven(counter)?counter/2:(counter+1)/2;
//System.out.println("Median size : " + medianSize);
nodesTraversedList=new ArrayList<>();
traverse(root);
// System.out.println("Median : " + nodesTraversedList.get(medianSize-1));
medianSum=medianSum+nodesTraversedList.get(medianSize-1);
}
System.out.println("Answer : " + medianSum);
}
public static void traverse(BSTNode root)
{
if(root==null)
return;
traverse(root.left);
nodesTraversedList.add(root.data);
traverse(root.right);
}
public static int pollAndGetSize(LinkedList<Integer> queue)
{
if(queue.size()>0) {
counter++;
return queue.poll();
}
return -1;
}
public static boolean isEven(int n)
{
return n%2==0;
}
public static BSTNode insertNode(int data, BSTNode root)
{
if(root==null)
{
BSTNode node = new BSTNode();
node.data=data;
return node;
}
if(data< root.data)
root.left= insertNode(data,root.left);
if(data> root.data)
root.right= insertNode(data,root.right);
return root;
}
public static void readFromFile() {
try {
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\gupta\\IdeaProjects\\untitled\\src\\MyPractice\\medianSum.txt"));
String line = reader.readLine();
// line = line.replaceAll("\s+","t");
while (line != null) {
// System.out.println(line);
// System.out.println(values[0]);
int src = Integer.parseInt(line);
queue.add(src);
// read next line
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}