CSC 2300 Data Structures - PowerPoint PPT Presentation

About This Presentation
Title:

CSC 2300 Data Structures

Description:

A tree consists of a distinguished node r, called the root, and zero or more ... Implementation of Trees. Can you suggest an obvious way to implement a tree? ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 22
Provided by: stude6
Learn more at: http://www.cs.rpi.edu
Category:
Tags: csc | data | structures | trees

less

Transcript and Presenter's Notes

Title: CSC 2300 Data Structures


1
CSC 2300Data Structures Algorithms
  • February 6, 2007
  • Chapter 4. Trees

2
Today Trees
  • Tree basics
  • Tree implementation
  • Induction and trees
  • Tree traversal
  • Tree algorithm analysis
  • Tree application file system

3
Terminology
4
Rooted Tree
  • Recursive definition. A tree consists of a
    distinguished node r, called the root, and zero
    or more nonempty (sub)trees T1, T2, , Tk, each
    of whose roots are connected by a directed edge
    from r.
  • Parent and child.
  • The root r is the parent of each subtree root.

5
Tree
  • Definition of leaves?
  • Which nodes are leaves?
  • Definition of siblings?
  • Which nodes are siblings?
  • Grandparent and grandchild?

6
Path and Length
  • A path from node n1 to node nk is defined as a
    sequence of nodes n1, n2, , nk such that ni is
    the parent of ni1, for 1i
  • The length of this path is the of edges on the
    path, namely k-1.
  • Is there a path of length zero?
  • There is exactly one path from the root to each
    node.

7
Depth and Height
  • The depth of node ni is the length of the unique
    path from the root to ni.
  • What is the depth of the root?
  • The height of ni is the length of the longest
    path from ni to a leaf.
  • What is the height of a leaf?
  • The height of a tree is equal to the height of
    its root.
  • The depth of a tree is equal to the depth of the
    deepest leaf.
  • How is the height of a tree related to its depth?

8
Ancestor and Descendant
  • If there is a path from ni to nk, then ni is an
    ancestor of nk and nk is a descendant of ni.
  • If ni ? nk, then ni is a proper ancestor of nk
    and nk is a proper descendant of ni.

9
Implementation of Trees
  • Can you suggest an obvious way to implement a
    tree?
  • Have in each node, besides its data, a link to
    each child of the node.
  • Why is this approach possibly very inefficient?
  • The of children per node may vary greatly and
    is not known in advance.
  • Is there a better solution?

10
Solution
11
Induction and Trees
12
Check Your Answer
  • m 5, i ?, l ?

13
Check Your Answer
  • m 2, i ?, l ?

14
Tree Traversals
15
Time Analysis of Tree Algorithm
16
Application
  • One popular use of tree is the directory
    structure in operating systems, including UNIX
    and DOS.

17
List Directory
18
Time Analysis
  • Line 1 is executed exactly once per node, since
    each name is output once.
  • It follows that line 2 must also be executed once
    per node.
  • Line 4 can be executed at most once for each
    child of each node. But the number of children is
    exactly one less than the number of nodes.
  • The for loop iterates once per iteration of
    line 4, plus each time the loop ends.
  • Thus, if there are N file names to be output,
    then the running time is O(N).

19
Traversal Strategy
  • What is the traversal strategy?
  • In preorder traversal, work in a node is
    performed before (pre) its children are processed.

20
Size of Each Directory
  • Suppose we know the number of disk blocks taken
    by each file. How can we find the size of each
    directory?

21
Postorder Traversal
  • In a postorder traversal, the work at a node is
    performed after (post) its children are evaluated.
Write a Comment
User Comments (0)
About PowerShow.com