-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
50 lines (34 loc) · 821 Bytes
/
tree.py
File metadata and controls
50 lines (34 loc) · 821 Bytes
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
class node():
"""
Class to represent nodes in a binary tree
1 parents and up to 2 children
"""
# Value of node
value = ""
# Parent variable
parent = ""
# List of Children
children = []
def __init__(self, value = 0):
"""
Init new node with a value
"""
self.value = value
def attach_child(self, child):
"""
Input a single node object to attach to this node as child
No output
"""
self.children.append(child)
child.attach_parent(self)
def attach_parent(self, parent):
"""
Input node object to become parent to this node
No output
"""
self.parent = parent
n1 = node(5)
n2 = node(6)
print(n2.parent)
n1.attach_child(n2)
print (n2.parent)