-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdata_structs.py
178 lines (130 loc) · 3.44 KB
/
data_structs.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
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
168
169
170
171
172
173
174
175
176
177
from __future__ import print_function
import heapq
__author__ = 'Biney Kingsley'
'''
This class implements basic data structure classes which is used to implemented the various search algorithms
'''
# Stacks class
class Stacks:
def __init__(self):
self.stackList = []
def clear(self):
self.__init__()
def push(self, data):
self.stackList.append(data)
def pop(self):
if self.isEmpty():
return
return self.stackList.pop()
def top(self):
if self.isEmpty():
return
return self.stackList[-1]
def isEmpty(self):
if self.size() == 0:
return True
def size(self):
return len(self.stackList)
# Queues class
class Queues:
def __init__(self):
self.queueList = []
def clear(self):
self.__init__()
def enqueue(self, data):
self.queueList.append(data)
def get_last_element(self):
return self.queueList[-1]
def dequeue(self):
item = self.queueList[0]
self.queueList.remove(item)
return item
def isEmpty(self):
if self.size() == 0:
return True
def showStructure(self):
for x in self.queueList:
print(x, end='\t')
def size(self):
return len(self.queueList)
class PriorityQueue:
def __init__(self):
self.elements = []
def empty(self):
return len(self.elements) == 0
def put(self, item, priority):
heapq.heappush(self.elements, (priority, item))
def get(self):
return heapq.heappop(self.elements)[1]
# Node Class
class Node:
def __init__(self, key=None, x_coord=None, y_coord=None):
'''
Arguments:
key: goal state
x_coord: x coordinates
y_coord: y coordinates
'''
self.child = []
self.right = None
self.left = None
self.Queues = Queues()
self.stack = Stacks()
self.key = key
self.cost = 0
self.x = x_coord
self.y = y_coord
self.parent = None
def set_x(self, x_coord=None):
self.x = x_coord
def get_x(self):
return self.x
def set_y(self, y_coord=None):
self.y = y_coord
def get_y(self):
return self.y
def set_cost(self, cost=None):
self.cost = cost
def get_cost(self):
return self.cost
def setRight(self, Node=None):
self.right = Node
def setLeft(self, Node=None):
self.left = Node
def setKey(self, Node=None):
self.key = Node
def getRight(self):
return self.right
def getLeft(self):
return self.left
def getKey(self):
return self.key
def hasLeft(self):
status = False
if self.left is not None:
status = True
else:
status = False
return status
def hasRight(self):
status = False
if self.right is not None:
status = True
else:
status = False
return status
def add_child(self, Node=None):
self.child.append(Node)
def first_child(self):
return self.child.pop(0)
def has_child(self):
state = False
if len(self.child) == 0:
state = False
else:
state = True
return state
def set_parent(self, Node=None):
self.parent = Node
def get_parent(self):
return self.parent