Lecture 17 Trees - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Lecture 17 Trees

Description:

(Pre- and Post- are 'opposites,' i.e. Ro-LR vs LR-Ro and 'In' puts Ro 'in-'side? ... Some properties of trees. A tree with n nodes has n-1 arcs ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 12
Provided by: Defa67
Category:
Tags: lecture | trees

less

Transcript and Presenter's Notes

Title: Lecture 17 Trees


1
Lecture 17 Trees
  • July 21st , 2003

2
Tree--Definition
  • A tree is an acyclic, connected graph with one
    node designated as the root of the tree.
  • Special case
  • nonrooted tree (free tree)
  • Forest acyclic graph (not necessarily
    connected)
  • Parent vs child(ren).

3
Definition (cont.)
  • Depth
  • Leaf
  • Node
  • Binary tree
  • Full binary tree
  • Complete binary tree (almost full).

4
Tree -- Applications
  • In data structure
  • Binary search tree
  • 2-3 tree
  • In file management
  • Files in computer are organized in a tree-like
    structure.
  • In biology
  • Family tree

5
Creating EXPRESSION Tree
  • Ex. 23 mentions the idea and shows an
  • Expression Tree for
  • (2 x) (y 3)
  • --- MINUS is the ROOT node PLUS MULT
    first-level below the root
  • and 2, x, y, 3 are the LEAVES
  • Prax 19 p. 374 asks for an ExpTree for a
    simpler case (2 3) 5
  • --- When youve seen one Expressn Tree youve
    seem em all ?

6
Binary tree representation
  • Left child - right child rep --- TABLE (See lotsa
    0,0 pairs!!)
  • Pointer representation --- Linked List (3-Slot
    Elements)
  • Ex 24 p. 374-375 illustrates both these REPs
    for a 3-level binary tree with one missing leaf
    !! 1 ? 2, 3 2 ? 4, 5 3 ? 6
  • Prax. 20 p. 375 illustrates both these REPs
    for a 4-level tree which opens only to one side
    (from the top node)
  • COMMENT on Prax 20 By writeg out the
    solutions in the white space of the page where
    Prax 20 is found, I thought it would be better to
    FIRST do the POINTER rep and then follow it up
    with Left-Child/Right-Child solution I
    report-you decide The pointer rep. kinda
    matches up with the drawing and helps identify
    0 (or ) locations for the LChild-RChild Rep

7
Tree traversal algorithms
  • Pre-order --- Root-Left-Right .. RoLtRt e-o
    o-e
  • In-order --- Left-Root-Right.. LtRoRt
    i-o e-o
  • Post-order --- Left-Right-Root.. LtRtRo o-o
    e-i
  • (Pre- and Post- are opposites, i.e. Ro-LR vs
    LR-Ro and In puts Ro in-side?)
  • A basic idea is a recursive view of a tree
  • Subtrees are labelled as T1 , T2, ,Tt

8
A traversal algorithm
  • ALGORITHM PREORDER
  • Preorder (tree T)
  • write (r)
  • for i1 to t do
  • Preorder(Ti)
  • end for
  • end Preorder
  • Postorder merely shifts the write the for
    loop (and changes names)
  • Inorder mimics Preorder mit an extra call on
    Inorder(Ti) at the start also changeg the for
    loop start to 2
  • See non binary tree remarks on p. 378

9
Traversal--Examples
  • Ex 25-27 p377 Ar puts ans. right above the
    Tree for PRE-ORD (Root-LR)
  • a
  • b
    c
  • d e f
    g
  • h
    i
  • and then just lists (on the page below) the
    solutions for INORD (L-Root-R, first, and POSTORD
    (LR-Root) last
  • Prax 21- p. 378 does about the same thing (all 3
    orders)
  • Can we rep. algebra expressions as binary trees?
    Choices are given as
  • Get Infix notation from Inorder, uncoverg (2
    x) 4 --- PARENS put in (How?)
  • Git Prefix notation (aka Polish notation) from
    Preorder .. uncoverg 2 x 4
  • Get Postfix notation (aka Reverse Polish)..
    Uncoverg 2 x 4
  • ? See a Pre- Post- convert to Infix (a
    hint on how parens appear?)
  • b Pre- Post- DO NOT NEED PARENS
    at all (SEMANTIC-wise) and
  • c They evaluate sequentially
    mit NO LOOK-AHEADs for paren-exprs

10
Some properties of trees
  • A tree with n nodes has n-1 arcs
  • Any tree with n nodes, the total number of arc
    ends is 2n-2.
  • A binary tree has at most 2d nodes at depth d.
  • The number of leaves in any binary tree is 1 more
    than the number of nodes with two children.

11
Exercise
  • Exercises 5.2
  • 3, 10,23, 28, 38, 40, 41
Write a Comment
User Comments (0)
About PowerShow.com