[CSUSB] >> [CompSci] >> [Dick Botting] >> [CS330 Course Materials] >> [Slides] >> 14
[Index] || [Contents] || [Grades] Mon Nov 11 15:41:50 PST 2002


    Notes on Trees


    The idea of tree is more useful than the data structures that implement the idea. You need to be able to talk, read, write, understand, and think about trees. You need to learn the language of trees!


    All modern operating systems organize the files as a tree (with some added "shortcuts", "links", or "aliases"). A folder in Mac/Windows is a node in a tree. The files are leafs in the tree.

    Family trees.


    Inheritance in Java.

    Parsing trees.

    Example in the Book

    Part of the project is to make the Animal Game work.


    A simple tree implementation

    More than good enough for the Animal Game.

    Here is a fairly typical but modern implementation of a Tree

    [ MyTree.h ]

    It includes the ability to take a Visitor to each node in the Tree in turn -- in the three different orders normally defined.


    In Book.

    distinguished: full tree, complete tree, and ballanced tree.

    Binary trees and powers of 2.

    Height greater than O( log[2](number nodes)).

    error on page 302. Height of ballanced tree. M[n] = f[n+3] -1, not f[n]+1. M[n] is still O(2**n) and so, if N=M[n] then n is O(log[2](n)).

    Result still hods

    Glossary Terms

    [ treeGlossary.html ]


    Code in book. Clever stuff. You may need this in practice. I don't expect you to recall and reproduce it in quizzes or exams.

    Traversals are used to implement iterators when data is stored in a tree.

    . . . . . . . . . ( end of section Notes on Trees) <<Contents | Index>>

    The Visitor Pattern

    An alternative to complex traversals implemented by iterators is the Object-Oriented Pattern called "Visitor". Here an object is given each node in turn and knows what to do with it. This is in an operation called 'visit'. All visitors have the same interface. Each class of visitor knows something special it can do: printout the data, add up the data, look for the maximum, multiply each item by a constant, etc.. . Because a Visitor is an object (and not just a function) it can have an initialized internal state. This makes Visitors a powerful technique.

    Here is a UML diagram showing three special kinds of Visitor and how they relate to a container. You can do this with any kind of of container.

    [Visitor in the UML]

    Here is the interface [ Visitor.h ]

      template <typename Data> class Visitor { //abstract
         virtual void visit(Data & d)=0;//abstract: visits data in a node

    Here is a simple test [ testVisitor.cpp ]

    Here is an example with trees etc [ treeVisitor.cpp ] and [ moreVisitor.cpp ]

    . . . . . . . . . ( end of section The Visitor Pattern) <<Contents | Index>>

Formulae and Definitions in Alphabetical Order