Top Banner
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
35

What is a Data Structure? - Piazza

Apr 30, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: What is a Data Structure? - Piazza

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

Page 2: What is a Data Structure? - Piazza

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...

Page 3: What is a Data Structure? - Piazza

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

Page 4: What is a Data Structure? - Piazza

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

Page 5: What is a Data Structure? - Piazza

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

Page 6: What is a Data Structure? - Piazza

Trees

● Data structure which looks like an upside down tree (or the root system of a tree)

– Nodes have parents and children

– No loops

Page 7: What is a Data Structure? - Piazza

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

Page 8: What is a Data Structure? - Piazza

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!)

Page 9: What is a Data Structure? - Piazza

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?

Page 10: What is a Data Structure? - Piazza

Examples

● What is the root?● Which nodes are leaf nodes?● Which nodes are inner nodes?● Where are the null links?

Page 11: What is a Data Structure? - Piazza

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

Page 12: What is a Data Structure? - Piazza

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

Page 13: What is a Data Structure? - Piazza

Types of Trees

Page 14: What is a Data Structure? - Piazza

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/

Page 15: What is a Data Structure? - Piazza

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!

Page 16: What is a Data Structure? - Piazza

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...

Page 17: What is a Data Structure? - Piazza

Terms: Nodes and Edges

● Nodes/Verticies– |V| = number of verticies

– Data

– Neighbors

– Identifier/Key

● Edges– |E| = number of edges

– Weights

– Direction

Page 18: What is a Data Structure? - Piazza

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

Page 19: What is a Data Structure? - Piazza

Source: http://en.wikipedia.org/wiki/File:Map_of_USA_with_state_names_2.svg

Page 20: What is a Data Structure? - Piazza

Source: http://en.wikipedia.org/wiki/File:UnitedStatesGraphViz.png

Page 21: What is a Data Structure? - Piazza

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?

Page 22: What is a Data Structure? - Piazza

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?

Page 23: What is a Data Structure? - Piazza

The Practical: Graph Storage

● Graph Data● Graph Storage:

– Adjacency Matrix

– Adjacency List

Page 24: What is a Data Structure? - Piazza

And now for something completely different...

Hashing!

Page 25: What is a Data Structure? - Piazza

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?

Page 26: What is a Data Structure? - Piazza

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?

Page 27: What is a Data Structure? - Piazza

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

Page 28: What is a Data Structure? - Piazza

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

Page 29: What is a Data Structure? - Piazza

So... Let's Practice!

● Hash these cards!

Page 30: What is a Data Structure? - Piazza

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

Page 31: What is a Data Structure? - Piazza

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)

Page 32: What is a Data Structure? - Piazza
Page 33: What is a Data Structure? - Piazza

General Issues for Data Structures

Page 34: What is a Data Structure? - Piazza

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?

Page 35: What is a Data Structure? - Piazza

Searching & Data Structures

● Visit every item?● Visit only some items?● Searching a List...

– 1, 3, 6, 24, 26, 38

● Searching in a Tree...● Searching in a Graph...