forked from alexhagiopol/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_basics.py
107 lines (83 loc) · 2.93 KB
/
tree_basics.py
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
# Implementations of structures and functions related to trees and graphs.
import queue
class BinaryNode: # binary tree node
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
class BinaryNodeLP: # binary node with link to parent
def __init__(self, val, left=None, right=None, parent=None):
self.val = val
self.left = left
self.right = right
self.parent = parent
def __eq__(self, other):
if self.val == other.val:
return True
return False
class LLElem: # linked list element. Used for P 4.3 that demands linked list
def __init__(self, val, next_elem=None):
self.val = val
self.next_elem = next_elem
def in_order_traversal(node, visit_sequence):
if node is not None:
in_order_traversal(node.left, visit_sequence)
visit_sequence.append(node.val) # visit operation
in_order_traversal(node.right, visit_sequence)
def pre_order_traversal(node, visit_sequence):
if node is not None:
visit_sequence.append(node.val) # visit operation
pre_order_traversal(node.left, visit_sequence)
pre_order_traversal(node.right, visit_sequence)
def post_order_traversal(node, visit_sequence):
if node is not None:
post_order_traversal(node.left, visit_sequence)
post_order_traversal(node.right, visit_sequence)
visit_sequence.append(node.val) # visit operation
class Node: # general graph node
def __init__(self, val, children=()):
self.val = val
self.children = children
self.visited = False
def dfs(root, visit_sequence):
if root is None:
return None
visit_sequence.append(root.val) # visit operation
for node in root.children:
dfs(node, visit_sequence)
def bfs(root, visit_sequence):
if root is None:
return None
frontier = queue.Queue()
frontier.put(root)
while not frontier.empty():
node = frontier.get()
visit_sequence.append(node.val) # visit operation
for child in node.children:
if not child.visited:
child.visited = True
frontier.put(child)
def stringify_bin_tree(root):
frontier = [root]
next_frontier = []
display_str = ''
while len(frontier) > 0:
for node in frontier:
if node is not None:
display_str += str(node.val) + ','
next_frontier += [node.left, node.right]
else:
display_str += '_,'
display_str += '\n'
frontier = next_frontier
next_frontier = []
return display_str
def stringify_list_of_linked_lists(linked_lists):
display_str = ''
for ll in linked_lists:
elem = ll[0]
while elem is not None:
display_str += str(elem.val) + ','
elem = elem.next_elem
display_str += '\n'
return display_str