Computer Science 112:

Introduction to Computer Science II

Gregory M. Kapfhammer


flickr photo shared by Billboard Art Project under a Creative Commons ( BY-NC-ND ) license

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

Linear data structures

Hierarchical data structures

Trees are like linked lists but ...

... they have "parent child" relationships!

Benefits of using trees?

Drawbacks of using trees?

Tree Type

A tree is a set of nodes

It stores data elements hierarchically

It contains a root node

It contains internal/external node(s)

The nodes have a parent-child relationship

What does this look like in memory?


flickr photo shared by Derek Severson under a Creative Commons ( BY-NC-ND ) license

Tree Type

A node can have an ... ancestor

... sibling

... descendant

A tree can contain a ... subtree

... a path of nodes

Any questions about trees?


flickr photo shared by Derek Severson under a Creative Commons ( BY-NC-ND ) license

Ordered trees

Un-ordered trees

Trade-offs of ordering?

Investigate the TreeSet

Check your repository

Compile and run the example

Notice that the data comes out in order!

Let's add more data to the TreeSet

Add random integers into the TreeSet

Depth of a node = number of ancestors

Tree height = depth of "deepest" node

Binary trees

General trees

Tree traversal

Any questions about trees?