Programmeerimise phikursus - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Programmeerimise phikursus

Description:

Infix, prefix, postfix operators. Stack machine for computing postfix. Parsing. Recall lists ... A collision is not an error. Infix, prefix, postfix. On the blackboard ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 18
Provided by: lam42
Category:

less

Transcript and Presenter's Notes

Title: Programmeerimise phikursus


1
  • Programmeerimise põhikursus

2
Overview of the lecture
  • More trees
  • Hash tables
  • Infix, prefix, postfix operators
  • Stack machine for computing postfix
  • Parsing

3
Recall lists
  • Could we in principle always use nested arrays
    (one array inside another?) instead of linked
    lists?
  • a013 a17 a22
    a319 a444

Array representation
13
7
2
19
44
Above two different representations of a list
of elements
(13,7,2,19,44)?
Linked list representation
4
Recall binary trees
  • class TreeNode
  • int item
  • TreeNode left
  • TreeNode right
  • static int countNodes( TreeNode root )
  • if ( root null )?
  • return 0
  • else
  • int count 1
  • count countNodes(root.left)
  • count countNodes(root.right)
  • return count
  • // end countNodes

5
Recall binary trees as a universal datatype
  • Binary trees can be used to construct any other
    datatype, including lists, arrays etc.
  • However, often it is faster and more efficient to
    use other datatypes directly, not through
    simulating them via binary trees.
  • Lisp and scheme two datatypes used binary trees
    (with list syntax) and arrays.
  • Possible example construct the following list as
    a binary tree
  • (a (1 2) () ((e) f) )?

6
Efficiency of standard operations on data
structures
  • Unsorted array
  • Search is slow (every element is looked through)?
  • Insertion is slow (all elements to the right must
    be moved)?
  • Sorted array
  • Search is fast (use binary search!)?
  • Insertion is slow (all elements to the right must
    be moved)?
  • List
  • Search is slow (every element is looked through)?
  • Insertion is fast (elements to the right are not
    moved)?
  • Binary (balanced) sort tree
  • Search is fast (binary search)?
  • Insertion is fast (elemetns to the right are not
    moved)?

7
Binary sort tree
  • A binary tree with a specially sorted form
  • For every node in the tree, the item in that node
    is
  • greater than every item in the left subtree of
    that node,
  • less than or equal to all the items in the right
    subtree of that node.

8
Expression trees
  • expr

9
Expression tree class def example, constructors
  • class ExpNode // A node in an expression tree.
  • static final int NUMBER 0, OPERATOR 1
  • int kind // Which type of node is this?
  • double number // The value in a node of type
    NUMBER.
  • char op // The operator in a node of
    type OPERATOR.
  • ExpNode left // Pointers to subtrees,
  • ExpNode right // in a node of type
    OPERATOR.
  • ExpNode( double val ) // NUMBER
  • kind NUMBER
  • number val
  • ExpNode( char op, ExpNode left, ExpNode right
    ) // OPERATOR
  • kind OPERATOR
  • this.op op
  • this.left left
  • this.right right
  • // end class ExpNode

10
Expression tree evaluation code example
  • static double getValue( ExpNode node )
  • if ( node.kind NUMBER )
  • // The value of a NUMBER node is the number
    it holds.
  • return node.number
  • else // The kind must be OPERATOR.
  • // Get the values of the operands and
    combine them
  • // using the operator.
  • double leftVal getValue( node.left )
  • double rightVal getValue( node.right )
  • switch ( node.op )
  • case '' return leftVal rightVal
  • case '-' return leftVal - rightVal
  • case '' return leftVal rightVal
  • case '/' return leftVal / rightVal
  • default return Double.NaN // Bad
    operator.
  • // end getValue()

11
Alternative treerep abstract and constant node
  • abstract class ExpNode
  • abstract double value() // Return
    the value of this node.
  • // end class ExpNode
  • class ConstNode extends ExpNode // Represents
    a node that holds a number
  • double number // The number in the
    node.
  • ConstNode( double val ) //
    Constructor. Create a node to hold val.
  • number val
  • double value() // The value is
    just the number that the node holds.
  • return number
  • // end class ConstNode

12
Alternative treerep internal node class
  • class BinOpNode extends ExpNode //
    Represents a node that holds an operator.
  • char op // The operator.
  • ExpNode left // The left operand.
  • ExpNode right // The right operand.
  • BinOpNode( char op, ExpNode left,
    ExpNode right ) // Constructor.
  • this.op op this.left left
    this.right right
  • double value()
  • double leftVal left.value()
  • double rightVal right.value()
  • switch ( op )
  • case '' return leftVal
    rightVal
  • case '-' return leftVal -
    rightVal
  • case '' return leftVal
    rightVal
  • case '/' return leftVal /
    rightVal
  • default return Double.NaN
    // Bad operator.
  • // end class BinOpNode

13
Parsing
  • On the blackboard

14
Hash table
  • Hash tables are used for quickly finding elements
    in a an array or a list.
  • For example to quickly find strings.
  • There are several ways to quickly find objects,
    in general
  • Binary search on a sorted array
  • Binary sorted trees of some kind
  • Hash tables

15
Hash table
16
Hash table
  • Hash tables store their data in an array,
  • The array index where a key is stored is based on
    the key.
  • The index is not equal to the key, but it is
    computed from the key.
  • The array index for a key is called the hash code
    for that key.
  • A function that computes a hash code, given a
    key, is called a hash function.
  • To find a key in a hash table, you just have to
    compute the hash code of the key and go directly
    to the array location given by that hash code.
  • Example If the hash code is 17, look in array
    location number 17.
  • Since there are fewer array locations than there
    are possible keys, it's possible that we might
    try to store two or more keys in the same array
    location. This is called a collision. A collision
    is not an error.

17
Infix, prefix, postfix
  • On the blackboard
Write a Comment
User Comments (0)
About PowerShow.com