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:
Bryan Schumaker 2010-11-09 23:22:52 -05:00
parent 946e6b19da
commit 6ad263d712
2 changed files with 40 additions and 5 deletions

View File

@ -47,6 +47,30 @@ class Tree(dict):
if len(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):
def __init__(self):

View File

@ -7,11 +7,22 @@ from libsaria import trees
#tree.insert(["home", "bjschuma", "Videos"])
#tree.insert(["home", "other", "Music"])
from libsaria import path
tree = path.make_tree("/home/bjschuma/Music")
#from libsaria import path
#tree = path.make_tree("/home/bjschuma/Music")
#print tree
print tree.value
#print tree.value
#tree.disp()
for path in tree.walk():
print path
#for path in tree.walk():
# 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()