Skip to content

Commit 060de10

Browse files
committed
Added graph visualisation, minor edits on 4.2
1 parent 3f649a3 commit 060de10

File tree

6 files changed

+62
-25
lines changed

6 files changed

+62
-25
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
venv/
2+
graph_out/
3+
.vscode/
4+
settings.json
5+
6+
*/__pycache__/*

__init__.py

Whitespace-only changes.

p4.2.py

+14-25
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,38 @@
11
from typing import List
2+
from utils.graphs import BiNode
3+
from utils.treeviz import *
24
# CTCI 4.2
35

6+
# Random links
7+
# https://orth.uk
8+
# https://foodprint.orth.uk for software engineering project
9+
10+
# https://devwebcl.blogspot.com/2016/12/big-o-comparison.html
11+
# https://awwapp.com/b/umyxkgethk506/
12+
413

514
def main():
615
# ordered_list = [1, 2, 3, 6]
716
ordered_list = [1, 2, 3, 6, 10, 12, 15, 16]
817
print(ordered_list)
9-
result = f"Result : {create_binary_from_sorted_arr(ordered_list)}"
18+
result = create_binary_from_sorted_arr(ordered_list)
19+
viz_tree(result)
1020
print(result)
1121

1222

13-
class Node:
14-
val: int = None
15-
left = None
16-
right = None
17-
18-
def __str__(self):
19-
return f"({self.left if self.left else '_' }/{self.val}\\{self.right if self.right else '_'})"
20-
21-
def __init__(self, val, left, right):
22-
self.val = val
23-
self.left = left
24-
self.right = right
25-
26-
# https://orth.uk
27-
# https://foodprint.orth.uk for software engineering project
28-
29-
30-
def create_binary_from_sorted_arr(arr: List[int]) -> Node:
23+
def create_binary_from_sorted_arr(arr: List[int]) -> BiNode:
3124
if (len(arr) == 0):
3225
return None
3326
if (len(arr) == 1):
34-
return Node(arr[0], None, None)
27+
return BiNode(arr[0], None, None)
3528

3629
# pick out middle number
3730
middle_index = len(arr) // 2
3831

3932
# recursively call
4033
left = create_binary_from_sorted_arr(arr[0:middle_index])
4134
right = create_binary_from_sorted_arr(arr[middle_index + 1:])
42-
return Node(arr[middle_index], left, right)
43-
44-
# https://devwebcl.blogspot.com/2016/12/big-o-comparison.html
45-
46-
# https://awwapp.com/b/umyxkgethk506/
35+
return BiNode(arr[middle_index], left, right)
4736

4837

4938
if __name__ == "__main__":

utils/__init__.py

Whitespace-only changes.

utils/graphs.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class BiNode:
2+
val: int = None
3+
left = None
4+
right = None
5+
6+
def __str__(self):
7+
return f"({self.left if self.left else '_' }/{self.val}\\{self.right if self.right else '_'})"
8+
9+
def __init__(self, val, left, right):
10+
self.val = val
11+
self.left = left
12+
self.right = right

utils/treeviz.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from graphviz import Digraph
2+
from utils.graphs import BiNode
3+
4+
def viz_tree(root: BiNode):
5+
graf = Digraph()
6+
7+
q = [root]
8+
9+
node_id, node_val = get_node_for_graph(root)
10+
graf.node(node_id, node_val)
11+
12+
while q:
13+
now_node = q.pop(0)
14+
now_node_id, _ = get_node_for_graph(now_node)
15+
16+
for node in (now_node.left, now_node.right):
17+
if node is not None:
18+
node_id, node_val = get_node_for_graph(node)
19+
graf.node(node_id, node_val)
20+
graf.edge(now_node_id, node_id)
21+
q.append(node)
22+
23+
graf.render(filename='graph_out/result.dot')
24+
25+
def get_node_for_graph(node):
26+
node_id = str(id(node))
27+
node_val = str(node.val)
28+
return node_id, node_val
29+
30+

0 commit comments

Comments
 (0)