CS 225
Data Structures
- Sept. 26 – Trees
Wad ade Fag agen-Ulm lmschneid ider
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.
Data Structures
Wad ade Fag agen-Ulm lmschneid ider
Suppose we want to look through every element in our data structure:
8 2 5
Ø
Iterators encapsulated access to our data:
8 2 5
Ø
Next ListNode * index (x, y, z)
Every class that implements an iterator has two pieces:
Every class that implements an iterator has two pieces:
Iterators encapsulated access to our data:
8 2 5
Ø
::begin ::end
#include <list> #include <string> #include <iostream> struct Animal { std::string name, food; bool big; Animal(std::string name = "blob", std::string food = "you", bool big = true) : name(name), food(food), big(big) { /* nothing */ } }; int main() { Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); std::vector<Animal> zoo; zoo.push_back(g); zoo.push_back(p); // std::vector’s insertAtEnd zoo.push_back(b); for ( std::vector<Animal>::iterator it = zoo.begin(); it != zoo.end(); it++ ) { std::cout << (*it).name << " " << (*it).food << std::endl; } return 0; }
stlList.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <list> #include <string> #include <iostream> struct Animal { std::string name, food; bool big; Animal(std::string name = "blob", std::string food = "you", bool big = true) : name(name), food(food), big(big) { /* none */ } }; int main() { Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); std::vector<Animal> zoo; zoo.push_back(g); zoo.push_back(p); // std::vector’s insertAtEnd zoo.push_back(b); for ( const Animal & animal : zoo ) { std::cout << animal.name << " " << animal.food << std::endl; } return 0; }
stlList.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
for ( const TYPE & variable : collection ) { // ... } std::vector<Animal> zoo; … for ( const Animal & animal : zoo ) { std::cout << animal.name << " " << animal.food << std::endl; } 14 … 20 21 22
for ( const TYPE & variable : collection ) { // ... } std::vector<Animal> zoo; … for ( const Animal & animal : zoo ) { std::cout << animal.name << " " << animal.food << std::endl; } 14 … 20 21 22 std::multimap<std::string, Animal> zoo; … for ( const Animal & animal : zoo ) { std::cout << animal.name << " " << animal.food << std::endl; } … … 20 21 22
“The most important non-linear data structure in computer science.”
A tree is:
“Mario Family Line” <http://limitbreak.gameriot.com/blogs/ Caveat-Emptor/Mario-Family-Line>
We’ll focus on binary trees:
a path from the root
a b d g h j c e i f
We’ll focus on binary trees:
graph
a b d g h j c e i f
We’ll focus on binary trees:
a b d g h j c e i f
vertex labels in the tree (repeats allowed)?
a b d g h j c e i f
reasonable name.
have a parent but no sibling.
b d g h j c e i f a
reasonable name.
have a parent but no sibling.
b d g h j c e i f a
reasonable name.
have a parent but no sibling.
b d g h j c e i f a
reasonable name.
have a parent but no sibling.
b d g h j c e i f a
reasonable name.
have a parent but no sibling.
b d g h j c e i f a
A binary tree T is either:
X S 2 C 2 5
height(T): length of the longest path from the root to a leaf Given a binary tree T: height(T) =
A X S 2 C 2 5
A tree F is full if and only if: 1. 2.
A X S 2 C 2 5
A perfect tree P is: 1. 2.
A X S 2 C 2 5
Conceptually: A perfect tree for every level except the last, where the last level if “pushed to the left”. Slightly more formal: For any level k in [0, h-1], k has 2k nodes. For level h, all nodes are “pushed to the left”.
A X S 2 C 2 5 Y Z
A complete tree C of height h, Ch:
TL is __________ and TR is _________ OR TL is __________ and TR is _________
A X S 2 C 2 5 Y Z
Is every full tree complete? If every complete tree full?
A X S 2 C 2 5 Y Z