CS 211 Object Oriented Programming Instructor: Katherine (Raven) Russell Email: [email protected] Office: ENGR 5328 Open Office Hours: Tu/Th 4:30-5:30pm other times by appointment
CS 211Object Oriented Programming
Instructor: Katherine (Raven) Russell
Email: [email protected]
Office: ENGR 5328
Open Office Hours: Tu/Th 4:30-5:30pmother times by appointment
What is a Data Structure?
● Well... it's a structure that holds data– linked lists were data structures, as were stacks,
queues, etc.
● It's used to organize the data– makes data easier to use
– makes data easier to find
– makes sure things happen in a certain order
● Fruit...
Dynamic Arrays
● Java: ArrayLists● Data structure like a
paper and pencil list● Need to copy things
down/over to add items● Need more paper if not
enough space
Image Source: http://en.wikipedia.org/wiki/File:Dy
namic_array.svg
Linked Lists
● Data structure along the lines of a treasure hunt– Start at the beginning
– At each “stop” find an item and the clue to the next place to look
Image Source: http://en.wikipedia.org/wiki/File:Singly-linked-list.svg
Stacks & Queues
● Stack– Data structure that works
like a... stack (e.g. a stack of paper)
● Queue– Data structure that works
like a... queue (or a “line” if you aren't British)
Image Source: http://en.wikipedia.org/wiki/File:Data_stack.svg and http://en.wikipedia.org/wiki/File:Data_Queue.svg
Trees
● Data structure which looks like an upside down tree (or the root system of a tree)
– Nodes have parents and children
– No loops
Trees
● Collection of nodes and edges– These nodes are different!
– Any shape, but can't have a loop
● Nodes have:– data
– (possibly) a “key” to sort/search by
– (possibly) pointer to children
– (possibly) pointer to parent
Types of Nodes
● By Relationship– Parent/Child Nodes
– Ancestor/Descendent Nodes
● By Tree Location– Inner/Branch/Internal Nodes
– Outer/Leaf/External/Terminal Nodes
– Root Node (only 1!)
Examples
● What is the parent of 27?● What are the children of 67?● What are the ancestors of 59?● What are the descendents of 55?
Examples
● What is the root?● Which nodes are leaf nodes?● Which nodes are inner nodes?● Where are the null links?
Common Tree Operations
● Enumerating = mention a number of things one by one (define:Enumerating in google)
– all the items
– a section of a tree
● Searching for an item● Adding/Deleting items● Pruning/Grafting● Balancing
Tree Data Structures
● Arrays– Need to know where each item is
● How? Need to limit number of children
– Most common for balanced binary trees
– Fast memory access (compared to linked)
● Linked Data Structures– easy to add, remove, and swap around parts
of the tree
Example: Binary Search Tree
● What is a binary tree?– every node can have at most two children
● What is a binary search tree?– binary tree with special rules:
● parent > elements in left sub tree● parent < elements in right sub tree● both children are binary search trees● no duplicates (how do we handle this?)
● http://people.ksp.sk/~kuko/bak/
Binary Search Tree
● Below is a binary search tree● If we wanted to find out if the value 40 was in
the tree, how would you go about it?● Like a linked list you can do a binary search on!
Graphs
● Data structure which works kinda like a road map
– Locations = nodes
– Roads (or “how to get from one location to another) = edges (or “how to get from one node to another)
– Linked Lists and Trees are graphs too...
Terms: Nodes and Edges
● Nodes/Verticies– |V| = number of verticies
– Data
– Neighbors
– Identifier/Key
● Edges– |E| = number of edges
– Weights
– Direction
Uses for a Graph
● Representing network-like structures– maps, paths, locations
– geometry and graphics (meshes)
– neurons, skeletons, etc.
– oh... and networks● computer networks● social networks
What Would We Like To Know?
● Are Nebraska and Oklahoma next to each other?
● How can you get from California to Virginia?● What is the fewest number of state you can
travel through to get from Oregon to Maine?● What is the population of Texas?
What Information Can We Get?
● the adjacency of two items● a path from two places
– shortest path?
● distance between two places?– path cost?
● values at those places?
Motivation
● I have x, and I want to...– put it in an array
– find it again quickly
● I could put it at myArray[x]– but x can be anything (not just an int)
● that's ok! everything is a number in computers!
– and arrays can't be infinite● well that's a problem
● What do I do?
The General Problem
● Variable length input– e.g. a string encoded as a number (see the
alphabet song from earlier
● Fixed length output– e.g. the valid integer indexes of an array
● Problems– how do I compute the values?
– what other issues occur if I have more values than places to put them?
For Example...
● I have students (x), and I want to...– put them in chairs I've numbered 0-30 (an
array)
– find a student quickly when I have his/her paper without wandering all over the room
● Variable length input and fixed length output– but students (x) aren't integers
● how do I represent students as numbers?
– and chairs (arrays) aren't infinite
Basic Hashing
● we need to put things in slots 0 → X● we have data● based on the data, generate a number
– e.g. “A” names go to slot 0, “B” in slot 1, etc.
● if the number is too big, use modulo to fix the problem
– e.g. if we have 10 chairs, z = 25, we can use % to pick a slot (in this case 5)
● put the data in that slot
Hash Terminology
● Hash Code– the integer computed (not fitted to the array)
● Hash Function– computes a hash code from an object
● Hash Table – Mapping where keys are based on hash codes
and values are the thing being hashed
– Look a lot like arrays / linked lists
● Load = item count / table size
Back to Same Spot Issues
● What if that slot is already occupied by something else?
– Separate Chaining (shown on next slide)
– Open Addressing (figure out a new slot)
Important Questions
● How does it work?– How do you find things?
– How do you put things in?
– What are the “rules”?
● What are the benefits / trade offs?– How fast is it?
– How big is it?