CS201 - Spring 2014 - Class 22

  • exercises

  • recursive DFS

  • search wrap-up
       - other applications:
          - game playing agents (e.g. chess)

  • tree terminology
       - nodes are the elements of a tree
       - edges connect the nodes
       - the root of the tree is the top of the tree
       - a subtree is any tree within the original tree (often including the original tree)
       - parent/child relationships
          - nodes have parents and children
          - the children of a node are the nodes directly under it
             - a node can have multiple children
             - for a binary tree, at most two children
          - the parent of a nodes is the the node directly above it in the tree
             - for a tree, a node only has one parent
             - all nodes except the root have a parent
       - ancestor/descendant relationship
          - a descendant of a node is any node under it in the tree (i.e. a child or a child or a child, etc.)
             - every node except the root is a descendant of the root
          - an ancestor of a node is any node above it in the tree (i.e. parent or a parent's parent, etc.)
             - similarly, the root is an ancestor of every node
       - siblings
          - two nodes are siblings if they have the same parent
       - leaf node
          - a leaf node is a node that does not have any children (i.e. at the bottom of the tree)
       - path
          - a path is a set of edges connecting nodes n and m
          - for a tree, we often say that a path must be the shortest set of edges from n to m
          - the length of the path is the number of edges along the path
       - height/depth of a node
          - the height of the tree is the maximum path from the root to any leaf
          - the depth of a node is the path length from the node to the root
             - the root is at depth 0
             - all children of the root at depth 1
             - etc.
       - degree of a tree
          - for now, we'll only talk about binary trees, but you can have n-ary trees
          - the degree of the tree is the maximum allowable number of children a node can have
       - tree types
          - simple tree (aka the loner)
             - a single node by itself
          - the twig
             - each node has one child
          - full tree
             - every node has 2 children except the leaves
          - complete tree
             - full binary tree except the leaves are filled in from left to right

       - tree properties
          - given a tree of height h
             - at most how many leaves does it have?
                - maximum occurs when the tree is full
                   - 2^h
                - in general, there are at most 2^d nodes at depth d
             - what is the maximum number of nodes?
                - the maximum number occurs when the tree is full
                   - bottom row has 2^h
                   - then 2^(h-1), 2^(h-2), ... , 1
                   - sum these up... 2^(h+1) - 1
             - what is the minimum number of nodes?
                - h+1, the twig
          - given a full tree, what is the height of the tree with respect to the number of nodes?
             n = 2^(h+1)-1
             log n = h+1 log 2
             h = O(log_2 n)

  • applications
       - expression trees
          - (2 + 3) * 4
          - ((4 + 2) * ((3 * 2) - 4)
          - 1 + 2 + 3 + 4 + 5
             - ambiguous!
       - geneology
       - huffman codes (compression)

  • look at BinaryTree code
       - similar idea to a linked list however, instead of just one "next" node we have a left and a right child
       - reinforces the idea that this is a recursive structure: a binary tree consists of a data item with left and right subtrees that are also binary trees
       - "data" stores the data at this node
       - "parent" references the parent
          - why do we keep track of our parent?
             - make some operations easier, e.g. depth
       - left and right keep track of the children
       - building a binary trees:
          - for now, just two constructors:
             - create a node with no children. When does this happen?
                - leaf node
             - create a node with children specified
                - internal node
                - where could we enforce the constraint that each node must contain two children?
                   - in the second constructor
          - how do we build a binary tree with this implementation?
             - from the bottom up!
          - ((4 + 2) * ((3 * 2) - 4)
          - look at buildSimpleTree method in BinaryTreeTraversal class