Trees - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Trees

Description:

The 'family' tree. At the top level of a tree is a single node called the root. ... Extended Family. Children are the descendants of all nodes above them in ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 18
Provided by: cseBu
Category:
Tags: family | tree | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
2
What is a tree?
  • A tree is a set of nodes connected by edges that
    indicate the relationships among the nodes.
  • A tree is a special form of a graph.
  • The nodes are arranged in levels that indicate
    the nodes hierarchy.

3
The family tree
  • At the top level of a tree is a single node
    called the root.
  • The nodes of each successive level are the
    children of the nodes at the previous level
  • A node that has children is the parent of those
    children.
  • Children that have the same parent are called
    siblings.

4
Extended Family
  • Children are the descendants of all nodes above
    them in the hierarchy.
  • Parents are ancestors of all nodes lower than
    them in the hierarchy that have a path from the
    parent to the descendant node.

5
Back to Botany
  • A node with no children is called a leaf node
  • A node with children is an interior, or non-leaf
    node.

6
Some final terminology
  • The height of a tree is the number of levels in
    the tree.
  • A path is a set of edges that connects two nodes
    in the tree in a strictly descending (or
    ascending) hierarchical order.
  • The length of a path is the number of edges that
    compose it.

7
ADTs can be recursive too
  • The tree ADT is recursive in nature.
  • Each node in the tree is itself a tree.
  • This includes the empty tree.

8
Some Real WorldTM Examples
  • Org Chart
  • Playoff Chart
  • Table Of Contents

9
Some Computing Examples
  • Expression Trees
  • Game Trees
  • Decision Trees

10
Big Families
  • How many children can a node have?
  • We are used to dealing with binary trees, but a
    general tree can have any number of children.
  • A tree with no more than n children per node is
    called an n-ary tree. Thus, the binary tree has
    no more than 2 children per node.

11
Full and Complete
  • For a strict n-ary tree, there are two more
    conditions of interest to us, which are fullness
    and completeness.
  • To say that a tree is full, is to say that each
    non-leaf node in the n-ary tree has exactly n
    children.
  • To say that a tree is complete is to say that it
    is full to its next to last level, and that the
    last level is filled in from left to right.

12
Guess the number of nodes
  • A full or complete tree can calculate the number
    of nodes with the formula nh-1, where n is the
    number of children per node, and h is the height
    of the tree.
  • This can also be expressed as the sum from i0 to
    h-1 of ni
  • You can reverse the calculation to calculate the
    height of a tree with x nodes, which is logn(x1)

13
OK, Now What?
  • So, now we know what all the parts of a tree are
    called. What use is it?
  • In order to do anything with the tree, we have to
    be able to access the nodes in the tree.
  • For bags, and stacks, and sets, and lists, this
    was a straightforward process, you just move
    linearly through the structure.
  • Our new structure now has an extra dimension.
    How do we move through this to visit all the
    nodes?

14
Traversals
  • A method of walking through the nodes of a tree
    is called a traversal.
  • There are two basic categories of tree traversal,
    breadth first, and depth first.
  • In breadth first, we treat the levels of the tree
    like linear structures, and walk through each
    level in order.
  • The primary breadth first search is called a row
    order traversal.

15
Traversals
  • In depth first, we walk through the tree in
    varying order dependent on the subtrees.
  • There are three main depth first traversals,
    preorder, inorder, and postorder.
  • preorder visits the root, then the left, then the
    right.
  • inorder visits the left, then the root, then the
    right.
  • postorder visits the left, then the right, then
    the root.
  • Note that inorder does not map to a general tree,
    but only makes sense for binary trees.

16
Traversals
  • The inorder traversal is of course how we
    typically write these expressions.
  • The postorder is how, for example, an HP
    calculator expects its input
  • The preorder is how many functional programming
    languages operate (e.g., Lisp, Scheme), and
    indeed, how functions are written the function
    name comes first, then its arguments.

17
Special Trees
  • Binary Search Tree must have comparable items,
    and all items in the left subtree are less than
    all items in the right subtree.
  • Heap must have comparable items, and the root
    of any tree is the smallest (minheap) or largest
    (maxheap) of the tree.
Write a Comment
User Comments (0)
About PowerShow.com