Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Lilian Blot Recursion Autumn 2012 TPOP 1. Lilian Blot Recursion Autumn 2012 TPOP 2.
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.
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Introduction to Trees Chapter Trees Why Trees? Trees are amazingly useful in Computer Science. They provide a structure for the efficient storage,
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
Recursion. Binary search example postponed to end of lecture.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
The Design and Analysis of Algorithms
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
A Level Computer Science Topic 9: Data Structures T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
Lecture 17 Non-Linear data structures Richard Gesick.
Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
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 ),
A Binary Search Tree Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Data Structures TREES.
Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming.
Object Oriented Analysis and Design Class and Object Diagrams.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Modern Programming Tools And Techniques-I
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Data Structure By Amee Trivedi.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Binary Trees and Binary Search Trees
The Design and Analysis of Algorithms
Binary search tree. Removing a node
Binary Trees our leafy, annoying friends
Lecture 22 Binary Search Trees Chapter 10 of textbook
ITEC 2620M Introduction to Data Structures
Data Structures & Algorithms
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
A Introduction to Computing II Lecture 13: Trees
Java Programming Language
Data Structures Using C++ 2E
C++ Object Oriented 1.
Presentation transcript:

Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things

Today Functions Object Oriented Programming Trees Graphs Recursion 1

Functions in Python Functions are a way to structure code for reuse. Return Values Arguments 2

Multiple return values x, y = swapTwoValues(x, y) def swapTwoValues(x, y): return y, x 3

Arguments The information you give to a method is called an argument def swapTwoValues(x, y): return y, x 4

Default Arguments Arguments can be optional def myFunction(text, offset=0, prefix=‘hi ’): print ‘ ‘*offset + prefix + text myFunction(“andrew”) myFunction(“andrew”, 5) 5

Named Arguments The specific argument can be named in the call to the function def myFunction(text, offset=0, prefix=‘hi ’): print ‘ ‘*offset + prefix + text myFunction(“andrew”) myFunction(“andrew”, prefix=“hello ”) myFunction(text=“andrew”, offset=2) 6

Object Oriented Programming aka Object Oriented Design. Structure functionality into “objects” with associated “member variables” and “methods”. 7

Objects vs. Types str and int are special classes They have methods defined specifically for them However, these are called types because they are built-in to the language. The distinction is rather blurry. 8

Classes and Instances int is a class 1 is an instance of the class “int” fd = FreqDist() FreqDist is a class fd is an instance of FreqDist. 9

Classes in Python class MyClass: def __init__(self): self.text = ‘hello’ def __repr__(self): return self.text also __len__ __str__ and __add__ 10

Importing a class from filename import class The filename specifies the package name of the class. This can be used like any other package. from filename import * 11

Inheritance Objects can define relationships between objects. Membership operations allow for the representation of “has-a”, “has-many”, and arbitrary property relationships. Inheritance relationships allow for the representation of “is-a” relationships. 12

Shape Example 13 Shape Rectangle Triangle Circle Square

Trees Binary Trees are a commonly used data structure. The core element of a tree is a node These nodes can contain values, and include pointers to one or more children, that are differentiated as “left” and “right” 14

Tree Example a a xyz 15 The

Binary Search Trees Binary Search trees have the properties –the value (or key), of any node is greater than the value of its left child –the value of any node is less than the value of its right child. 16

Binary Search Tree Example What does this have to do with Binary Search?

Tree class What does a tree class require? 18

Graphs Graphs are similar to Trees Graphs have: –Nodes –Edges Nodes can contain values Edges connect two notes, and can also have a value. 19

Graph Example a a b c c d

What does a Graph class require? 21

In class coding Using an object in a program Initializing data in an object Objects that you can iterate over Write a Shape class Write a Tree class Write a Graph class 22

Recursion A function that calls itself is called a recursive function. A good recursive function must include –A stopping condition –Modification 23

Example of Recursion Print every element of a tree. Search for an entry in a tree. Print every element of a graph. Search in a graph. 24

Next Time Machine Learning –A primer on machine learning and its role in Computational Linguistics. –Classification in NLTK (after break) 25