-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameTree.py
More file actions
167 lines (138 loc) · 4.59 KB
/
GameTree.py
File metadata and controls
167 lines (138 loc) · 4.59 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#
# Author: Nathaniel Le Sage 6/4/2017
#
class Node(object):
def __init__(self, element=None, # the board layout
parent=None,
children=[],
hasChildren=False,
numChildren=0,
depth=0,
util=[0,0,0,0,0,0,0,0,0]): # utility of the next move
self.element = element # generic value associated with the node
self.parent = parent
self.children = children
self.hasChildren = hasChildren
self.numChildren = numChildren
self.depth = depth
self.util=[0,0,0,0,0,0,0,0,0]
def setChild(self, c): # adds a child
if type(c) == Node:
if self.children == []: # stops new object from initializing with child array from parent
reset = [] # if you don't have this logic, an infinite loop occurs
self.children = reset
self.children.append(c)
c.depth = self.depth + 1
self.updateNumChildren(1)
if c.parent == None:
c.parent = self
else:
print("node already has parent!")
self.hasChildren = True
else:
print("Not a node!")
def Children(self):
return self.children
def Element(self):
return self.element
def HasChildren(self):
return self.hasChildren
def remove(self): # removes node and all children of node
for c in self.children:
if c.hasChildren == True:
c.remove()
else:
c.updateNumChildren(-1)
del c # does nothing
self.children = []
gc.collect()
def updateNumChildren(self, p): # keeps number of children updated
self.numChildren += p
def DFS(self, board): # pre-order DFS
t = []
if self.hasChildren == False: # end of tree
return [self]
else:
t.append(self) # add node before children have been visited
for c in self.children:
t.extend(c.PreOrderTraversal()) # recursively visit each child
return t
def BFS(self, d=1): # breadth-first traversal
t = []
if self.isRoot() == True: # root is first in list
t = [self]
for c in self.children: # add children
t.append(c)
for c in t: # for children of children
if c.depth != 0: # end of recursive depth
t.extend(c.BFS(d+1)) # add next nodes to list with +1 level of depth
return t
def isRoot(self): # sees if the node is the root node
if self.depth==0:
return True
else:
return False
def isLeaf(self):
for i in range(len(self.Children())):
if self.Children()[i].element != None:
return False
return True
def __repr__(self):
return str(self)
def __str__(self):
r = ""
s = str("element: " + str(self.element))
if self.parent == None:
r = " root"
return s + r + " children: " +str(self.numChildren) + " depth: " +str(self.depth)
class BinaryNode(Node):
def setChild(self, c):
if self.children == []: # stops new object from initializing with child array from parent
reset = []
self.children = reset
if len(self.children) < 2:
self.children.append(c)
else:
print("Parent cannot have more than two children")
c.parent=self
c.depth = self.depth + 1
self.updateNumChildren(1)
if c.getParent() == None:
c.parent = self
else:
print("node already has parent!")
self.hasChildren = True
def printTrace(l):
for node in l:
print(str(node))
def Demo():
print("Pre/in/postorder and BFS traversals.")
a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)
f = Node(6)
g = Node(7)
a.setChild(b)
a.setChild(c)
b.setChild(d)
b.setChild(e)
c.setChild(f)
c.setChild(g)
print("---")
print("Pre-order traversal:")
printTrace(a.PreOrderTraversal())
print("---")
print("---")
print("In-order traversal:")
printTrace(a.InOrderTraversal())
print("---")
print("---")
print("Post-order traversal:")
printTrace(a.PostOrderTraversal())
print("---")
print("---")
print("Breadth-first traversal:")
printTrace(a.BFS())
print("---")