CSS 343: Notes from Lecture 3 (DRAFT)

Administrivia

Assignment 1

Running Off the End of a Structure

question: what does this do:

#include <iostream>
int main() {
   int i;
   int a[10];
   for (i = 0; i <= 10; ++i) {
      a[i] = 0;
   }
   cout << "initialization complete\n";
   return 0;
}
	

Interlude: A Random Musing About the Payload

Inside our list structure, we defined a string for the word data and an integer counter as part of the list node structure. For a library, this is not very general. Word and count are not a fundamental part of the list algorithms. There are several approaches of variable reasonableness to solve the problem (all of which have been applied):

  1. copy the list code and substitute the payload field(s) using your favorite text editor
  2. write a program (in any programming language, but some languages are easier than others) that will read input and write output like this:
    for each line of input
      if line does not contain "$$PAYLOAD"
        print line unchanged
      else
        print the part before "$$PAYLOAD"
        print the payload definition
        print the part after "$$PAYLOAD"
    	  
  3. use the C/C++ #define mechanism (but this is not fundamentally different different than case 1)
  4. use the preprocessor #include mechanism and some compiler flags to select a file that contains some definition for a Payload class
  5. use C++ inheritance
  6. use C++ templates
    template <typename T> struct Node {
       T* payload;  // or "T payload;" depending on your resource management
       Node<T>* Next;
    };
    	  

C++ templates are the preferred mechanism for solving exactly this sort of problem.

More About Sequential Processing

Nonsequential Processing

Interlude: (Mis-)Use of a Phone Book

Dictionaries

Trees and Binary Trees

A binary search tree (BST) is a binary tree with additional properties, so let's look at trees in general first. Trees express hierarchical relationships:

In a binary tree, a node has up to two children which we shall call left and right

Binary Search Tree

A BST is a binary tree and an ordering relation where, for each node, every node in its left subtree is less than the node and every node in the right subtree is greater than the node

Problem with Binary Search Tree