Data Structures –An Introduction
Comp Sci Club12 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)
1. Data Structures – some background2. Binary Trees – BSTs, methods(), uses, etc.3. *Types of Binary Trees4. *Heaps, *HeapSort5. Conclusion
Data Structures
• Simple, efficient ways of organizing data• (Goes far Beyond the ‘Array’ and ‘ArrayList’
concepts taught in CS III!)
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/
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.
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
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
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
Heaps
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?
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
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
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
Animated Simulations• http://www.ee.ryerson.ca/~
courses/coe428/sorting/heapsort.html• http://
en.wikipedia.org/wiki/Heapsort#mediaviewer/File:Heapsort-example.gif
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
Top Related