CPS 100, Spring 2010 10.1 Trees: no data structure lovelier?

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
© 2004 Goodrich, Tamassia Binary Search Trees   
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Data Structures – Binary Tree
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.
CompSci Binary Trees l Linked lists: efficient insertion/deletion, inefficient search  ArrayList: search can be efficient, insertion/deletion.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
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.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
CPS Scoreboard l What else might we want to do with a data structure? l What are maps’ limitations? AlgorithmInsertionDeletionSearch Unsorted.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
David Stotts Computer Science Department UNC Chapel Hill.
Data Structures Using Java1 Chapter 10 Binary Trees.
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)
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
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.
1 CMSC 341 Introduction to Trees Textbook sections:
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CompSci 100e 7.1 Plan for the week l Recurrences  How do we analyze the run-time of recursive algorithms? l Setting up the Boggle assignment.
From doubly-linked lists to binary trees
Printing a search tree in order
Recursive Objects (Part 4)
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.
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
What’s the Difference Here?
Week 6 - Wednesday CS221.
Trees.
Tree.
Compsci 201 Trees, Tries, Tradeoffs
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Chapter 21: Binary Trees.
common code “abstracted out” same code written several times: don’t!
Binary Trees, Binary Search Trees
Binary SearchTrees [CLRS] – Chap 12.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

CPS 100, Spring Trees: no data structure lovelier?

CPS 100, Spring Plan of Action: re trees l Trees from top to bottom  Why trees are useful, tradeoffs in types of trees  How trees are implemented  Where trees are used, data structures and algorithms l We’ll concentrate on binary trees  A tree can be empty  A tree consists of a (root (subtree) (subtree))  Analyzing tree functions with recurrences l Other types of trees from a 30,000 ft view

CPS 100, Spring From doubly-linked lists to binary trees l Re-imagine prev/next, no longer linear  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”

CPS 100, Spring Binary Trees l Search and insert: toward the best of both worlds  Linked list: efficient insert/delete, inefficient search  ArrayList: efficient (binary) search, but shift l Binary trees: efficient insert, delete, and search  trees used in many contexts, not just for searching, Game trees, collisions, … Cladistics, genomics, quad trees, …  search in O(log n) like sorted array Average case, worst case can be avoided!  insertion/deletion O(1) like list, once location found

CPS 100, Spring A TreeNode by any other name… l 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 ”

CPS 100, Spring Printing a search tree in order l 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); } l Inorder traversal “llama” “tiger” “monkey” “jaguar” “elephant” “ giraffe ” “pig” “hippo” “leopard”

CPS 100, Spring Tree traversals l 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 ”

CPS 100, Spring Barbara Liskov l First woman to earn PhD from compsci dept  Stanford l Turing award in 2008 l OO, SE, PL “It's much better to go for the thing that's exciting. But the question of how you know what's worth working on and what's not separates someone who's going to be really good at research and someone who's not. There's no prescription. It comes from your own intuition and judgment.”

CPS 100, Spring Basic tree terminology l Binary tree is a structure:  empty  root node with left and right subtrees l Tree Terminology  parent and child : A is parent of B, E is child of B  leaf node has no children, internal node has 1 or 2 children  path is sequence of nodes (edges), N 1, N 2, … N k N i is parent of N i+1  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 A B ED F C G

CPS 100, Spring Tree functions l 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) ); } l Modify function to compute number of nodes in a tree, does complexity change?  What about computing number of leaf nodes?

CPS 100, Spring Balanced Trees and Complexity l 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; }

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

CPS 100, Spring Recurrences l If T(n) = T(n-1) + O(1)… where do we see this? T(n) = T(n-1) + O(1) true for all X so, T(n-1) = T(n-2)+ O(1) T(n) = [T(n-2) + 1] + 1 = T(n-2) + 2 = [T(n-3) + 1] + 2 = T(n-3) + 3 l True for 1, 2, so euraka! We see a pattern T(n) = T(n-k) + k, true for all k, let n=k T(n) = T(n-n) + n = T(0) + n = n l We could solve, we could prove, or remember!

CPS 100, Spring Recognizing Recurrences l Solve once, re-use in new contexts  T must be explicitly identified  n must be some measure of size of input/parameter T(n) is for quicksort to run on an n-element array T(n) = T(n/2) + O(1) binary search O( ) T(n) = T(n-1) + O(1) sequential search O( ) T(n) = 2T(n/2) + O(1) tree traversal O( ) T(n) = 2T(n/2) + O(n) quicksort O( ) T(n) = T(n-1) + O(n) selection sort O( ) l Remember the algorithm, re-derive complexity n log n n log n n n2n2