320 likes | 433 Vues
Discrete Structure. Li Tak Sing( 李德成 ) Lectures 14-15. Recursive functions for binary tree. Let "nodes" be the function that returns the number of nodes in a binary tree. Using the pattern matching form nodes(<>)=0 nodes(tree(L,a,R))=1+nodes(L)+nodes(R) Using the if-then-else-form
E N D
Discrete Structure Li Tak Sing(李德成) Lectures 14-15
Recursive functions for binary tree • Let "nodes" be the function that returns the number of nodes in a binary tree. • Using the pattern matching form • nodes(<>)=0 • nodes(tree(L,a,R))=1+nodes(L)+nodes(R) • Using the if-then-else-form • nodes(T) = if T=<> then 0 else 1+nodes(left(T))+nodes(right(T))fi
Binary search tree • Let 'insert' be a function that accepts two parameters, x and a binary search tree. The function returns another binary search tree so that x is inserted into the original tree. • insert(x,<>)=tree(<>,x,<>). • insert(x,tree(L,a,R))=if (x<a) then tree(insert(x,L),a,R) else tree(L,a,insert(x,R))
Binary search tree • Another form: • insert(x,T)= if T=<> then tree(<>,x,<>) else if x<root(T) then tree(insert(x,left(T)),root(T), right(T)) else tree(left(T),root(T), insert(x,right(T)))
Traversing Binary Trees • Preorder traversal • The preorder traversal of a binary tree starts by visiting the root. Then there is a preorder traversal of the left subtree • Preorder procedurePreorder(T): if T<> then print(root(T)); Preorder(left(T)); Preorder(right(T)); fi.
A preorder function • A preorder function is defined below: • preOrd(<>)=<>, • preOrd(tree(L,x,R))=x::cat(preOrd(L),preOrd(R))
Inorder traversal • The inorder traversal of a binary tree starts with an inorder traversal of the left tree. Then the root is visited. Lastly there is an inorder traversal of the right subtree. • Inorder procedureInorder(T): if T<> theninorder(left(T)); print(root(T));inorder(right(T)); fi.
Inorder function • A inorder function is defined below: • preOrd(<>)=<>, • preOrd(tree(L,x,R))=cat(preOld(L),x::preOld(R))
postorder traversal • The postorder traversal of a binary tree starts with a postorder of the left subtree and is followed by a postorder traversal of the right subtree. Lastly the root is visited.
Example. • Write a recursive definition for the procedure P such that for any binary tree T, P(T) prints those nodes of T that have two children, during inorder traverse of the tree. • Write down a recursive procedure to print out the leaves of a binary tree that are encountered during an inorder traverse of the tree.
The repeated element problem • To remove repeated elements from a list. • remove(<a,b,a,c>)=<a,b,c> • Only the left most repeated element is kept. • To find the solution, lets consider a list with head H and tail T. • The function can be done by removing all occurrences of H in T and then concatenate H with that result.
The repeated element problem • So the remove function can be defined as: • remove(<>)=<> • remove(H::T)=H::remove(removeAll(H,T)) • We have assumed the existence of a function removeAll which will remove all the occurrences of H in T. • For example removeAll(a,<a,b,a,c,d>) would return <b,c,d>
removeAll • removeAll(a,<>)=<> • removeAll(a,a::T)=removeAll(a,T) • removeAll(a,H::T)=H::removeAll(a,T)
removeAll • removeAll(a,M)= if M=<> then <> else if a=head(M) then removeAll(a,tail(M)) else head(M)::removeAll(a,tailM))
Grammars • A set of rules used to define the structure of the strings in a language. • For example, the following English sentence consists of a subject followed by a predicate: The big dog chased the catsubject: The big dogpredicate: chased the cat
Grammar • The grammar of the sentence is:sentence subject predicate • The subject phase can be further divided into an article, an adjective and a noun:subject article adjective nounso article=this, adjective=big, noun=dog
Grammer • Similarly, the predicate can have the following rule:predicate verb objectverb=chased, object=cat
Structure of grammas • If L is a language over an alphabet A, then a grammer for L consists of a set of grammer rules of the formwhere and denotes strings of symbols taken from A.
Structure of grammas • The grammar rule is often called a production, and it can be read in several different ways as replace by produces rewrites to reduces to
Start symbol • Every grammar has a special grammar symbol called the start symbol, and there must be at least one production with the left side consisting of only the start symbol. For example, if S is the start symbol for a grammar, then there must be at least one production of the form: S
An example • S • SaS • SbS • ScS • All strings that consists of a, b and c.
Definition of a Grammar • An alphabet N of grammar symbols called nonterminals. • An alphabet T of symbols called terminals. The terminals are distinct from the nonterminals. • A specific nonterminal S, call the start symbol. • A finite set of productions of the form , where and are strings over the alphabet NT with the restriction that is not the empty string. There is at least one production with only the start symbol S on its left side. Each nonterminal must appear on the left side of some production.
Grammar • When two or more productions have the same left side, we can simplify the notation by writing one production with alternate right sides separated by the vertical line |. For example, the last grammar can be rewritten as:S | aS | bS | cS
Grammars • A grammar is a 4-tuple. G=(N,T,S,P) • N: non-terminals, T: terminals,S: start symbol,P: rules • In the last example, we have G=({S},{a,b,c},S,{S | aS | bS | cS • })
Derivations • A string made up of terminals and nonterminals is called a sentential form. • If x and y are sentential forms and is a production, then the replacement of by is called derivation, and we denote it by writing xyxy
Derivations • derives in one step • + derives in one or more steps • * derives in zero or more steps
Examples in derivatives • S=AB • A=|aA • B= |bB • SAB AbB Ab aAb aaAb aab
Rightmost and leftmost derivation • It is possible to find several different derivations of the same string. • For example, we can have • S AB AbB Ab aAb ab • S AB aAB aB abB ab • A derivation is called a leftmost derivation if at each step the leftmost nonterminal of the sentential is reduced by some production. Similarly, a derivation is called a rightmost derivation if at each step the rightmost nonterminal of the sentential form is reduced by some production.
The language of a grammar • If G is a grammar, then the language of G is the set of terminal strings derived from the start symbol of G. The language of G is denoted by L(G). • The formal definition is:If G is a grammar with start symbol S and set of terminals T, then the language of G is the set L(G)={s | sT* and S+s}
Recursive production • A production is called recursive if its left side occurs on its right side. For example, the production SaS is recursive. • A production A is indirectly recurisve if A derives a sentential form that contains A.
Recursive production • For example: • Sb|aA • A c|bS • The productions S aA and A bS are both indirectly recurisve: • SaA abS • A bS baA
Recursive grammar • A grammar is recursive if it contains either a recursive production or an indirectly recursive production. • A grammar for an infinite language must be recursive.