data structures – an introduction

16
Data Structures – An Introduction Comp Sci Club 12 June 2014

Upload: saad

Post on 22-Feb-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Data Structures – An Introduction. Comp Sci Club 12 June 2014. After CS III AP…. First of all – Congratulations on finishing CS III There is much more to Comp Sci (A lot more than I thought when I took the AP Exam!). Overview (for Today’s Meeting). Data Structures – some background - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data Structures – An  Introduction

Data Structures –An Introduction

Comp Sci Club12 June 2014

Page 2: Data Structures – An  Introduction

After CS III AP…

• First of all – Congratulations on finishing CS III• There is much more to Comp Sci– (A lot more than I thought when I took the AP Exam!)

Page 3: Data Structures – An  Introduction

Overview (for Today’s Meeting)

1. Data Structures – some background2. Binary Trees – BSTs, methods(), uses, etc.3. *Types of Binary Trees4. *Heaps, *HeapSort5. Conclusion

Page 4: Data Structures – An  Introduction

Data Structures

• Simple, efficient ways of organizing data• (Goes far Beyond the ‘Array’ and ‘ArrayList’

concepts taught in CS III!)

Page 5: Data Structures – An  Introduction

Data Structures

• Abstract Data Type – more generalized forms of these structures (e.g. Queue, Stack, LinkedList, etc.)

• Data Structure – how programmers/software developers implement these data types (ArrayQueue, LinkedStack/ArrayStack, Singly/DoublyLinkedList)

• However…– There are some concepts which are more ambiguous, and

can be considered part-“data type” and part-“data structure”

– And, we will see a few of these in this presentation

http://cs.lmu.edu/~ray/notes/dtds/

Page 6: Data Structures – An  Introduction

The Binary (Search) Tree• Organization of TreeNode objects: each ‘level’ of this

structure has up to 2N Nodes.• Each Node has a reference to value; left, and right• Several different kinds of binary trees; a very common

one is the Binary Search Tree– All Nodes left of root have VALUES less than the root; All

Nodes right of root have VALUES greater than the root.– This principle applies to every Node, in fact.

Page 7: Data Structures – An  Introduction

The Binary (Search) Tree• http://www.cs.cmu.edu/~adamchik/15-121/lectures/Tr

ees/trees.html• BST represents hierarchy; allows very efficient

searching or inserting; and can be manipulated easily

J

F

B H

M

T

Y

Root Node →

Leaves

Page 8: Data Structures – An  Introduction

Binary Trees – Specific Types*

– Full Binary Tree – every node has zero or two ‘children’• Perfect Binary Tree – all leaf nodes lie on same level

– Complete Binary Tree – every level is filled completely, except perhaps the last level (more detail later)

http://en.wikipedia.org/wiki/Binary_treehttp://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html http://www.scribd.com/doc/17116770/Types-of-Binary-Tree

Page 9: Data Structures – An  Introduction

Infinite Binary Trees – A Digression

– Stern-Brocot Tree – Mathematical model for the set of all positive rational numbers (known since the 1850s!)

– Wikipedia: the cardinalities of these numbers (the nodes themselves = 0א ; ways of permuting them = c)– There are numerous interesting facts about this model – see

cut-the-knort.org and other sites for more info

http://en.wikipedia.org/wiki/Binary_treehttp://www.cut-the-knot.org/blue/Stern.shtml

Page 10: Data Structures – An  Introduction

Heaps

Page 11: Data Structures – An  Introduction

A Heap*

Disambiguation: I am not talking about THE Heap, which is considered the source for all dynamic data (i.e., how data is stored in an Object rather than a primitive type.)

….What do I mean then?

Page 12: Data Structures – An  Introduction

A Heap*

I. Special Binary Tree – a complete binary treea) Bottom row (‘level’) fills from the leftb) Allows simple implementations (Array duality)c) Max-heap property – root node is greater than all others; min-

heap property – root node is less than all others

II. Technically, the heap can be seen as a data structure for a PriorityQueue

a) Queue, you may recall, is a built-in Java interface, modeling ‘FIFO’

b) PriorityQueue – more specific type that assigns a priority to each task/value, removing the highest Priority first

Page 13: Data Structures – An  Introduction

Heaps*

• Percolation Up/DownMethod of adding/deleting a Node to/from a Heap.Involves swapping the last/final Node with the Root

Node, then comparing the new ‘last’ with its parent Nodes to find where it should be.

• https://www.cs.auckland.ac.nz/~jmor159/PLDS210/heaps.html

• http://www.cs.cmu.edu/~adamchik/15-121/lectures/Binary%20Heaps/heaps.html

Page 14: Data Structures – An  Introduction

HeapSort!• Relatively efficient kind of sort• O (n log (n)) [for a worst case, this is not too bad]• Collects all elements into heap, sorts them inside the Heap,

then remove each while updating the structure• In-place algorithm – uses small, constant memory• Somewhat complicated process

1. Make a heap out of the given info // Can be max or min!2. Remove the root Node; replace it with the final Node in the heap3. Perform comparisons (from left to right) to “rebalance” or

reconfigure the heap again (method siftUp() or siftDown())4. Repeat steps 2-3 until there are no Nodes remaining

http://stackoverflow.com/questions/8938375/an-intuitive-understanding-of-heapsorthttp://en.wikipedia.org/wiki/Heapsorthttps://www.cs.auckland.ac.nz/~jmor159/PLDS210/heapsort.html

Page 15: Data Structures – An  Introduction

Animated Simulations• http://www.ee.ryerson.ca/~

courses/coe428/sorting/heapsort.html• http://

en.wikipedia.org/wiki/Heapsort#mediaviewer/File:Heapsort-example.gif

Page 16: Data Structures – An  Introduction

Thank you Very Much!

• This should be posted on http://mthcompsci.wordpress.com/ within a week

• Many thanks to all of you for a great year of CS Club• I exhort you to continue next year– (March competition!)– (Recruiting more people?)

• Good luck on Final Exams; have a great summer