Lecture 17:  Trees

Announcements

·      programming assignment #3 due midnight tonight

·      homework #6 is due at noon on Thursday

·      next midterm on Tuesday April 5 – conflicts?

 

conceptual picture

A

 
 

 

 

 

 

 


terminology

·      each circle is a node

·      each connecting arrow is an edge

·      the node with no incoming edges (A) is the root

·      the nodes without outgoing edges (D,I,E,X) are leaves

note:  an upside down tree!

every non-empty tree has

one root,

one or more leaves

if an edge connects node A to node B,

        node A is the parent of node B

        node B is the child of node A

in a tree, the root has no parent

the root has no parent

all other nodes have exactly one parent

 

 

                              is not a tree!     (D has two parents)

 

 

 


More terminology

·      a path in a tree is a sequence of connected nodes

I

 
e.g.,

 

 

 

 

 


·      the length of the path is the number of nodes

3                     2                     1

·      the height of a tree is the length of the longest path from the root to a leaf

·      the depth of a node is the length of the path from the root

e.g.,   tree depths: A     1

                              B     2

                              D     3

·      The subtrees of a node n are the trees rooted at each of n's children

 

 

 


A's 3 subtrees

 

 

                                                      C's 2 subtrees:  E and X

 

Binary Trees

a special kind of tree in which

·      no node has more than two children, and

·      each child is either a left child or a right child

 

 

 

 

 

 

 

 


all binary trees – all different

 

Representing Binary Trees

class BinaryTreenode {

  public Object data;  // can be private, get/set

  public BinaryTreenode left, right;

}

 

example:                            You try:  fill in the field values

 


                                                                               "A"

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Trees are great for resursive methods!

example:  recursive definition of height for a binary tree

·      an empty tree has height = 0

·      a non-empty tree has height

       = 1 + max (height left subtree, height right subtree)

 

You try:

        int height ( BinaryTreenode T ) {

      //return height of T

solution:

      if ( T == null ) return 0;

      return (1+max(height(T.left), height(T.right));

    }

    int max (int j, int k) {

      if (j>k) return j;

      return k;

    }

Representing General Trees (arbitrary number of children)

·      no fixed or predefined max. number of children per node

·      can't have one field per child node

·      instead use a list of children

class Treenode {

    public Object data;

    private List kids;

    // insert constructor here

    // insert methods to add, get, remove kids here

}

·      the items in the list will be Treenodes

·      initialize the List to an ArrayList or LinkedList  - why?

methods add(), get(pos), remove(pos) are useful

// constructor

public Treenode( Object ob ) {

    data = ob;

    kids = new ArrayList(); // empty list of children

}

public List getKids() { return kids };

example:

kidss

 

data

 
                                                          "A"

 

 

 

 


 

data

 

kidss

 

data

 

kidss

 

data

 

kidss

 
                        "B"                             "C"                             "D"

                                                                                     

 

Note:  to iterate through a Treenode T's children:

  Iterator it = T.getKids().iterator();

  while (it.hasNext()) {

    it.next()…  // returns a Treenode, T's next child

    }

 

You try:

height of a tree =

# of nodes in longest path from a root to a leaf

for empty tree:   height = 0

non-empty tree:  1 + height of "tallest" subtree

 

 

 

 

 

 


   

1   int height( Treenode T ) {

2     if (T == null) return 0;

3    int sub = 0;

4     Iterator it = T.getKids().iterator();

5     while (it.hasNext()){

6       int tmp = height (it.next());

7       if (tmp > sub) sub = tmp;

8     }

9     return 1 + sub;

10  }

(simulate with input = node A)