4 # Purpose: cython unbalanced binary tree module
6 # Copyright (c) 2010-2013 by Manfred Moitzi
11 from cwalker
import cWalker
13 from cwalker cimport
*
20 def __cinit__(self
, items
=None):
26 def __dealloc__(self
):
27 ct_delete_tree(self
._root
)
33 def __getstate__(self
):
34 return dict(self
.items())
36 def __setstate__(self
, state
):
40 ct_delete_tree(self
._root
)
44 def get_value(self
, key
):
45 result
= <object> ct_get_item(self
._root
, key
)
54 walker
.set_tree(self
._root
)
57 def insert(self
, key
, value
):
58 res
= rb_insert(&self
._root
, key
, value
)
60 raise MemoryError('Can not allocate memory for node structure.')
64 def remove(self
, key
):
66 result
= rb_remove(&self
._root
, key
)
68 raise KeyError(str(key
))
73 """ Get item with max key of tree, raises ValueError if tree is empty. """
75 node
= ct_max_node(self
._root
)
76 if node
== NULL
: # root is None
77 raise ValueError("Tree is empty")
78 return (<object>node
.key
, <object>node
.value
)
81 """ Get item with min key of tree, raises ValueError if tree is empty. """
83 node
= ct_min_node(self
._root
)
84 if node
== NULL
: # root is None
85 raise ValueError("Tree is empty")
86 return (<object>node
.key
, <object>node
.value
)