Recursive Data Structures and Grammars v Themes Recursive
- Slides: 26
Recursive Data Structures and Grammars v Themes Ø Recursive Description of Data Structures Ø Grammars and Parsing Ø Recursive Definitions of Properties of Data Structures Ø Recursive Algorithms for Manipulating and Traversing Data Structures v Examples Ø Lists Ø Trees Ø Expressions and Expression Trees
Lists v. A 2 -tuple ØThe data (for this node), and the rest of the list § List = () – empty list § List = ( d, List ), where d is some element v. E. g. , L = () L = (24, L) – now (24, ()) L = (3, L ) – now (3, ( 24, ()))
Lists – Observations v. Can be modified in constant time (vs. Θ(n) for an array) v. Do not support random access. To get the kth element is Θ(n) (vs. Θ(1) for an array)
Stacks – Linear structures v. Linear structure – Last In First Out (LIFO) v. Relative order of elements is maintained v. Can be built on a list or array. All operations are constant-time v. E. g. : ØThe “undo” stack in an editor ØThe operands and operators in a scientific calculator ØThe function call stack in a running program
Stack – operations vpush(S, x) – insert x onto the “top” of S vpop(S) – remove the element at the top vis_empty(S) – true if stack is empty
Queue – Linear structure v. Linear structure – First In First Out (FIFO) v. Relative order of elements is maintained v. Can be built on a list or array. All operations are (can be) constant-time v. E. g. : ØThe print spooler ØYour email server
Queue – operations vinsert(Q, x) – insert x at the end (back) of Q vpop(Q) – remove the element at the front vis_empty(Q) – true if queue is empty
n-Trees v. Finite collection of nodes ØEach node has exactly one parent, but for the root node ØEach node may have up to n children ØA leaf node has no children ØAn interior node is a node that is not a leaf v. Each subtree is a tree rooted at a given node
Binary Trees v. A binary tree is a 2 -tree ØEach node has, at most, 2 sub-trees v. A binary tree is ØEmpty, or ØConsists of a node with 3 attributes: § value § left, which is a tree § right, which is a tree v. A subtree of a binary tree is a binary tree
Number of Nodes of a Binary Trees v. Nnodes(T) = 0 if T is empty v. Nnodes(T. left) + Nnodes(T. right) + 1
Internal Path Length (IPL) v. The sum of the length of the paths from the root to every node in the tree v. Recursively: ØIPL( T ) = 0 if T is empty ØIPL( T ) = IPL( left( T )) + IPL( right( T )) + num_nodes( T ) - 1
Paths, Ancestors, Descendants v. Given a sequence of nodes: m 1, m 2, m 3, . . . , mk , where mi is parent of mi+1 i < k Ømi is an ancestor of mj if i ≤ j Ømi is an proper ancestor of mj if i < j Ømi is an descendent of mj if i ≥ j Ømi is an proper descendant of mj if i > j Øm 1, m 2, m 3, . . . , mk is a path from m 1 to mk, of length k-1
Height of Binary Trees v. Height of tree T, H(T), is the max length of the paths from the root all of the leaves v. Height(T) = -1 if T is empty vmax( Height(T. left), Height(T. right) ) + 1
Depth of a Node in a Tree v. The depth, or level of a node is the length of the path from the root to that node
Internal Path Length v. Sum of the depths of all of the nodes in a tree ØAlternatively, the sum of the level of every node in the tree v. Recursively ØIPL(T) = 0 if T is empty ØIPL(T) = IPL(T. left) + IPL(T. right) + Nnodes(T)-1
Recurrence for the Number of Binary Trees v. Let Tn be the number of binary trees with n nodes. v. T 0 = 1, T 1 = 1, T 2 = 2, T 3 = 5
Ordered Trees v. Binary Search Tree (BST) ØBinary Tree ØAll elements in T®left are < T®value ØAll elements in T®right are >= T®value v. Each subtree of a BST is a BST
Partially Ordered Trees v. Heap (binary) ØComplete binary tree § So, height = Θ(n) ØNodes are assigned some measure, a priority ØNo node has lower priority than its children
Inorder traversal v. Recursively visit nodes in T. left vvisit root v. Recursively visit nodes in T. right v. An in order traversal of a BST lists the elements in sorted order. Proof by induction.
Expression Trees v. Basic arithmetic expressions can be represented by a binary tree ØInternal nodes are operators ØLeaf nodes are operands v. Consider 2 * ( 3 + 5 ) : v. Note that parentheses don’t appear. Expression trees are not ambiguous, as infix expressions are (can be). * 2 + 3 5
Expression Tree – In-order Traversal v. An in-order traversal yields 2*3+5 v. We put parentheses around every operation to make this correct: (2 * ( 3 + 5 ) ) (not all are needed) * 2 + 3 5
Pre- and Post-Order Traversal v. Always go left-right v. Pre (me first): § Visit node § Traverse left subtree § Traverse right subtree v. Post (me last): § Traverse left subtree § Traverse right subtree § Visit node
Expression Trees – Pre- and Post. Order v. Pre: * 2 + 3 5 v. Post: 2 3 5 + * v. Note: Parentheses never needed * 2 + 3 5
Ambiguous Grammars (preview) <expr> / | <expr>+<expr>
(blank, for notes)
- Example of unrestricted grammar
- Questions on context free grammar
- Which grammar generates regular language
- Useless symbols
- Types of grammar
- Recursive and non recursive algorithm
- Give other examples of homologous structures
- Ajit diwan
- Kevin wayne princeton
- Data structures and algorithms tutorial
- Explain single pass macro processor
- Assembler data structures
- Information retrieval data structures and algorithms
- Data structures and abstractions with java
- Data structures and algorithms bits pilani
- Adts, data structures, and problem solving with c++
- Ajit diwan iit bombay
- Data structures and algorithm
- Data structures and algorithms
- Data structures and algorithms
- Ian munro waterloo
- Information retrieval data structures and algorithms
- Ephemeral data structure
- Data structures and algorithms
- Drawing out patterns and themes from data
- Www.btechsmartclass.com
- R data structures