Data Structures An Introduction Comp Sci Club 12
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) 1. 2. 3. 4. 5. Data Structures – some background Binary Trees – BSTs, methods(), uses, etc. *Types of Binary Trees *Heaps, *Heap. Sort Conclusion
Data Structures • Simple, efficient ways of organizing data • (Goes far Beyond the ‘Array’ and ‘Array. List’ concepts taught in CS III!)
Data Structures • Abstract Data Type – more generalized forms of these structures (e. g. Queue, Stack, Linked. List, etc. ) • Data Structure – how programmers/software developers implement these data types (Array. Queue, Linked. Stack/Array. Stack, Singly/Doubly. Linked. List) • 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 Tree. Node objects: each ‘level’ of this structure has up to 2 N 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/15121/lectures/Trees/trees. html • BST represents hierarchy; allows very efficient searching or inserting; and can be manipulated easily Root Node → J F B Leaves M H T Y
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_tree http: //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 1850 s!) – 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_tree http: //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 tree a) b) c) Bottom row (‘level’) fills from the left Allows simplementations (Array duality) 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 Priority. Queue a) b) Queue, you may recall, is a built-in Java interface, modeling ‘FIFO’ Priority. Queue – more specific type that assigns a priority to each task/value, removing the highest Priority first
Heaps* • Percolation Up/Down Method 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/~jmor 159/PLDS 210/h eaps. html • http: //www. cs. cmu. edu/~adamchik/15121/lectures/Binary%20 Heaps/heaps. html
Heap. Sort! • 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 heap 3. Perform comparisons (from left to right) to “rebalance” or reconfigure the heap again (method sift. Up() or sift. Down()) 4. Repeat steps 2 -3 until there are no Nodes remaining http: //stackoverflow. com/questions/8938375/an-intuitive-understanding-of-heapsort http: //en. wikipedia. org/wiki/Heapsort https: //www. cs. auckland. ac. nz/~jmor 159/PLDS 210/heapsort. html
Animated Simulations • http: //www. ee. ryerson. ca/~courses/coe 428/sorting/heapso rt. 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
- Slides: 16