0% found this document useful (0 votes)
11 views4 pages

Laboratory 8 FLT (Eng)

Laboratory 8 focuses on creating a lexico-syntactic analyzer for processing binary search trees in Haskell and ML. It includes operations like inserting nodes and counting nodes, with examples provided for both syntaxes. Additionally, it proposes exercises for searching, deleting nodes, and checking if a binary tree is balanced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Laboratory 8 FLT (Eng)

Laboratory 8 focuses on creating a lexico-syntactic analyzer for processing binary search trees in Haskell and ML. It includes operations like inserting nodes and counting nodes, with examples provided for both syntaxes. Additionally, it proposes exercises for searching, deleting nodes, and checking if a binary tree is balanced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Laboratory 8

Interpreting binary trees in Haskell/ML

The eighth laboratory consists of creating a lexico-syntactic analyzer to process binary search
trees in Haskell syntax, and comparatively, in ML syntax. The ML part will appear on a light blue
background. For the implementation we will choose one of the two syntaxes. We will consider
reading the trees and executing some operations on them: insert (inserting a node in a binary
tree) and count (counting the nodes of a tree, different from empty leaves Lf).
Examples of input in Haskell:

Node Lf 2 Lf
> Node Lf 2 Lf

Node (Node Lf 2 Lf) 10 (Node Lf 12 Lf)


> Node (Node Lf 2 Lf) 10 (Node Lf 12 Lf)

count (insert 16 (Node (Node Lf 2 Lf) 15 Lf))


>3

insert (count (Node Lf 17 (Node Lf 24 Lf))) (insert 12 (Node Lf 10 Lf))


> Node (Node Lf 2 Lf) 10 (Node Lf 12 Lf)

Examples of input in ML:

Node(2, Lf, Lf)


> Node(2, Lf, Lf)

Node(10, Node(2, Lf, Lf), Node(12, Lf, Lf))


> Node(10, Node(2, Lf, Lf), Node(12, Lf, Lf))

count(insert(16, Node(15, Node(2, Lf, Lf), Lf)))


>3

insert(count(Node(17, Lf, Node(24, Lf, Lf))), insert(12, Node(10, Lf, Lf)))


> Node(10, Node(2, Lf, Lf), Node(12, Lf, Lf))

As we can notice in the examples, the Haskell syntax of a binary tree is the following:
Node (Tree Int) Int (Tree Int)

And the ML syntax of a binary tree is: Node (int, int Tree, int Tree)
Let us take the binary tree from the following image as an example:

The representation of the tree in Haskell syntax is the following:

Node (Node Lf 2 Lf) 10 (Node Lf 12 Lf)

And the representation in ML syntax is:

Node(10, Node(2, Lf, Lf), Node(12, Lf, Lf))

In order to represent binary search trees in memory, we will create a structure with three
members: one for the node key (of type int) and two for the node’s children (pointers to left
subtree and right subtree).

typedef struct _node {


int key;
struct _node *left, *right;
} node;

There will coexist two types on the value stack: int, for numbers read from the input and for
nodes’ keys, and a structure of type tree, for reducing numbers from the input to trees in the
right format (by reducing the right-hand side of productions to the left-hand side). So, yylval will
be a union with two members:

%union {
int ival;
struct _node *btree;
}

Regarding the set of productions, we will first have a set of rules (expr) that describe the input.
We can separate instructions from the input in two categories, by the result type. The trees
reading and the insert instruction both return trees, while the count instruction returns an
integer. So, we will have two sets of rules, one for the situations resulting in a tree (t_expr) and
one for those resulting in integers (i_expr). The most important set of rules is the one describing
how a binary tree is constructed (tree) using two Haskell/ML data constructors for binary trees:
the Node constructor with three arguments and the Lf constructor with zero arguments.
The set of productions for Haskell syntax:

expr : i_expr
| t_expr
;
i_expr : COUNT t_expr
|'(' i_expr ')'
| NUMBER
;
t_expr : INSERT i_expr t_expr
| '(' t_expr ')'
| tree
;
tree : NODE tree NUMBER tree
| '(' tree ')'
| LF
;

The set of productions for ML syntax:

expr : i_expr
| t_expr
;
i_expr : COUNT '(' t_expr ')'
| NUMBER
;
t_expr : INSERT '(' i_expr ',' t_expr ')'
| tree
;
tree : NODE '(' NUMBER ',' tree ',' tree ')'
| LF
;
Proposed exercises:
1. Implement a function that searches for a number in a tree.
Examples in Haskell:
find 12 (Node (Node Lf 2 Lf) 10 (Node Lf 12 Lf))
> true
find 17 (Node (Node Lf 2 Lf) 10 (Node Lf 12 Lf))
> false

Examples in ML:
find(12, Node(10, Node(2, Lf, Lf), Node(12, Lf, Lf)))
> true
find(17, Node(10, Node(2, Lf, Lf), Node(12, Lf, Lf)))
> false

2. Implement a function that deletes a node from a tree. Check first if the node exists.
Example in Haskell:
delete 12 (Node (Node Lf 2 Lf) 10 (Node Lf 12 Lf))
> Node (Node Lf 2 Lf) 10 Lf

Example in ML:
delete(12, Node(10, Node(2, Lf, Lf), Node(12, Lf, Lf)))
> Node(10, Node(2, Lf, Lf), Lf)

3. Create a function to check if a binary tree is balanced.


Examples in Haskell:
balanced (Node (Node Lf 2 Lf) 10 (Node Lf 12 Lf))
> true
balanced (Node (Node Lf 2 Lf) 10 Lf)
> false
Examples in ML:
balanced(Node(10, Node(2, Lf, Lf), Node(12, Lf, Lf)))
> true
balanced(Node(10, Node(2, Lf, Lf), Lf))
> false

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy