-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_binary_tree.py
More file actions
68 lines (56 loc) · 2.04 KB
/
Copy pathvalid_binary_tree.py
File metadata and controls
68 lines (56 loc) · 2.04 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
"""
Given a tuple where the first element is the parent and the second element is the child. Check if it is a valid
binary tree
"""
'''
A tree is a valid binary tree iff
1. There is only one root
2. If each node has a single parent(1 indegree)
3. Each node has no more than 2 children
4. There is no cycle
'''
class Tree:
def __init__(self, tuples):
self.in_degrees = {}
self.adj_list = {}
self._build_tree(tuples)
def _build_tree(self, tuples):
for parent, child in tuples:
if parent not in self.adj_list:
self.adj_list[parent] = []
if parent not in self.in_degrees:
self.in_degrees[parent] = 0
if child not in self.in_degrees:
self.in_degrees[child] = 0
self.adj_list[parent].append(child)
self.in_degrees[child] += 1
def has_one_root(self):
root = [node for node, value in self.in_degrees.items() if value == 0]
return len(root) == 1
def has_two_children(self):
for node in self.adj_list:
children = self.adj_list[node]
if len(children) > 2:
return False
return True
def has_one_parent(self):
for node in self.in_degrees:
if self.in_degrees[node] > 1:
return False
return True
def has_a_cycle(self):
sources = [node for node, value in self.in_degrees.items() if value == 0]
top_sort = []
while sources:
current_source = sources.pop()
top_sort.append(current_source)
for child in self.adj_list[current_source]:
self.in_degrees[child] -= 1
if self.in_degrees[child] == 0:
sources.append(child)
return len(top_sort) == len(self.in_degrees)
def is_valid(self):
return self.has_one_root() and self.has_two_children() and self.has_one_parent() and not self.has_a_cycle()
if __name__ == '__main__':
tree = Tree([(1, 2), (2, 3), (3, 4)])
print(tree.is_valid())