-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
231 lines (181 loc) · 5.96 KB
/
Copy pathtree.py
File metadata and controls
231 lines (181 loc) · 5.96 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from queue import Queue
import json
class TreeNode():
name = None
data = None
parent = None
def __init__(self, name) -> None:
self.name = name
self.children = []
def toDict(self):
if self.parent != None:
nodeDict = {
"name":self.name,
"data":self.data,
"parent":self.parent.name,
"children":[child.toDict() for child in self.children]
}
else:
nodeDict = {
"name":self.name,
"data":self.data,
"parent":None,
"children":[child.toDict() for child in self.children]
}
return nodeDict
#basic tree structure for practice
class Tree():
root = None
current = None
depth = 0
height = 0
def __init__(self, treeNode:object) -> None:
if self.root == None:
self.root = treeNode
self.current = self.root
def printCurrentChildren(self):
for child in self.current.children:
print(child.name)
def addChildToCurrent(self, leaf:object) -> None:
leaf.parent = self.current
self.current.children.append(leaf)
# print("Appended", leaf.name, "to", self.current.name)
def findNode(self, node, current = None) -> object:
if self.current == node:
return None
if current == None:
current = self.root
if current == node:
# print(f"{node.name}, found")
self.current = current
return
elif len(current.children) == 0:
return None
else:
for child in current.children:
self.findNode(node, child)
def findNodeDFS(self, target):
visited = []
current = self.root
while current:
visited.append(current)
if current == target:
return current
else:
lastCurrent = current
for child in current.children:
if child not in visited:
current = child
break
if lastCurrent == current:
current = current.parent
def gptDFS(self, target):
#gpt suggested corrections to my dfs algorithm
visited = set()
stack = [self.root]
while stack:
current = stack.pop()
#print(current.name)
if current.name == target.name:
return current
visited.add(current)
for child in current.children:
if child not in visited:
stack.append(child)
return None
def insertNode(self, parentNode: object, child:object):
self.findNode(parentNode)
self.addChildToCurrent(child)
def traverseTree(self):
if len(self.current.children) == 0:
return self.current
else:
for child in self.current.children:
self.current = child
self.traverseTree()
def printLevelOrderTraversal(self):
q = Queue()
q.put(self.root)
while not q.empty():
levelSize = q.qsize()
for _ in range(levelSize):
node = q.get()
print(node.name, end = " ")
for child in node.children:
q.put(child)
print()
def printRealFileStructure(self, desiredStart = None, indentation = 0):
if desiredStart == None:
start = self.root
else:
start = desiredStart
if indentation == 0:
print(start.name)
else:
print(" " * indentation + "└─" + start.name)
indentation += 3
if start.children:
for child in start.children:
self.printRealFileStructure(child, indentation)
else:
return None
def deleteNode(self, desiredNode):
self.findNode(desiredNode)
if len(self.current.children) > 0:
print(f"Unable to delete {desiredNode.name} since it has children")
else:
self.current = desiredNode.parent
for i, child in enumerate(self.current.children):
if child.name == desiredNode.name:
print("deleting child from parent")
self.current.children.pop(i)
def getTreeDimensions(self, current = None):
if current == None:
current = self.root
if len(current.children) == 0:
return 1
else:
self.depth += 1
for child in current.children:
result = self.getTreeDimensions(child)
if result != None:
self.height += result
def main():
base = TreeNode("base")
tree = Tree(base)
node1 = TreeNode("node1")
node2 = TreeNode("node2")
node3 = TreeNode("node3")
node4 = TreeNode("node4")
node5 = TreeNode("node5")
node6 = TreeNode("node6")
node7 = TreeNode("node7")
tree.insertNode(base,node1)
tree.insertNode(base,node2)
tree.insertNode(node1,node3)
tree.insertNode(node1,node4)
tree.insertNode(node2,node5)
tree.insertNode(node2,node6)
# tree.deleteNode(node2)
# tree.deleteNode(node6)
# tree.getTreeDimensions()
# print(tree.depth)
# print(tree.height)
# rootDict = base.toDict()
# print(rootDict)
# jsonString = json.dumps(rootDict, indent=2)
# with open("tree.json", "w") as jsonFile:
# jsonFile.write(jsonString)
# result = tree.findNodeDFS(node6)
# print(result)
result = tree.gptDFS(node7)
print(result)
#tree.printCurrentChildren()
# tree.printLevelOrderTraversal()
# tree.printFileStructure()
# tree.printRealFileStructure()
#tree.insertNode(node1,node2)
#tree.findNode(node2)
#tree.printTree()
if __name__ == "__main__":
main()