From 6ad263d712312d19da15da3fe3a61b101d71877b Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Tue, 9 Nov 2010 23:22:52 -0500 Subject: [PATCH] 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. --- libsaria/trees.py | 24 ++++++++++++++++++++++++ tests/trees.py | 21 ++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/libsaria/trees.py b/libsaria/trees.py index a4ae1f0f..02d0bc11 100644 --- a/libsaria/trees.py +++ b/libsaria/trees.py @@ -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): diff --git a/tests/trees.py b/tests/trees.py index 935d040e..84361d52 100644 --- a/tests/trees.py +++ b/tests/trees.py @@ -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()