Skip to content

Commit 42175ff

Browse files
author
William Blankenship
committed
Test now outputs results of unit tests. tree_zig_zag.py for testing
1 parent 83deb55 commit 42175ff

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

test.js

+26
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,31 @@ exports.test = function(dir,name,command) {
3030
}
3131
}
3232
console.log(files);
33+
34+
//Start workers to test the files
35+
for(i=0;i<files.length;i++) {
36+
var exec = require('child_process').exec;
37+
var str = command+' < "'+dir+'/'+name+'/'+files[i]+'.input"';
38+
console.log(str);
39+
var child = exec(str, function(error,stdout,stderr) {
40+
console.log("stdout: "+stdout);
41+
});
42+
}
43+
}
44+
}
45+
46+
function runTest(id,command) {
47+
return function(error,input) {
48+
if(error) {
49+
console.log("Unable to open input for "+id);
50+
return;
51+
}
52+
console.log("running process: ",id);
53+
var exec = require('child_process').exec;
54+
var child = exec("exec "+command, function(error,stdout,stderr) {
55+
console.log("stdout "+id+": "+stdout);
56+
});
57+
console.log("writing to stdin: ",id);
58+
child.stdin.write(input);
3359
}
3460
}

tree_zig_zag.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
3+
import collections
4+
import sys
5+
6+
7+
class Node(object):
8+
__slots__ = ('value', 'left', 'right')
9+
10+
def __init__(self, value):
11+
self.value = value
12+
self.left = None
13+
self.right = None
14+
15+
def __hash__(self):
16+
return hash(self.value)
17+
18+
def __eq__(self, other):
19+
if id(self) == id(other):
20+
return True
21+
return self.value == other.value
22+
23+
def __str__(self):
24+
return "{value=%s, left=%s, right=%s}" % (self.value, self.left, self.right)
25+
26+
def __repr__(self):
27+
return str(self.value)
28+
29+
30+
class TreeReader(object):
31+
@classmethod
32+
def get_vertex(cls, value, vertices):
33+
if value == -1:
34+
return None
35+
key = Node(value)
36+
if key not in vertices:
37+
vertices[key] = key
38+
return vertices[key]
39+
40+
@classmethod
41+
def read_tree(cls, stream):
42+
root = None
43+
vertices = {}
44+
for line in stream:
45+
(vertex_value, left_value, right_value) = map(int, line.split())
46+
vertex = cls.get_vertex(vertex_value, vertices)
47+
vertex.left = cls.get_vertex(left_value, vertices)
48+
vertex.right = cls.get_vertex(right_value, vertices)
49+
if root is None:
50+
root = vertex
51+
return root
52+
53+
54+
def zig_zag_traverse(root):
55+
is_left_most = True
56+
current_level = []
57+
sentinel = Node(-1)
58+
queue = collections.deque([root, sentinel])
59+
while len(queue) != 0:
60+
node = queue.popleft()
61+
if node == sentinel:
62+
index = 0 if is_left_most else len(current_level) - 1
63+
print(current_level[index].value)
64+
current_level = []
65+
is_left_most = not is_left_most
66+
if len(queue) != 0:
67+
queue.append(sentinel)
68+
else:
69+
current_level.append(node)
70+
queue.extend(x for x in [node.left, node.right] if x)
71+
72+
73+
def main():
74+
root = TreeReader.read_tree(sys.stdin)
75+
zig_zag_traverse(root)
76+
77+
78+
if __name__ == "__main__":
79+
sys.exit(main())

0 commit comments

Comments
 (0)