Trees I think that I shall never see Trees I A poem lovely as a - - PDF document

trees
SMART_READER_LITE
LIVE PREVIEW

Trees I think that I shall never see Trees I A poem lovely as a - - PDF document

Trees I think that I shall never see Trees I A poem lovely as a tree -- J. Kilmer Trees Anatomy of a Tree a In CS, we look at trees Node from the bottom up Basic element of the tree b c Contains a piece of


slide-1
SLIDE 1

1

Trees I Trees

I think that I shall never see A poem lovely as a tree

  • - J. Kilmer

Trees

  • In CS, we look at trees

from the bottom up

Anatomy of a Tree

  • Node

– Basic element of the tree – Contains a piece of information – Represented by the circles a b c d e f g h i j

Anatomy of a Tree

  • Child

– Direct descendant of a node – Node a has 2 children: b and c – A node may have multiple children

  • Parent

– Direct ancestor of a node – The parent of node b and c are Node a – A node can have only 1 parent

a b c d e f g h i j

Anatomy of a Tree

  • Sibling

– Nodes that share the same parent – Nodes b and c are siblings

  • Leaf

– Node that has no children – Nodes d,e,h,i, and j are leaves a b c d e f g h i j

slide-2
SLIDE 2

2

Anatomy of a Tree

  • Root

– Topmost node of the tree – The root is an orphan: It has no parent. – Node a is the root of this tree. a b c d e f g h i j

Anatomy of a Tree

  • Root

– Topmost node of the tree – The root is an orphan: It has no parent. – Node a is the root of this tree. a b c d e f g h i j

Anatomy of a Tree

  • Trees are strictly

acyclic

– A child node cannot be a parent to one of its ancestors – This figure is not a valid tree – Node c has 2 parents! a b c d e f g h i j

Anatomy of a Tree

  • Subtree

– All children of a node, can be considered the root of it’s own tree – These are called subtrees – I smell recursion! a b c d e f g h i j

Anatomy of a Tree

  • Balanced Tree

– A tree is balanced if all child subtrees have similar depths – This tree is not balanced since Node c’s subtree has a depth

  • f 2 but Node b’s

subtree has a depth of 1. a b c d e f g h i j

Anatomy of a Tree

  • Binary Tree

– A binary tree is a tree where each node has at most 2 children. – This tree is not a binary tree a b c d e f g h i j

slide-3
SLIDE 3

3

Anatomy of a Tree

  • Binary Tree

– But this tree is – Children of nodes in a binary tree are referred to as left child or right child

  • Node h is the left child
  • f Node f
  • Node i is the right child
  • f Node f

a b c d e f g h i

Anatomy of a Tree

  • Full Binary Tree

– A binary tree is full if

  • All of it’s leaf nodes are
  • f the same depth
  • Each non-leaf node has

2 children

– This binary tree is not full. a b c d e f g h i

Anatomy of a Tree

  • Full Binary Tree

– But this one is. a b c d e f g

Anatomy of a Tree

  • Complete Binary Tree

– A binary tree is complete if

  • Each level (except the

deepest) must contain as many nodes as possible

  • At the deepest level, all

nodes as as far left as possible

– This binary tree is not complete. a b c d e f g h i

Anatomy of a Tree

  • Complete Binary Tree

– But this one is!

  • Questions?

a b c d e f g h i

Traversing a Tree

  • A means to process all the nodes in a tree

– A traversal starts at the root – Visits each node exactly once

  • “Processes” the data in a node when visited

– Nodes can be visited in different orders

  • Breadth-first traversal
  • Depth-first traversal
slide-4
SLIDE 4

4

Anatomy of a Tree

  • Breadth-first

– All the nodes at a given level are visited before the nodes at the next level – Example:

  • a,b,c,d,e,f,g,h,i

a b c d e f g h i

Anatomy of a Tree

  • Depth-first

– At each node, the node is visited as well as it’s children’s subtrees. – Different types based

  • n order
  • Pre-order
  • In-order
  • Post-order

a b c d e f g h i

Anatomy of a Tree

  • Pre-order

– At each node

  • The node is visited first
  • Pre-order traversal of

the left subtree

  • Pre-order traversal of

the right subtree

– Example:

  • a,b,d,h,i,e,c,f,g

a b c d e f g h i

Anatomy of a Tree

  • in-order

– At each node

  • In-order traversal of the

left subtree

  • The node is visited next
  • The In-order traversal
  • f the right subtree

– Example:

  • h,d,i,b,e,a,f,c,g

a b c d e f g h i

Anatomy of a Tree

  • post-order

– At each node

  • Post-order traversal of

the left subtree

  • Post-order traversal of

the right subtree

  • The node is visited next

– Example:

  • h,i,d,e,b,f,g,c,a

a b c d e f g h i

What are trees good for?

  • Hierarchical relationships

Performer Actor Musician isA isA Guitarist Pianist Drummer

slide-5
SLIDE 5

5

What are trees good for?

  • Binary trees can be used to represent

decision taxonomies

Mammal? Bigger than a cat? Underwater? Trout Bird Elephant Mouse

yes yes yes no no no

What are trees good for?

  • Branching can also imply ordering of node

data

45 30 55 50 60 22 35

< < < > > >

Summary

  • Trees

– Terminology

  • Root
  • Children
  • Leaf

– Trees are acyclic – Traversal

  • Breadth-first
  • Depth-first

– Pre-order – In-order – Post-Order

Next Time

  • Coding a binary tree in Java