-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.py
73 lines (61 loc) · 1.7 KB
/
Tree.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
import weakref
class Tree(object):
def __init__(self, data = None, children=None):
self.setup(data, children)
def setup(self, data, children = None):
self.data = data
self.parent = None
self.children = []
for ci, child in enumerate(self.children):
child.parent = weakref.ref(self)
child.order = ci
self.i = -1
self.j = -1
def addChild(self, child):
"""
Add a child subtree to the tree
"""
child.parent = weakref.ref(self)
child.order = len(self.children)
self.children.append(child)
def getParent(self):
return self.parent()
def isTerminal(self):
return len(self.children) == 0
def depth(self,d = 0):
maxDepth = d
for child in self.children:
childDepth = child.depth(d+1)
if childDepth+1 > maxDepth:
maxDepth = childDepth
return maxDepth
def bottomup(self, returnOnlySelf = False):
for child in self.children:
for node in child.bottomup():
yield node
yield self
def detach(self):
if self.parent():
self.parent().delete_child(self.order)
def delete_child(self, i):
self.children[i].parent = None
self.children[i].order = 0
self.children[i:i+1] = []
for j in range(i,len(self.children)):
self.children[j].order = j
def insert_child(self, i, child):
child.parent = weakref.ref(self)
if i != -1:
self.children[i:i] = [child]
else:
self.children[len(self.children):]=[child]
for j in range(i,len(self.children)):
self.children[j].order = j
def frontier(self):
if len(self.children) != 0:
l = []
for child in self.children:
l.extend(child.frontier())
return l
else:
return [self]