Address-Of operator equivalent, PAVL search tree

castironpi castironpi at gmail.com
Fri Aug 22 00:56:44 EDT 2008


On Aug 21, 11:03 pm, castironpi <castiro... at gmail.com> wrote:
> Hi,
>
> I am translating the GNU library's PAVL search tree implementation
> into Python.  I can't use it directly because a delete function I need
> uses a different method of finding the node to delete.
>
> It contains this line:
>
>       q = (struct pavl_node *) &tree->pavl_root;
>
> line 276 in http://www.sfr-fresh.com/unix/misc/avl-2.0.3.tar.gz:a/avl-2.0.3/pavl.c

It seems the only property that's accessed is q->pavl_link[ 0 ], done
in order to generically set the tree's root along with other nodes.  I
will use a proxy class to reroute set operations on q.link[ 0 ] to
tree.root.  It is also accessible via q.left.  Here is a tentative
implementation:

class RootProxy:
    __slots__= '_tree'
    class RootLink:
        __slots__= '_tree'
        def __init__( self, tree ):
            self._tree= tree
        def __getitem__( self, key ):
            assert key== 0
            return self._tree.root
        def __setitem__( self, key, val ):
            assert key== 0
            self._tree.root= val
    def __init__( self, tree ):
        self._tree= tree
    def _getleft( self ):
        return self._tree.root
    def _setleft( self, val ):
        self._tree.root= val
    left= property( _getleft, _setleft )
    def _getlink( self ):
        return self.RootLink( self._tree )
    link= property( _getlink )

The line then becomes:
>       q = (struct pavl_node *) &tree->pavl_root;
    q= RootProxy( tree )

Thanks for your attention.  Any questions or comments please share.



More information about the Python-list mailing list