Session 7: Trees

Trees

We only consider binary trees here. You can find the definition of a binary tree in Bratko, Section 9.2, p. 204.

Efficiently searching sorted lists

Searching for an element in a list can be done by using the member/2 predicate. If you know that the given list is sorted, however, searching can be done more efficiently.

Binary dictionaries

We could search very efficiently if we could immediately check the median of our list (if this is not the element we are looking for, decide in which half of the list we have to search further, take the median of that half, etc). This is the idea behind binary dictionaries as defined in Bratko (Section 9.2, p.205). In Bratko you can find definitions of predicates to search, insert and delete elements in such a binary dictionary.

Balanced Binary dictionaries

A balanced tree has the following property: for each each node in the tree it is the case that the depth of the left subtree differs at most one from the depth of the right subtree. Being balanced is an important property for a ordered tree (a binary dictionary): it ensures access to an element in log(n) time, where n is the number of elements in the tree (see Bratko p.207).