CS201 - Spring 2014 - Class 23

  • exercises

  • admin
       - assignment 8
       - final exam will be self-scheduled

  • java tidbit for the day: ternary operator
       - what is an operator?
          - operators are special built-in functions that perform operate on built-in data types (int, double, long, String)
          - e.g. mathematical operators: +, -, *, /, %
       - say we have two variables "first" and "second" and we want to assign the value of the larger of the two to a third variable "largest". How would we do it?
       - one way:
          if( first > second ){
             largest = first;
          }else{
             largest = second;
          }
       - another way:
          largest = first > second ? first : second;
       - the ternary operator takes three parts:
          <condition> ? <expression1> : <expression2>
          
          - if the condition is true, the value of the ternary expression is <expression1>
          - if the condition is false, the value of the ternary expression is <expression2>
          - you can simplify many statements using this!
       
  • building trees top down
       - Using the constructors, we can build the trees from the bottom up
       - In some cases, it's necessary to be able to build tree top down. Similarly, it can be useful to modify a tree
       - Write the setLeft and setRight methods
          - should take a BinaryTree as a parameter
          - don't forget to make sure to keep ALL data up to data (left, right, parent, etc.)
       - look at the setLeft and setRight methods in the BinaryTree class in BinaryTree code

  • a few other basic methods
       - isLeaf
          - check if left AND right are null
       - isRoot
          - check if parent == null

  • tree traversal
       - we can "traverse" a tree by visiting all of the nodes in the tree
       - why would we want to traverse all of the nodes?
          - we may want to ask a question about the data, e.g. search
          - we may want to print out the values in the tree in some order
          - we may want to process the values in the tree in some order
       - look at the four different traversal methods in BinaryTreeTraversal class in BinaryTree code
          - how do these traverse the data?
       - how do these relate to BFS and DFS?

  • some other tree methods
       - implement (can be found in BinaryTree code)
          - height
             - running time?
                - O(n)
          - depth
             - running time?
                - O(h)
          - size
             - running time?
                - O(n)
          - search
             - running time?
                - O(n)
       - isFull()
          - a tree is full if:
             - it's a leaf
             - both of it's children are full AND both of it's children are at the same height
          - look at isFullSlow() method
             - running time?
                - O(n h)