Lecture 14 Slides - Trees!

1 of 31

Lecture 14

Trees!

2 of 31

Tree structures (root at the bottom)

3 of 31

Tree structures (root at the top)

4 of 31

Tree structures

  • Branching structures
    • Circles are called nodes or vertices
    • Connections are called edges
  • Typically start with a single node called the root
    • Traditionally drawn at the top of the tree
    • Branches downward
    • Merges upward

5 of 31

File systems are trees

  • Modern operating systems allow files to be placed in directories (folders)
    • And directories to be placed in other directories
    • And so on and so on
  • Creating a branching structure called a tree
  • The start of the tree is called the root directory

root

a

b

d

e

f

g

c

6 of 31

The DNS is a tree

  • The DNS is the Domain Name System
  • System used to keep track of internet host names
  • Namespace is tree structured
    • Each part of the name is a node in the tree
    • Full name is a path through the tree

root

edu

com

northwestern

berkeley

cnn

amazon

gov

it

cs

7 of 31

Data flow diagrams are (often) trees

8 of 31

Expressions are trees

(+ a

(- b)

(* c d)))

( )

+

a

( )

( )

-

b

*

c

d

9 of 31

Type hierarchies are trees

  • Computers have a taxonomy of types of data objects
  • Types can have subtypes
    • Which can have subsubtypes

object

number

integer

float

procedure

posn

color

picture

string

boolean

exception

contract-violation

arity-mismatch

10 of 31

Even English sentences can be thought of as trees

11 of 31

Relationships between nodes

  • Nodes directly below a node are its children
  • The node directly above is its parent

12 of 31

Relationships between nodes

  • Nodes above are its ancestors
  • Nodes below a node are its descendants

13 of 31

Kinds of nodes

  • The only node with no parent is the root

14 of 31

Kinds of nodes

  • Nodes with no children are called leaves
  • Other nodes are called interior nodes

15 of 31

Storing information in trees

  • Most of the trees we care about have additional information stored in the nodes or edges
  • These are sometimes called labeled trees
    • And the data stored in the node is called a label

( )

a

b

( )

( )

c

d

e

( )

f

( )

g

16 of 31

Trees are recursive

  • A tree node can have children
    • which can have children
      • which can have children
        • which can have children
          • which can have children

  • So trees are also inductively defined (aka recursively defined like linked lists!)
  • We define them in many different ways depending on what kinds of trees we want to use
  • Let’s think about ways of defining trees of numbers
    • i.e. trees where nodes are labeled with numbers

17 of 31

Representing�binary trees

Important: this is a somewhat different binary tree representation than the ones used in the tutorial and assignment; there are lots of ways to represent trees!

18 of 31

Example: binary trees

  • A binary tree is a tree where every node has at most two children
    • Usually called the left- and right-child
  • Binary trees come up in both the tutorial and the assignment this week
    • Each defines them slightly differently
  • Here, we’ll look at a variant that looks a little different than the one in the tutorial and assignment.

19 of 31

Example: binary trees

(define-struct binary-tree (data left right))

  • Here:
    • data is the data stored in the node
    • left and right are the nodes for the left- and right-child
  • A leaf (a node with no children) is just a node whose left and right fields are empty
  • Inductive definition: a binary tree is either:
    1. The value empty (i.e. '()), or
    2. (make-binary-tree any binary-tree binary-tree)

20 of 31

Example: binary trees

So the following are all valid binary trees:

  • empty ; (by rule 1)
  • (make-binary-tree 1 empty empty) ; by (a) and rule 2
  • (make-binary-tree 2

(make-binary-tree 1 empty empty)

empty) ; by (a), (b) and rule 2

  • (make-binary-tree 2

(make-binary-tree 1 empty empty)

(make-binary-tree 0 empty empty)) ; by (a), (b) and rule 2

  • (make-binary-tree 2

(make-binary-tree 1

empty

(make-binary-tree 1 empty empty))

(make-binary-tree 1 empty empty)) ; by (a), (b) and rule 2

21 of 31

Tree Diagrams to Check Your Understanding

22 of 31

Writing a recursion

(define-struct binary-tree (data left right))

  • In this variant, a binary tree is either:
    • The value empty (i.e. ‘()), or
    • (make-binary-tree any binary-tree binary-tree)
  • So our base case will be the value empty
    • “The empty tree”
  • And our recursive case will recurse on both (tree recursion):
    • The left child, and
    • The right child

23 of 31

Example: Counting the nodes in a tree

(define (node-count tree)

(if (empty? tree)

0

(+ 1

(node-count (binary-tree-left tree))

(node-count (binary-tree-right tree)))))

  • Check if it’s empty (base case)
    • Then there are no nodes (return zero)
  • Otherwise recursive case
    • Count the nodes on the left
    • Count the nodes on the right
    • Return the sum

24 of 31

More general trees

25 of 31

Another tree example

  • Binary tree nodes can only have two children.
  • Why if we want to allow nodes to have arbitrary numbers of children?
  • Put the children in a list!

(define-struct tree (data child-list))

  • Inductive definition:
    • A tree is (make-tree any (listof tree))
  • Note that in this representation,
    • There’s no either/or in the definition
    • There’s no representation for an empty tree
    • A leaf is just a node with an empty child-list

26 of 31

Examples

  • (make-tree 1 '()) ; is a tree
  • (make-tree 1� (list (make-tree 2 '()))) ; is a tree
  • (make-tree 1� (list (make-tree 2 '())� (make-tree 3 '()))) ; is a tree
  • (make-tree 1� (list (make-tree 2 '())� (make-tree 3 '())� (make-tree 4 '()))) ; is a tree
  • (make-tree 1� (list (make-tree 2 '())� (make-tree 3 (list (make-tree 5 '())))� (make-tree 4 '()))) ; is a tree

27 of 31

Tree Diagrams to Check Your Understanding

28 of 31

Counting the nodes in the new type of tree

(define (new-count-nodes tree)� (+ 1� (foldl + 0� (map new-count-nodes� (tree-child-list tree)))))

Or just:

(define (new-count-nodes tree)� (foldl + 1� (map new-count-nodes� (tree-child-list tree))))

29 of 31

Counting the nodes in the tree

(define (new-count-nodes tree)� (+ 1� (foldl + 0� (map new-count-nodes� (tree-child-list tree)))))

  • Wait!
  • Where’s the base case?
  • Won’t this recurse infinitely?

30 of 31

Nope!

Let’s run it on a leaf, using the substitution model:

(new-count-nodes (make-tree 1 '()))

  • (+ 1� (foldl + 0� (map new-count-nodes� (tree-child-list (make-tree 1 '())))))
  • (+ 1� (foldl + 0� (map new-count-nodes� '())))
  • (+ 1� (foldl + 0 '()))
  • (+ 1 0)
  • 1

31 of 31

Counting the nodes in the tree

(define (new-count-nodes tree)� (+ 1� (foldl + 0� (map new-count-nodes� (tree-child-list tree)))))

  • New-count-nodes is recursive
  • But it uses map to call itself
    • Rather than calling itself directly
  • If the child list is empty (a leaf)
    • Then map doesn’t have anything to call it on
  • So the recursion stops naturally