cs 225
play

CS 225 Data Structures Sept. 26 Trees Wad ade Fag agen-Ulm - PowerPoint PPT Presentation

CS 225 Data Structures Sept. 26 Trees Wad ade Fag agen-Ulm lmschneid ider It Iterators Suppose we want to look through every element in our data structure: 8 2 5 Iterators encapsulated access to our data: Cur. Location Cur.


  1. CS 225 Data Structures Sept. 26 – Trees Wad ade Fag agen-Ulm lmschneid ider

  2. It Iterators 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 Iterators Every class that implements an iterator has two pieces: 1. [Implementing Class]:

  5. It Iterators 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 #include <list> 1 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 #include <list> 1 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 }

  9. For Each and It 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 }

  10. For Each and It 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 }

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

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

  13. More 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 d e f g h i j

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

  15. More 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 d e f g h i j

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

  17. Tree Terminology • 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? • Which vertex has the fewest children ? e f d • 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

  18. Tree Terminology • 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? • Which vertex has the fewest children ? e f d • 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 • 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? • Which vertex has the fewest children ? e f d • 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 • 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? • Which vertex has the fewest children ? e f d • 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 • 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? • Which vertex has the fewest children ? e f d • 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. Binary ry Tree – Defined C A binary tree T is either: • S X OR A 2 • 2 5

  23. Tree Property: height 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) =

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

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

  26. Tree Property: complete 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

  27. Tree Property: complete 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 _________

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

Recommend


More recommend