DOC PREVIEW
Duke CPS 100E - Lecture

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

What’s the Difference Here?Contrast LinkedList and ArrayListWhat about LinkedList?Linked list applications continuedLinked lists, CDT and ADTBuilding linked listsDissection of add-to-frontStandard list processing (iterative)Standard list processing (recursive)Recursion with picturesRecursion and linked listsBinary TreesFrom doubly-linked lists to binary treesBasic tree definitionsA TreeNode by any other name…Printing a search tree in orderTree traversalsTree functionsBalanced Trees and ComplexityWhat is complexity?CompSci 100e9.1What’s the Difference Here?How does find-a-track work? Fast forward?CompSci 100e9.2Contrast LinkedList and ArrayListSee ISimpleList, SimpleLinkedList, SimpleArrayListMeant to illustrate concepts, not industrial-strengthVery similar to industrial-strength, howeverArrayList --- why is access O(1) or constant time?Storage in memory is contiguous, all elements same sizeWhere is the 1st element? 40th? 360th?Doesn’t matter what’s in the ArrayList, everything is a pointer or a reference (what about null?)CompSci 100e9.3What about LinkedList?Why is access of Nth element linear time?Why is adding to front constant-time O(1)?frontCompSci 100e9.4Linked list applications continuedIf programming in C, there are no “growable-arrays”, so typically linked lists used when # elements in a collection varies, isn’t known, can’t be fixed at compile timeCould grow array, potentially expensive/wasteful especially if # elements is small.Also need # elements in array, requires extra parameterWith linked list, one pointer used to access all the elements in a collectionSimulation/modeling of DNA gene-splicingGiven list of millions of CGTA… for DNA strand, find locations where new DNA/gene can be spliced in•Remove target sequence, insert new sequenceCompSci 100e9.5Linked lists, CDT and ADTAs an ADTA list is empty, or contains an element and a list( ) or (x, (y, ( ) ) )As a pictureAs a CDT (concrete data type) pojo: plain old Java objectpublic class Node{ Node p = new Node(); String value; p.value = “hello”;Node next; p.next = null;};p0CompSci 100e9.6Building linked listsAdd words to the front of a list (draw a picture)Create new node with next pointing to list, reset start of list public class Node { String value; Node next; Node(String s, Node link){ value = s; next = link; } }; // … declarations here Node list = null; while (scanner.hasNext()) { list = new Node(scanner.next(), list); }What about adding to the end of the list?CompSci 100e9.7Dissection of add-to-frontList initially emptyFirst node has first wordEach new word causes new node to be createdNew node added to frontRhs of operator = completely evaluated before assignmentlistAlist = new Node(word,list);B Node(String s, Node link) { info = s; next = link;}listCompSci 100e9.8Standard list processing (iterative)Visit all nodes once, e.g., count them or process thempublic int size(Node list){ int count = 0; while (list != null) { count++; list = list.next; } return count; }What changes in code if we generalize what process means?Print nodes?Append “s” to all strings in list?CompSci 100e9.9Standard list processing (recursive)Visit all nodes once, e.g., count them public int recsize(Node list) { if (list == null) return 0; return 1 + recsize(list.next); }Base case is almost always empty list: null pointerMust return correct value, perform correct actionRecursive calls use this value/state to anchor recursionSometimes one node list also used, two “base” casesRecursive calls make progress towards base caseAlmost always using list.next as argumentCompSci 100e9.10Recursion with picturesCounting recursivelyint recsize(Node list){ if (list == null) return 0; return 1 + recsize(list.next);}recsize(Node list)return 1+recsize(list.next)recsize(Node list)return 1+recsize(list.next)recsize(Node list)return 1+recsize(list.next)recsize(Node list)return 1+recsize(list.next)System.out.println(recsize(ptr));ptrCompSci 100e9.11Recursion and linked listsPrint nodes in reverse orderPrint all but first node and…•Print first node before or after other printing? public void print(Node list) { if (list != null) { } }print(list.next);System.out.println(list.info);System.out.println(list.info); print(list.next);CompSci 100e9.12Binary TreesLinked lists: efficient insertion/deletion, inefficient searchArrayList: search can be efficient, insertion/deletion notBinary trees: efficient insertion, deletion, and searchtrees used in many contexts, not just for searching, e.g., expression treessearch in O(log n) like sorted arrayinsertion/deletion O(1) like list, once location found!binary trees are inherently recursive, difficult to process trees non-recursively, but possible •recursion never required, often makes coding simplerCompSci 100e9.13From doubly-linked lists to binary treesInstead of using prev and next to point to a linear arrangement, use them to divide the universe in halfSimilar to binary search, everything less goes left, everything greater goes rightHow do we search?How do we insert?“llama”“tiger”“monkey”“jaguar”“elephant”“giraffe”“pig”“hippo”“leopard”“koala”“koala”“koala”“koala”“koala”CompSci 100e9.14Basic tree definitionsBinary tree is a structure:emptyroot node with left and right subtreesterminology: parent, children, leaf node, internal node, depth, height, path•link from node N to M then N is parent of M–M is child of N•leaf node has no children–internal node has 1 or 2 children•path is sequence of nodes, N1, N2, … Nk–Ni is parent of Ni+1–sometimes edge instead of node•depth (level) of node: length of root-to-node path–level of root is 1 (measured in nodes)•height of node: length of longest node-to-leaf path–height of tree is height of rootTrees can have many shapes: short/bushy, long/stringyIf height is h , how many nodes in tree?ABEDFCGCompSci 100e9.15A TreeNode by any other name…What does this look like?What does the picture look like?public class TreeNode{ TreeNode left; TreeNode right; String info; TreeNode(String s, TreeNode llink, TreeNode rlink){ info = s; left = llink;


View Full Document

Duke CPS 100E - Lecture

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Test 1

Test 1

7 pages

Load more
Download Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?