-
Notifications
You must be signed in to change notification settings - Fork 1
/
CNode.py
89 lines (66 loc) · 2.29 KB
/
CNode.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
# -*- coding: utf-8 -*-
from Queue import Queue
class CNode(object):
"""Holds a node in our Calendar"""
def __init__(self,name="",content=None,attr=None):
self.name = name
self.content = content
if attr:
self.attr = attr
else:
self.attr = {}
self.children = []
def add_child(self,child):
if not self.children.__contains__(child):
self.children.append(child)
child.parent = self
def __str__(self):
return "{%s [%s]: %s %s}" %(self.name,
str(self.attr),
str(self.content),
[str(c) for c in self.children])
def delete_child(self,name):
"""Delete the first child with name `name'
returns True if a child was deleted
"""
for i,c in zip(range(len(self.children)), self.children):
if c.name == name:
self.children.pop(i)
return True
return False
def search(self,name,all=False,depth=None,keep_depth=False):
"""Breadth-first search for one or more elements
all: if True, find all matching items
depth: how far to search for items (None means infinite depth)
keep_depth: whenever we reach an element, don't search *deeper* in
the tree for additional elements (only relevant when all=True)
"""
q = Queue()
results = []
q.put(self)
if depth == None: depth = -2
this_level = 1
next_level = 0
while depth != -1 and not q.empty():
if this_level == 0:
depth -= 1
this_level = next_level
next_level = 0
# TODO: check if this is off by one
e = q.get()
this_level -= 1
if e.name == name:
if keep_depth:
# This is the last level
depth = 0
if not all:
return e
else:
results.append(e)
for child in e.children:
q.put(child)
next_level += 1
if all:
return results
else:
return None