Value Trees
Value trees store a value in addition to the path. They come in normal and doubly-linked flavors.
This commit is contained in:
parent
f29c57992a
commit
fe449ffea4
|
@ -47,6 +47,7 @@ class Tree(dict):
|
|||
if len(path) > 1:
|
||||
child.insert(path[1:])
|
||||
|
||||
|
||||
class DLTree(Tree):
|
||||
def __init__(self):
|
||||
Tree.__init__(self)
|
||||
|
@ -107,8 +108,43 @@ class DLFSTree(DLTree, FSTree):
|
|||
def insert_path_split(self, base, file = None):
|
||||
if file == None:
|
||||
child = self.insert(base)
|
||||
#return DLTree.insert(self, base)
|
||||
else:
|
||||
child = self.insert(base + [file])
|
||||
#return DLTree.insert(self, base + [file])
|
||||
return child
|
||||
|
||||
|
||||
class ValTree(Tree):
|
||||
def __init__(self):
|
||||
Tree.__init__(self)
|
||||
self.value = None
|
||||
|
||||
def __disp__(self, child, level):
|
||||
return "%s (%s)", (child, self[child].value)
|
||||
|
||||
def insert(self, path, values):
|
||||
child = self.__insert__(path)
|
||||
if len(values) > 0:
|
||||
child.value = values[0]
|
||||
vals = values[1:]
|
||||
else:
|
||||
vals = values
|
||||
if len(path) > 1:
|
||||
child.insert(path[1:], values[1:])
|
||||
|
||||
|
||||
class DLValTree(DLTree, ValTree):
|
||||
def __init__(self):
|
||||
DLTree.__init__(self)
|
||||
ValTree.__init__(self)
|
||||
|
||||
def insert(self, path, values):
|
||||
child = self.__insert__(path)
|
||||
child.parent = self
|
||||
if len(values) > 0:
|
||||
child.value = values[0]
|
||||
vals = values[1:]
|
||||
else:
|
||||
vals = values
|
||||
if len(path) > 1:
|
||||
return child.insert(path[1:], vals)
|
||||
return child
|
||||
|
|
|
@ -27,15 +27,32 @@ from libsaria import trees
|
|||
#print p.walk_backwards()
|
||||
|
||||
|
||||
tree = trees.DLFSTree()
|
||||
n = tree.insert_path("/a/b/c/d")
|
||||
o = tree.insert_path("/a/b/c/e")
|
||||
p = tree.insert_path("/f/g/h/i")
|
||||
#tree = trees.DLFSTree()
|
||||
#n = tree.insert_path("/a/b/c/d")
|
||||
#o = tree.insert_path("/a/b/c/e")
|
||||
#p = tree.insert_path("/f/g/h/i")
|
||||
#
|
||||
#for path in tree.walk_paths():
|
||||
# print path
|
||||
#
|
||||
#print n.walk_path_backwards()
|
||||
#print o.walk_path_backwards()
|
||||
#print p.walk_path_backwards()
|
||||
|
||||
for path in tree.walk_paths():
|
||||
print path
|
||||
|
||||
print n.walk_path_backwards()
|
||||
print o.walk_path_backwards()
|
||||
print p.walk_path_backwards()
|
||||
#tree = trees.ValTree()
|
||||
#tree.insert(["a", "b", "c", "d"], [1, 2, 3, 4])
|
||||
#tree.insert(["a", "b", "c", "e"], [1, 2, 3, 5])
|
||||
#tree.insert(["f", "g", "h", "i"], [6, 7, 8, 9])
|
||||
#tree.disp()
|
||||
|
||||
|
||||
tree = trees.DLValTree()
|
||||
n = tree.insert(["a", "b", "c", "d"], [1, 2, 3, 4])
|
||||
o = tree.insert(["a", "b", "c", "e"], [1, 2, 3, 5])
|
||||
p = tree.insert(["f", "g", "h", "i"], [6, 7, 8, 9])
|
||||
tree.disp()
|
||||
|
||||
print n.walk_backwards()
|
||||
print o.walk_backwards()
|
||||
print p.walk_backwards()
|
||||
|
|
Loading…
Reference in New Issue