cs 225
play

CS 225 Data Structures Se Sept. 26 26 Tr Trees Wa Wade - PowerPoint PPT Presentation

CS 225 Data Structures Se Sept. 26 26 Tr Trees Wa Wade Fagen-Ul Ulmschneider It Iter erators Suppose we want to look through every element in our data structure: 8 2 5 Iterators encapsulated access to our data: Cur.


  1. CS 225 Data Structures Se Sept. 26 26 – Tr Trees Wa Wade Fagen-Ul Ulmschneider

  2. It Iter erators Suppose we want to look through every element in our data structure: Ø 8 2 5

  3. Iterators encapsulated access to our data: Cur. Location Cur. Data Next Ø ListNode * 8 2 5 index (x, y, z)

  4. It Iter erators Every class that implements an iterator has two pieces: 1. [Implementing Class]:

  5. It Iter erators Every class that implements an iterator has two pieces: 2. [Implementing Class’ Iterator]: • Must have the base class: std::iterator • std::iterator requires us to minimally implement:

  6. Iterators encapsulated access to our data: ::begin ::end Ø 8 2 5

  7. stlList.cpp 1 #include <list> 2 #include <string> 3 #include <iostream> 4 5 struct Animal { 6 std::string name, food; 7 bool big; 8 Animal(std::string name = "blob", std::string food = "you", bool big = true) : 9 name(name), food(food), big(big) { /* nothing */ } 10 }; 11 12 int main() { 13 Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); 14 std::vector<Animal> zoo; 15 16 zoo.push_back(g); 17 zoo.push_back(p); // std::vector’s insertAtEnd 18 zoo.push_back(b); 19 20 for ( std::vector<Animal>::iterator it = zoo.begin(); it != zoo.end(); it++ ) { 21 std::cout << (*it).name << " " << (*it).food << std::endl; 22 } 23 24 return 0; 25 }

  8. stlList.cpp 1 #include <list> 2 #include <string> 3 #include <iostream> 4 5 struct Animal { 6 std::string name, food; 7 bool big; 8 Animal(std::string name = "blob", std::string food = "you", bool big = true) : 9 name(name), food(food), big(big) { /* nothing */ } 10 }; 11 12 int main() { 13 Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); 14 std::vector<Animal> zoo; 15 16 zoo.push_back(g); 17 zoo.push_back(p); // std::vector’s insertAtEnd 18 zoo.push_back(b); 19 20 for ( auto it = zoo.begin(); it != zoo.end(); it++ ) { 21 std::cout << (*it).name << " " << (*it).food << std::endl; 22 } 23 24 return 0; 25 }

  9. stlList.cpp 1 #include <list> 2 #include <string> 3 #include <iostream> 4 5 struct Animal { 6 std::string name, food; 7 bool big; 8 Animal(std::string name = "blob", std::string food = "you", bool big = true) : 9 name(name), food(food), big(big) { /* none */ } 10 }; 11 12 int main() { 13 Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); 14 std::vector<Animal> zoo; 15 16 zoo.push_back(g); 17 zoo.push_back(p); // std::vector’s insertAtEnd 18 zoo.push_back(b); 19 20 for ( const Animal & animal : zoo ) { 21 std::cout << animal.name << " " << animal.food << std::endl; 22 } 23 24 return 0; 25 }

  10. Fo For Each and Iterators for ( const TYPE & variable : collection ) { // ... } 14 std::vector<Animal> zoo; … … 20 for ( const Animal & animal : zoo ) { 21 std::cout << animal.name << " " << animal.food << std::endl; 22 }

  11. Fo For Each and Iterators for ( const TYPE & variable : collection ) { // ... } 14 std::vector<Animal> zoo; … … 20 for ( const Animal & animal : zoo ) { 21 std::cout << animal.name << " " << animal.food << std::endl; 22 } … std::multimap<std::string, Animal> zoo; … … 20 for ( const Animal & animal : zoo ) { 21 std::cout << animal.name << " " << animal.food << std::endl; 22 }

  12. Tr Trees “The most important non-linear data structure in computer science.” - David Knuth, The Art of Programming, Vol. 1 A tree is: • •

  13. A A Rooted Tree “Mario Family Line” <http://limitbreak.gameriot.com/blogs/ Caveat-Emptor/Mario-Family-Line>

  14. Mor More Sp Specific Trees We’ll focus on binary trees : • A binary tree is rooted – every node can be reached via a path from the root a c b e f d g h i j

  15. Mor More Sp Specific Trees We’ll focus on binary trees : • A binary tree is acyclic – there are no cycles within the graph a c b e f d g h i j

  16. More Sp Mor Specific Trees We’ll focus on binary trees : • A binary tree contains two or fewer children – where one is the “left child” and a one is the “right child”: c b e f d g h i j

  17. Tree Terminology gy • What’s the longest English word you can make using the vertex labels in the tree (repeats allowed)? a c b e f d g h i j

  18. Tree Terminology gy • Find an edge that is not on the longest path in the tree. Give that edge a reasonable name. • One of the vertices is called the root of the tree. Which one? a • Make an “word” containing the names of the vertices that have a parent but no sibling . c b • How many parents does each vertex have? e f d • Which vertex has the fewest children ? • Which vertex has the most ancestors ? g • Which vertex has the most descendants ? • List all the vertices is b’s left subtree . h • List all the leaves in the tree. i j

  19. Tree Terminology gy • Find an edge that is not on the longest path in the tree. Give that edge a reasonable name. • One of the vertices is called the root of the tree. Which one? a • Make an “word” containing the names of the vertices that have a parent but no sibling . c b • How many parents does each vertex have? e f d • Which vertex has the fewest children ? • Which vertex has the most ancestors ? g • Which vertex has the most descendants ? • List all the vertices is b’s left subtree . h • List all the leaves in the tree. i j

  20. Tree Terminology gy • Find an edge that is not on the longest path in the tree. Give that edge a reasonable name. • One of the vertices is called the root of the tree. Which one? a • Make an “word” containing the names of the vertices that have a parent but no sibling . c b • How many parents does each vertex have? e f d • Which vertex has the fewest children ? • Which vertex has the most ancestors ? g • Which vertex has the most descendants ? • List all the vertices is b’s left subtree . h • List all the leaves in the tree. i j

  21. Tree Terminology gy • Find an edge that is not on the longest path in the tree. Give that edge a reasonable name. • One of the vertices is called the root of the tree. Which one? a • Make an “word” containing the names of the vertices that have a parent but no sibling . c b • How many parents does each vertex have? e f d • Which vertex has the fewest children ? • Which vertex has the most ancestors ? g • Which vertex has the most descendants ? • List all the vertices is b’s left subtree . h • List all the leaves in the tree. i j

  22. Tree Terminology gy • Find an edge that is not on the longest path in the tree. Give that edge a reasonable name. • One of the vertices is called the root of the tree. Which one? a • Make an “word” containing the names of the vertices that have a parent but no sibling . c b • How many parents does each vertex have? e f d • Which vertex has the fewest children ? • Which vertex has the most ancestors ? g • Which vertex has the most descendants ? • List all the vertices is b’s left subtree . h • List all the leaves in the tree. i j

  23. Bi Binary Tree – De Defined C A binary tree T is either: • S X OR A 2 • 2 5

  24. Tree Property: heigh ght C height(T) : length of the longest path from the root to a leaf S X Given a binary tree T: A 2 2 5 height(T) =

  25. Tr Tree Property: full C A tree F is full if and only if: 1. S X 2. A 2 2 5

  26. Tr Tree Property: perfect C A perfect tree P is: 1. S X 2. A 2 2 5

  27. Tree Property: complete Tr C Conceptually : A perfect tree for every level except the last, where the last level if “pushed to the left”. S X Slightly more formal : For any level k in A 2 2 5 [0, h-1], k has 2 k nodes. For level h, all nodes are “pushed to the left”. Y Z

  28. Tree Property: complete Tr C A complete tree C of height h , C h : 1. C -1 = {} 2. C h (where h>0) = {r, T L , T R } and either: S X T L is __________ and T R is _________ A 2 2 5 OR Y Z T L is __________ and T R is _________

  29. Tr Tree Property: complete C Is every full tree complete ? S X A 2 2 5 If every complete tree full ? Y Z

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend