CPSC 221

Trees

Today

Announcements

  • Homework Two is due tonight; PA2 comes out tonight
  • Examlet 4 runs Thursday to Sunday — book your seat on PrairieTest
  • Starting this week, labs include office hours


Where we’re going

  • Tree vocabulary
  • Recursive definitions
  • Implementation intro

Trees

“… most important nonlinear structure[s] in computer science.” — Donald Knuth, The Art of Computer Programming, Vol 1

A tree: ______________________________

1 2 3 4 5 6

Family trees · organization charts · classification trees (what kind of flower is this? is this mushroom poisonous?) · file directories · non-recursive call graphs

We’ll study more specific trees first:

Tree terminology

  • One of the vertices is called the root of the tree. Guess which one it is.
  • Make an English word containing the names of the vertices that have a parent but no sibling.
  • How many parents does each vertex have?
  • Which vertex has the fewest children?
  • Which vertex has the most ancestors? descendants?
  • What is d’s depth? What is d’s height? What is d’s level?
  • List all the vertices in b’s left subtree.
  • List all the leaves in the tree.
a b c d e f g h i j

Tree terminology (for your reference)

  • root: the single node with no parent
  • leaf: a node with no children
  • child: a node pointed to by me
  • parent: the node that points to me
  • sibling: another child of my parent
  • ancestor: my parent or my parent’s ancestor
  • descendant: my child or my child’s descendant
  • subtree: a node and its descendants
  • depth of node x: number of edges on the path from the root to x
  • height of node x: number of edges on the longest path from x to a leaf
  • level k: all the nodes at depth k
a b c d e f g h i j

Branching: \(d\)-ary trees (binary if \(d = 2\))

+ * 4 2 − 3 5

A d-ary tree T is either

  •  

OR

  •  

Full \(d\)-ary tree:


Perfect \(d\)-ary tree:


Complete \(d\)-ary tree:


Binary tree height

height(r) — length of the longest path from root r to a leaf

+ * 4 2 − 3 5

Given a binary tree T, write a recursive definition of the height of T, height(T):

Number of nodes in a perfect tree of height h, N(h):

A rooted tree

Contemplating implementation

root 5 3 9 1 4 6

Tree ADT:

  • insert
  • remove
  • traverse
template<class T>
class tree {
public:  // discuss later
private:
    struct Node {
        T data;
        Node * left;
        Node * right;
    };
    Node * root;  // + more?
};

Theorem

Theorem: Our implementation of an \(n\) item binary tree has _______ null pointers.

root 5 3 9 1 4 6

Practice 1

  1. Draw a FULL tree with 9 nodes. Is it the same as your neighbour’s?

Practice 2

  1. Draw a COMPLETE tree with 12 nodes. Is it the same as your neighbour’s?

Practice 3

3a) Give an upper bound for the number of nodes in a tree of height h:



3b) Give a lower bound for the height of a tree containing n nodes:

Practice 4

  1. Give upper and lower bounds for the number of nodes in a complete tree of height h: