10.07.2015 Views

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

- 131 -<strong>to</strong> place the Nth item, skip N-1 itemsThus, in <strong>to</strong>tal it is necessary <strong>to</strong> skip:1+...(N-2)+(N-1) = N*N/2items. For example, for a worst case sort of 1000 items it is necessary <strong>to</strong> skip 500000 items.Remember, for searching and sorting in a linear list, each skip involves a comparison between a list item and arequired or new item. If the items are strings then comparison is character by character so the number of comparisonscan get pretty big for relatively short lists.Note that we have been considering naive linear list algorithms. For particular problems, if there is a known orderingon a sequence of values then it may be possible <strong>to</strong> represent the sequence as an ordered list of ordered sub-sequences.For example, a sequence of strings might be represented as list of ordered sub-lists, with a sub-list for each letter of thealphabet.7.9. TreesTrees are general purpose nested structures which enable far faster access <strong>to</strong> ordered sequences than linear lists. Herewe are going <strong>to</strong> look at how trees may be modelled using lists. To begin with, we will introduce the standard treeterminology.A tree is a nested data structure consisting of a hierarchy of nodes. Each node holds one data item and has branches<strong>to</strong> sub-trees which are in turn composed of nodes. The first node in a tree is called the root. A node with emptybranches is called a leaf. Often, a tree has the same number of branches in each node. If there are N branches then thetree is said <strong>to</strong> be N-ary.If there is an ordering relationship on the tree then each sub-tree consists of nodes whose items have a commonrelationship <strong>to</strong> the original node’s item. Note that ordering implies that the node items are all the same type. Orderedlinear sequences can be held in a tree structure which enables far faster access and update.We are now going <strong>to</strong> look specifically at binary trees. A binary tree node has two branches, called the left and rightbranches, <strong>to</strong> binary sub-trees. Formally, the empty tree, which we will denote as EMPTY, is a binary tree:EMPTY is a binary treeand a tree consisting of a node with an item and two sub-trees is a binary tree if the sub-trees are binary trees:NODE ITEM L R is a binary treeif L is a binary tree and R is a binary treeWe will model a binary tree using lists. We will represent EMPTY as NIL:def EMPTY = NILdef ISEMPTY = ISNILand a node as a list of the item and the left and right branches:def NODE ITEM L R = [ITEM,L,R]The item and the sub-trees may be selected from nodes:ITEM (NODE I L R) = ILEFT (NODE I L R) = LRIGHT (NODE I L R) = R

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!