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

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Fundamentals of Python: From First Programs Through Data Structures
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Trees, and Binary Search Trees COMP171.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Fundamentals of Programming II Introduction to Trees
Tree.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Trees 7/14/2009.
CS212: Data Structures and Algorithms
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

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

Lists  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  E.g., L = () L = (24, L) – now (24, ()) L = (3, L ) – now (3, ( 24, ()))

Lists – Observations  Can be modified in constant time (vs. Θ(n) for an array)  Do not support random access. To get the k th element is Θ(n) (vs. Θ(1) for an array)

Stacks – Linear structures  Linear structure – Last In First Out (LIFO)  Relative order of elements is maintained  Can be built on a list or array. All operations are constant-time  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  push(S, x) – insert x onto the “top” of S  pop(S) – remove the element at the top  is_empty(S) – true if stack is empty

Queue – Linear structure  Linear structure – First In First Out (FIFO)  Relative order of elements is maintained  Can be built on a list or array. All operations are (can be) constant-time  E.g.:  The print spooler  Your server

Queue – operations  insert(Q, x) – insert x at the end (back) of Q  pop(Q) – remove the element at the front  is_empty(Q) – true if queue is empty

n-Trees  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  Each subtree is a tree rooted at a given node

Binary Trees  A binary tree is a 2-tree  Each node has, at most, 2 sub-trees  A binary tree is  Empty, or  Consists of a node with 3 attributes:  value  left, which is a tree  right, which is a tree  A subtree of a binary tree is a binary tree

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

Internal Path Length (IPL)  The sum of the length of the paths from the root to every node in the tree  Recursively:  IPL( T ) = 0 if T is empty  IPL( T ) = IPL( left( T )) + IPL( right( T )) + num_nodes( T ) - 1

Paths, Ancestors, Descendants  Given a sequence of nodes: m 1, m 2, m 3,..., m k, where m i is parent of m i+1  i < k  m i is an ancestor of m j if i ≤ j  m i is an proper ancestor of m j if i < j  m i is an descendent of m j if i ≥ j  m i is an proper descendant of m j if i > j  m 1, m 2, m 3,..., m k is a path from m 1 to m k, of length k-1

Height of Binary Trees  Height of tree T, H(T), is the max length of the paths from the root all of the leaves  Height(T) = -1 if T is empty  max( Height(T.left), Height(T.right) ) + 1

Depth of a Node in a Tree  The depth, or level of a node is the length of the path from the root to that node

Internal Path Length  Sum of the depths of all of the nodes in a tree  Alternatively, the sum of the level of every node in the tree  Recursively  IPL(T) = 0 if T is empty  IPL(T) = IPL(T.left) + IPL(T.right) + Nnodes(T)-1

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,[],[]]]]

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

Ordered Trees  Binary Search Tree (BST)  Binary Tree  All elements in T ® left are < T ® value  All elements in T ® right are >= T ® value  Each subtree of a BST is a BST

Partially Ordered Trees  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  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.

Expression Trees  Basic arithmetic expressions can be represented by a binary tree  Internal nodes are operators  Leaf nodes are operands  Consider 2 * ( ) : *  Note that parentheses don’t appear. Expression trees are not ambiguous, as infix expressions are (can be).

Expression Tree – In-order Traversal  An in-order traversal yields 2 *  We put parentheses around every operation to make this correct: (2 * ( ) ) (not all are needed) * 2+ 35

Pre- and Post-Order Traversal  Always go left-right  Pre (me first):  Visit node  Traverse left subtree  Traverse right subtree  Post (me last):  Traverse left subtree  Traverse right subtree  Visit node

Expression Trees – Pre- and Post- Order  Pre: *  Post: *  Note: Parentheses never needed * 2+ 35

Ambiguous Grammars (preview) / | \ / | \ + + / | \ / | \ + +

(blank, for notes)