What’s the Difference Here?

Slides:



Advertisements
Similar presentations
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CPS 100, Spring From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
CPS Wordcounting, sets, and the web l How does Google store all web pages that include a reference to “peanut butter with mustard”  What efficiency.
CPS Data processing example l Scan a large (~ 10 7 bytes) file l Print the 20 most frequently used words together with counts of how often they.
CPS 100, Spring Trees: no data structure lovelier?
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CPS 100, Spring From data to information l Data that’s organized can be processed  Is this a requirement?  What does “organized” means l Purpose.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
CompSci 100e 7.1 From doubly-linked lists to binary trees l Instead of using prev and next to point to a linear arrangement, use them to divide the universe.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
CPS Scoreboard l What else might we want to do with a data structure? l What are maps’ limitations? AlgorithmInsertionDeletionSearch Unsorted.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Search Trees (BST)
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string.
Compsci 100, Fall From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
CompSci 100e 7.1 Plan for the week l Review:  Boggle  Coding standards and inheritance l Understand linked lists from the bottom up and top-down  As.
(c) University of Washington20-1 CSC 143 Java Trees.
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
From doubly-linked lists to binary trees
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Non Linear Data Structure
Printing a search tree in order
PFTFBH Binary Search trees Fundamental Data Structure
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
Data Structures Binary Trees 1.
COSC160: Data Structures Binary Trees
Plan for the Week Review Big-Oh
From data to information
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
PFTWBH More examples of recursion and alternatives
Week 6 - Wednesday CS221.
CMSC 341 Introduction to Trees.
Compsci 201 Trees, Tries, Tradeoffs
ITEC 2620M Introduction to Data Structures
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
TREES General trees Binary trees Binary search trees AVL trees
Find in a linked list? first last 7  4  3  8 NULL
common code “abstracted out” same code written several times: don’t!
Binary Search Trees.
CS6045: Advanced Algorithms
Binary Trees, Binary Search Trees
Trees Chapter 10.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
8.2 Tree Traversals Chapter 8 - Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

What’s the Difference Here? How does find-a-track work? Fast forward?

Contrast 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?)

What about LinkedList? Why is access of Nth element linear time? Why is adding to front constant-time O(1)? front

Linked 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 sequence

Linked 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 object public class Node { Node p = new Node(); String value; p.value = “hello”; Node next; p.next = null; }; p

Building 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?

Dissection 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 assignment list list A list = new Node(word,list); B Node(String s, Node link) { info = s; next = link;}

Standard list processing (iterative) Visit all nodes once, e.g., count them or process them public 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?

Standard 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 argument

Recursion with pictures recsize(Node list) return 1+ recsize(list.next) Counting recursively int 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) ptr recsize(Node list) return 1+ recsize(list.next) System.out.println(recsize(ptr));

Recursion 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);

Binary 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 simpler

From 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? “koala” “llama” “tiger” “monkey” “jaguar” “elephant” “giraffe” “pig” “hippo” “leopard” “koala” “koala” “koala” “koala”

Basic 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? A B E D F C G

A 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; right = rlink; } “llama” “tiger” “giraffe”

Printing a search tree in order When is root printed? After left subtree, before right subtree. void visit(TreeNode t) { if (t != null) { visit(t.left); System.out.println(t.info); visit(t.right); } Inorder traversal Big-Oh? What’s the big-Oh? “llama” “tiger” “monkey” “jaguar” “elephant” “giraffe” “pig” “hippo” “leopard”

Tree traversals Different traversals useful in different contexts Inorder prints search tree in order Visit left-subtree, process root, visit right-subtree Preorder useful for reading/writing trees Process root, visit left-subtree, visit right-subtree Postorder useful for destroying trees Visit left-subtree, visit right-subtree, process root “llama” “tiger” “monkey” “jaguar” “elephant” “giraffe”

Tree functions Compute height of a tree, what is complexity? int height(Tree root) { if (root == null) return 0; else { return 1 + Math.max(height(root.left), height(root.right) ); } Modify function to compute number of nodes in a tree, does complexity change? What about computing number of leaf nodes?

Balanced Trees and Complexity A tree is height-balanced if Left and right subtrees are height-balanced Left and right heights differ by at most one boolean isBalanced(Tree root) { if (root == null) return true; return isBalanced(root.left) && isBalanced(root.right) && Math.abs(height(root.left) – height(root.right)) <= 1; }

What is complexity? Assume trees are “balanced” in analyzing complexity Roughly half the nodes in each subtree Leads to easier analysis How to develop recurrence relation? What is T(n)? What other work is done? How to solve recurrence relation Plug, expand, plug, expand, find pattern A real proof requires induction to verify correctness