forked from alanwoj16/ps2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
61 lines (46 loc) · 1.38 KB
/
Copy pathnode.py
File metadata and controls
61 lines (46 loc) · 1.38 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
class Node:
def __init__(self):
self.label = None
self.children = {}
self.name = None
self.mode = None
def get_label(self):
return self.label
def get_name(self):
return self.name
def get_children(self):
return self.children
def get_mode(self):
return self.mode
def add_child(self, node, value):
self.children[value] = node
return self
def remove_descendant(self, node):
# First, search our direct children for the node
for child in self.children:
if self.children[child]==node:
self.children.pop(child)
return True
# The node is not one of our children. Ask each of them in turn to do it.
for child in self.children:
if self.children[child].remove_descendant(node):
return True
# The node was not found in our tree
return False
def print_node(self):
print "New Node"
print "Node label: ", self.label, " name: ", self.name
print "Number of children: ", len(self.children)
for key in self.children.keys():
print "Key: " + str(key)
print "Name: " + str(self.children[key].name)
print "Label: " + str(self.children[key].label)
self.children[key].print_node()
#test cases for add_child
#node1 = Node()
#node1.name = "PAtt"
#node2 = Node()
#node2.name = "CAtt"
#node1.add_child(node2,'y')
#print node1.get_children()
#print node1.get_children()['y'].name