4.1 Search
We will now describe how to find the closest match
of a set of points X in our tree. Starting from the root
of the tree we follow the path of nodes as dictated by
comparing the similarities of X with each node’s T
L
and T
R
until we reach a leaf. Then we report the tem-
plate of that leaf as a possible result. Since we replace
the constant time operations of a binary search tree
with operations that also require constant time (with
respect to the number of nodes n), this can be done in
O(logn) time as per the binary search tree bibliogra-
phy.
Due to the non-strict nature of the Hausdorff dis-
tance and therefore our similarity measure too, we
cannot give any guarantees that the first result of a
search is the best one. To overcome this we note the
confidence of each node in the path to the previous
result and we backtrack through the path and reverse
the decision of the node with the lowest confidence
and proceed to search the subtree we skipped in the
previous search. Once a nodes decision has been re-
versed, we set it’s confidence to 1, so that it won’t
switch again until the search is over.
This way, if we allow r tries, we come up with r
template candidates. We determine the best match
by exhaustive search between these candidates. This
takes us O(rlogn) time to do.
Regarding the values of nodes’ confidence along
the path to a leaf, what we expect is that the confi-
dence will be lower toward the end of the path (be-
cause the templates with a low least common ances-
tor will be similar) and toward the root of the path
(because there will be a lot of templates to separate in
each subtree). It would be a good idea to replace the
confidence by a function of |sim(X,T
L
) −sim(X,T
R
)|
and the depth of a node, however we find that it is
more practical to artificially restrict switching paths
at the higher levels in the first few tries.
4.2 Insertion
To insert a new node q
n+1
with a template T
n+1
into
the tree we start by searching for T
n+1
in the current
tree. If we come to an internal node with only 1 child
during our search, we add the new node as it’s other
child. If the search stops at a leaf node q
i
, we replace
it with an internal node and add q
n+1
and q
i
as the new
internal node’s children. The template T
n+1
is also
added to the proper sum matrix (S
L
or S
R
) of every
node it traverses.
This means that the new template will be inserted
near similar templates and guarantees that if we
search for the template again, the search will find it
in the first try (provided no tree rotations have been
performed since it’s insertion).
After inserting a node, we then follow the reverse
path to the root, balancing and retraining every af-
fected node. Again, the changes we propose involve
constant time operations (node training is indepe-
dent from the number of nodes n), so insertion takes
O(logn) time as a property of binary search trees.
4.3 Deletion
Our tree only supports the deletion of leaf nodes.
Moreover, we feel that the task of deleting a node
based on an input shape is not well defined. Trying
to delete a template we have not yet stored by search-
ing for it first will result in the deletion of the template
that is the closest match for it, something that is prob-
ably not what we wanted. Even if the template exists
in the tree, the search operation is not guaranteed to
find it.
In this section we will describe the deletion of a
leaf whose location must be known beforehand. De-
termining which leaf we want to delete is subject to
the deletion policy we wish to enforce (and using ad-
ditional data structures). For example, if we want to
delete the oldest template at a time we can maintain a
queue of pointers to the templates in the order they are
inserted into the tree. If we want to delete nodes on
a least recently used basis, we will probably need to
maintain a minimum-heap data structure for the use
of the nodes. While there is nothing preventing the
deletion of a search result, we must note that doing so
is unadvisable.
Starting from the node q
j
which want to delete, we
travel backwards to the root via parent node pointers.
The reverse path is what we need to proceed with the
deletion as per normal binary search trees. We sub-
tract the nodes template T
j
from the proper sum (S
L
or
S
R
) of each node traversed and rebalance where nec-
essary.
Deleting nodes and rebalancing can result in an in-
ternal node with no children. Since we do not allow
that, we check whether an internal node is left child-
less, mark that node for deletion and repeat the pro-
cess again.