Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Trees

Description:

An empty tree has no nodes ... In UNIX, the root of the tree is denoted by / A plain file is typically a leaf. 7. Family trees ... – PowerPoint PPT presentation

Number of Views:219
Avg rating:3.0/5.0
Slides: 13
Provided by: DavidMa5
Category:
Tags: family | genealogy | tree | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
2
Definition of a tree
  • A tree is a node with a value and zero or more
    children
  • Depending on the needs of the program, the
    children may or may not be ordered
  • A tree has a root, internal nodes, and leaves
  • Each node contains an element and has branches
    leading to other nodes (its children)
  • Each node (other than the root) has a parent
  • Each node has a depth (distance from the root)

3
More definitions
  • An empty tree has no nodes
  • The descendents of a node are its children and
    the descendents of its children
  • The ancestors of a node are its parent (if any)
    and the ancestors of its parent
  • The subtree rooted at a node consists of the
    given node and all its descendents
  • An ordered tree is one in which the order of the
    children is important an unordered tree is one
    in which the children of a node can be thought of
    as a set
  • The branching factor of a node is the number of
    children it has
  • The branching factor of a tree is the average
    branching factor of its nodes

4
Data structure for a tree
  • Each node in a tree has an arbitrary number of
    children, so we need something that will hold an
    arbitrary number of nodes, such as an ArrayList
  • class TreeltVgt V value ArrayList
    children
  • If we dont care about the order of children, we
    might use a Set instead of a ArrayList

5
ADT for a tree
  • It must be possible to
  • Construct a new tree
  • If a tree can be empty, this may require a header
    node
  • Add a child to a node
  • Get (iterate through) the children of a node
  • Access (get and set) the value in a node
  • It should probably be possible to
  • Remove a child (and the subtree rooted at that
    child)
  • Get the parent of a node

6
File systems
  • File systems are almost always implemented as a
    tree structure
  • The nodes in the tree are of (at least) two
    types folders (or directories), and plain files
  • A folder typically has childrensubfolders and
    plain files
  • A folder also contains a link to its parentin
    both Windows and UNIX, this link is denoted by ..
  • In UNIX, the root of the tree is denoted by /
  • A plain file is typically a leaf

7
Family trees
  • It turns out that a tree is not a good way to
    represent a family tree
  • Every child has two parents, a mother and a
    father
  • Parents frequently remarry
  • An upside down binary tree almost works
  • Since it is a biological fact (so far) that every
    child has exactly two parents, we can use left
    child father and right child mother
  • The terminology gets a bit confusing
  • If you could go back far enough, it becomes a
    mathematical certainty that the mother and father
    have some ancestors in common

8
Part of a genealogy
Elaine
Pauline
Chester
Eugene
Paula
Carol
David
Winfred
Danielle
Steven
Isaac
9
Game trees
  • Trees are used heavily in implementing games,
    particularly board games
  • A node represents a position on the board
  • The children of a node represent all the possible
    moves from that position
  • More precisely, the branches from a node
    represent the possible moves the children
    represent the new positions
  • Planning ahead (in a game) means choosing a path
    through the tree
  • However
  • You cant have a cycle in a tree
  • If you can return to a previous position in a
    game, you have a cycle
  • Graphs can have cycles

10
Trees for expressions and statements
  • Examples

11
More trees for statements
  • while (n gt 1) exp x exp n--
  • for (int i 0 i lt n i) ai 0

12
The End
Write a Comment
User Comments (0)
About PowerShow.com