1 / 24

CSE 326: Data Structures Lecture #7 Branching Out

CSE 326: Data Structures Lecture #7 Branching Out. Steve Wolfman Winter Quarter 2000. Today’s Outline. Talking about HW #2 Some Tree Review Binary Trees Dictionary ADT Binary Search Trees. HW #2. Solutions are online www.cs.washington.edu/326 navigate to assignments Good work!

lambj
Download Presentation

CSE 326: Data Structures Lecture #7 Branching Out

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 326: Data StructuresLecture #7Branching Out Steve Wolfman Winter Quarter 2000

  2. Today’s Outline • Talking about HW #2 • Some Tree Review • Binary Trees • Dictionary ADT • Binary Search Trees

  3. HW #2 • Solutions are online • www.cs.washington.edu/326 • navigate to assignments • Good work! • If you got 5/8 or less, come talk to us • If you don’t understand anything on the quiz, come talk to us or e-mail us (owner-cse326@cs) • understand the solution even if you got it right on the quiz! • Problems #3 and #8

  4. Similar store collections of elements all elements of the same type support an inserting operator and a removing operator define a structured ordering on the removal of elements (I.e., not random) Different a priority queue is not a queue! very different orderings on elements pqueues require comparisons on the elements stacks and queues are highly efficient (pqueues slightly less so) theoretical computational power (pqueues and queues beat stacks Problem #8: How is a Queue like a Stack like a Priority Queue?

  5. Tree Calculations t • Find the longest undirected path in a tree • Might be: • the longest path in one of the subtrees • the longest path that goes through t

  6. Tree Calculations Example A B C D E F G H I J K L L M N

  7. Data left pointer right pointer Binary Trees • Binary tree is • a root • left subtree (maybe empty) • right subtree (maybe empty) • Properties • max # of leaves: • max # of nodes: • average depth for N nodes: • Representation: A B C D E F G H I J

  8. A F E D C B left pointer left pointer left pointer left pointer left pointer left pointer right pointer right pointer right pointer right pointer right pointer right pointer Representation A B C D E F

  9. Stack Push Pop Queue Enqueue Dequeue List Insert Remove Find Priority Queue Insert DeleteMin What We Can Do So Far What’s wrong with Lists? Remember decreaseKey?

  10. Dictionary operations create destroy insert find delete Stores values associated with user-specified keys values may be any (homogenous) type keys may be any (homogenous) comparable type Zasha interesting ID, but not enough ooomph! Bone More oomph, less high scoring Scrabble action Wolf the perfect mix of oomph and Scrabble value Dictionary ADT insert • Darth • - formidable find(Wolf) • Wolf • - the perfect mix of oomph • and Scrabble value

  11. Dictionary operations create destroy insert find delete Stores keys keys may be any (homogenous) comparable quickly tests for membership Berner Whippet Alsatian Sarplaninac Beardie Sarloos Malamute Poodle Search ADT insert • Min Pin find(Wolf) NOT FOUND

  12. A Modest Few Uses • Arrays • Sets • Dictionaries • Router tables • Page tables • Symbol tables • C++ Structures

  13. Desiderata • Fast insertion • runtime: • Fast searching • runtime: • Fast deletion • runtime:

  14. Naïve Implementations insert find delete • Linked list • Unsorted array • Sorted array so close!

  15. Binary tree property each node has  2 children result: storage is small operations are simple average depth is small Search tree property all keys in left subtree smaller than root’s key all keys in right subtree larger than root’s key result: easy to find any given key Binary Search Tree Dictionary Data Structure 8 5 11 2 6 10 12 4 7 9 14 13

  16. Getting to Know BSTs Example and Counter-Example 15 8 4 8 5 11 1 7 11 2 6 10 18 7 3 4 15 20 21 BINARY SEARCH TREE NOT A BINARY SEARCH TREE

  17. Getting to Know All About BSTs In Order Listing 10 5 15 2 9 20 17 7 30 In order listing: 25791015172030

  18. Getting to Like BSTs Finding a Node Node *& find(Comparable key, Node *& root) { if (root == NULL) return root; else if (key < root->key) return find(key, root->left); else if (key > root->key) return find(key, root->right); else return root; } 10 5 15 2 9 20 17 7 30 runtime:

  19. Getting to Hope BSTs Like You Iterative Find Node * find(Comparable key, Node * root) { while (root != NULL && root->key != key) { if (key < root->key) root = root->left; else root = root->right; } return root; } 10 5 15 2 9 20 17 7 30 Look familiar?

  20. Insert void insert(Comparable key, Node * root) { Node *& target(find(key, root)); assert(target == NULL); target = new Node(key); } 10 5 15 2 9 20 17 7 30 runtime:

  21. Digression: Value vs. Reference Parameters • Value parameters (Object foo) • copies parameter • no side effects • Reference parameters (Object & foo) • shares parameter • can affect actual value • use when the value needs to be changed • Const reference parameters (const Object & foo) • shares parameter • cannot affect actual value • use when the value is too intricate for pass-by-value

  22. BuildTree for BSTs • Suppose the data 1, 2, 3, 4, 5, 6, 7, 8, 9 is inserted into an initially empty BST: • in order • in reverse order • median first, then left median, right median, etc.

  23. Analysis of BuildTree • Worst case: O(n2) as we’ve seen • Average case assuming all orderings equally likely:

  24. Find minimum Findmaximum Bonus: FindMin/FindMax 10 5 15 2 9 20 17 7 30

More Related