Add DLTree()
This patch adds a double-linked tree class for use in storing the library. This class can walk backwards from a leaf node when walk_backwards is called.
This commit is contained in:
parent
4a33a2f0fd
commit
825a8b8346
|
@ -47,6 +47,30 @@ class Tree(dict):
|
||||||
if len(path) > 1:
|
if len(path) > 1:
|
||||||
child.insert(path[1:])
|
child.insert(path[1:])
|
||||||
|
|
||||||
|
class DLTree(Tree):
|
||||||
|
def __init__(self):
|
||||||
|
Tree.__init__(self)
|
||||||
|
self.parent = None
|
||||||
|
|
||||||
|
def walk_backwards(self, child = None):
|
||||||
|
key = None
|
||||||
|
if child != None:
|
||||||
|
key = [item[0] for item in self.items() if id(item[1]) == id(child)]
|
||||||
|
if self.parent == None:
|
||||||
|
return key
|
||||||
|
ret = self.parent.walk_backwards(self)
|
||||||
|
if key:
|
||||||
|
return ret + key
|
||||||
|
else:
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def insert(self, path):
|
||||||
|
child = self.__insert__(path)
|
||||||
|
child.parent = self
|
||||||
|
if len(path) > 1:
|
||||||
|
return child.insert(path[1:])
|
||||||
|
return child
|
||||||
|
|
||||||
|
|
||||||
class FSTree(Tree):
|
class FSTree(Tree):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
|
@ -7,11 +7,22 @@ from libsaria import trees
|
||||||
#tree.insert(["home", "bjschuma", "Videos"])
|
#tree.insert(["home", "bjschuma", "Videos"])
|
||||||
#tree.insert(["home", "other", "Music"])
|
#tree.insert(["home", "other", "Music"])
|
||||||
|
|
||||||
from libsaria import path
|
#from libsaria import path
|
||||||
tree = path.make_tree("/home/bjschuma/Music")
|
#tree = path.make_tree("/home/bjschuma/Music")
|
||||||
#print tree
|
#print tree
|
||||||
|
|
||||||
print tree.value
|
#print tree.value
|
||||||
#tree.disp()
|
#tree.disp()
|
||||||
for path in tree.walk():
|
#for path in tree.walk():
|
||||||
print path
|
# print path
|
||||||
|
|
||||||
|
|
||||||
|
tree = trees.DLTree()
|
||||||
|
|
||||||
|
n = tree.insert(["a", "b", "c", "d"])
|
||||||
|
o = tree.insert(["a", "b", "c", "e"])
|
||||||
|
p = tree.insert(["f", "g", "h", "i"])
|
||||||
|
tree.disp()
|
||||||
|
print n.walk_backwards()
|
||||||
|
print o.walk_backwards()
|
||||||
|
print p.walk_backwards()
|
||||||
|
|
Loading…
Reference in New Issue