Mar 27
I will look over midterm projects and we'll discuss then. Comments/questions now?
Discuss the upcoming schedule for assignments. Move to a Tuesday due date?
trees
This week: look at the discussion in the text of trees - heaps in particular.
I would like to pick up the pace a bit, and do this chapter in about 1 week
now that I think you are familiar with the general ideas behind what we're doing.
A tree is a special case of a graph - which we'll do next.
Please read the chapter in the textbook on trees, making sure you understand
- what a tree is
- several different ways to represent trees in a computer program,
- several ways to traverse (i.e. do something to each node)
- aside: depth-first and breadth-first, two important ideas, are done in the next chapter on graphs
- heap - a priority queue (push anything, pop biggest)
- balanced tree - binary search
- summary of O() for put, get, search for various data structures
We'll walk through some of the text today in class.
Topics :
- trees as recursive data structures
- embedded lists
- objects with links
- tree traversal (pre-, in-, post-)
- heaps : balanced min or max tree
- ADT (alternating decision tree) : binary search
- AVL (inventor initials) : binary search tree (treated as dict)
I have several issues with their tree discussion
- 'branch' isn't defined in their vocabulary, but is used extensively in the API that follows. From context, it seems to be 'node name' ... which isn't what I would expect.
- Their 'insertBranch' operations are also not what I'd expect.
- Some of the passed things are trees; some are names of nodes. Which is which isn't clear from context.
- The "if" case in the insert seems to be redundant: if the list being replaced is empty, it could still just be added below. So why treat that as a special case?
- Why not just use a class for all this?
- No docstrings - bad example and practice.
- capitalization without python class - also bad practice.
In the section using classes, the "else" is still
not needed :
def insertLeft(self, newNodeName):
oldLeft = self.leftChild
self.LeftChild = BinaryTree(newNodeName)
self.leftChild.leftChild = oldLeft
Also, they don't create any good tools for displaying these trees.
See
http://en.wikipedia.org/wiki/Tree_traversal
for a clearer explanation of the traversal stuff.
In particular, all of these are part of
a "depth-first" traversal, and depending
on what you want to do, there may be
pre-, in-, and post- operations.
They have left breadth & depth traversals
until the graph section ... which seems like a funny choice to me.
aside : graphviz
Here's an example :
graph foo {
2 -- {3 5};
3 -- {e[label=8] 10};
5 -- {12 8};
e -- 9;
}
where the "e" node displays as "8" but is a different node than 8.