-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericTrees.py
More file actions
35 lines (30 loc) · 893 Bytes
/
Copy pathGenericTrees.py
File metadata and controls
35 lines (30 loc) · 893 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
class Node:
def __init__(self,data=None,next=None):
self.data =data
self.firstChild = None
self.nextSibling = None
class GenericTree:
def __init__(self,parent,value=None):
self.parent = parent
self.value = value
self.childList = []
if parent is None:
self.birthOrder = 0
else:
self.birthOrder = len(parent.childList)
parent.childList.append(self)
def nChildren(self):
return len(self.childList)
def nthChild(self,n):
return self.childList[n]
def fullPath(self):
result = []
parent = self.parent
kid =self
while parent:
result.insert(0,kid.birthOrder)
parent,kid = parent.parent,parent
return result
def nodeId(self):
fullpath = self.fullPath()
return NodeId(fullpath)