Lecture 17: Trees
Announcements
·
programming
assignment #3 due
·
homework
#6 is due at
·
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
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
E C A B D 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
C A B D
"A"
"D"
Operations on trees are implemented using resursive
methods!
example: recursive definition of the height of 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: write
int height ( BinaryTreenode T ) {
//fill in the code to 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; // can be private
private List kids; // should be private
// 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"
… … …
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: write
the code for
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
int height (Treenode
T) {
// fill in
your code here
}
Solution:
1 int height(
Treenode T ) {
2 if (T == null) return 0; // base case
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;
A B C D F E H G
10 }
simulate with
input = node A:
/ / / / sub: 0 1 tmp: 1 2 it.next: B
C
D
height (A)
X X
/ sub: 0 it.hasNext: false
height(B) height(C) height(D)
/ sub: 0 1 tmp: 1 it.next: E F
X X X
/ sub:
0 1 tmp: 1 it.next:
H sub: 0 it.hasNext: false sub: 0 it.hasNext: false
height(E) height(F) height(G)
X
sub: 0 it.hasNext: false
height(H)