Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.

Similar presentations


Presentation on theme: "Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data."— Presentation transcript:

1 Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data Structures –Recursive Algorithms for Manipulating and Traversing Data Structures Examples –Lists –Trees –Expressions and Expression Trees

2 Grammars Syntactic Categories (non-terminals) – Production Rules (replace syntactic category on the rhs by the lhs, “|” is or) –  + –  –  0|1|2|3|4|5|6|7|8|9

3 Derivation Repeatedly replace syntactic categories by the lhs of rules whose rhs is equal to the syntactic category  +  + +

4 Derivation (e.g. 2)   3  23  123 When there are no more syntactic categories, the process stops and the resulting string is said to be derived from the initial syntactic category.

5 Languages The language, L( ), derivable from the syntactic category using the grammar G is defined inductively. Initially L( ) is empty If  X 1    X n is a production in G and s i = X i is a terminal or s i  L(X i ), then the concatenation s 1 s 2 …s n is in L( )

6 Language The number of strings of length n in the language L( ) is 10 n. Proof is by induction.

7 Language  () | ( ) L( ) = strings of n left parens followed by n right parens, for n >= 0.

8 Systematic Generation C statement example

9 Binary Trees A binary tree is –empty –consists of a node with 3 elements value left, which is a tree right, which is a tree

10 Height of Binary Trees Height(T) = -1 if T is empty max(Height(T.left),Height(T.right)) + 1 Alternative: Max over all nodes of the level of the node.

11 Number of Nodes of a Binary Trees Nnodes(T) = 0 if T is empty Nnodes(T.left) + Nnodes(T.right) + 1

12 Internal Path Length IPL(T) = 0 if T is empty IPL(T) = IPL(T.left) + IPL(T.right) + Nnodes(T)-1 Alternative: Sum over all nodes of the level of the node.

13 External Format for Binary Trees  []  [,, ] [], [1,[],[]], [2,[1,[],[]],[]], [2,[[],[1,[],[]]] [3, [2,[1,[],[]],[]], []], [3, [2,[[],[1,[],[]]],[]] [3, [1,[],[]], [1,[],[]]], [3, [],[2,[1,[],[]],[]]], [3, [],[2,[[],[1,[],[]]]]

14 Recurrence for the Number of Binary Trees Let T n be the number of binary trees with n nodes. T 0 = 1, T 1 = 1, T 2 = 2, T 3 = 5

15 Binary Search Trees Binary Tree All elements in T->left are value All elements in T->right are >= T->value

16 Inorder traversal Recursively visit nodes in T.left visit root Recursively visit nodes in T.right An in order traversal of a BST lists the elements in sorted order. Proof by induction.

17 Parse Tree A derivation is conveniently stored in a tree, where internal nodes correspond to syntactic categories and the children of a node correspond to the element of the rhs in the rule that was applied

18 Example Parse Tree / \ | / \ 1 | | 2 | 3

19 Recursive Decent Parser Balanced parentheses

20 Ambiguous Grammars / | \ / | \ + + / | \ / | \ + +


Download ppt "Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data."

Similar presentations


Ads by Google