Linear vs. non-linear
collections of objects

    linear
non-linear
access of elements is sequential access along branching paths
unique first element may be many "first" elements
unique last element may be many "last" elements
each interior element has a unique successor each element may have multiple successors
one-dimensional two-dimensional (or more)
e.g.: arrays
vectors
linked lists
stacks
queues
e.g.: trees
graphs

Trees

  • made up of nodes and arcs (or branches)
  • organization flows along arcs from root to outer nodes
  • there is a single unique path along the arcs from the root to any particular node

  • A note on graphs
  • made up of vertices and edges

  • may have more than one flow into a node



  • Tree terminology
  • each tree originates at a unique starting node called the root
  • an empty tree contains no nodes -
    its root is simply a "null pointer"
  • each node is the parent of zero or more nodes called its children
  • nodes with no children are called leaves
  • nodes that are neither the root nor a leaf are called internal nodes


  • Tree genealogy
  • children of a node and children of these children are called its descendants
  • the parents and grandparents of a node are called its ancestors
  • each non-root node has a unique parent
  • to go from parent node to its children and other descendants, follow a path (of arcs)


  • Subtrees

  • each node in the tree is the root of a subtree

  • a subtree is defined by its root and the descendants of its root



  • Degree of a tree
  • a tree's degree is the maximum number of children possessed by any node


  • tree of degree 3


    tree of degree 2
    Level of a node
    the level of a node is defined by its path: it's a count of the number of arcs between the node and the root

    Depth or height of a tree
  • the depth or height of the tree can be characterized as:
  • - the maximum level of any node in the tree
    OR
    - the length of the longest path from the root to a node

  • Density of a tree

    Two extremes of tree density


    Advantages of higher density

    Moral: State your assumptions!!

    Examples of trees

    Binary trees

  • trees of degree 2
  • each node restricted to 0, 1, or 2 children

  • Complete binary trees

    =>maximal number of leaves with minimal path length


    Properties of full binary trees


    Properties of complete binary trees



    B>Properties of path length


    Consider a tree where M , the number of nodes in the tree, is 10,000

    Implementing binary trees

  • built with nodes
  • access to the tree is via a pointer to the root node
  • each node contains
  • leaf nodes have null left and right pointers

  • example: the empty tree
  • Example:



    Choices for implementation

    Manipulating binary trees

    node <int> *root, *lKid, *rKid;

    lKid = new node<int> (20);
    rKid = new node<int> (30);
    root = new node<int> (10,lKid,rKid);

    lKid = new node<int> (40);
    rKid = new node<int> (50);
    node<int> *ip = root->left();
    ip->left(lKid);
    ip->right(rKid);

    Tree traversals

    a total of six different organized orderings

    NLR LNR LRN NRL RNL RLN

    Traversal types