Trees - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Trees

Description:

Preorder traversal. Postorder traversal. Inorder traversal. Level order traversal ... Pre-order traversal ... only once, so a pre-order traversal takes O(n) ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 13
Provided by: patric190
Category:
Tags: preorder | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
2
Definition
  • A general tree set of nodes and edges that
    connect them.
  • Exactly one path between 2 nodes
  • Path connected sequence of edges
  • A rooted tree
  • One distinguished node is called the root
  • Every node c, except root, has one parent p, the
    first node on path from c to the root
  • Root has no parent and a node can have any number
    of children

3
Terminology
  • Leaf node with no children
  • Sibling nodes with same parent
  • Ancestors of a node A nodes on path from d to
    root, including A (?), As parent, As
    grandparent,, root.
  • If A is ancestor of D, then D is a descendant of
    A
  • Length of path number of edges (NOT vertices) in
    the path
  • Depth of node N length of path from N to root
  • Depth of root is zero
  • Height of node N length of path from N to its
    deepest descendant
  • Height of any leaf is zero
  • Height of a tree height of the root
  • Subtree rooted at N tree formed by N and its
    descendants.
  • Size number of nodes

4
Examples
  • Leaf C, D, F, H
  • Sibling B and G, C, D, and E
  • Height of the tree
  • Size of the tree
  • Depth of H
  • Ancestors of node D
  • What happens if there is a link between D and F?
  • Is it a tree?
  • Why?

5
Another example
  • The structure shown on the right was extracted
    from Gene Ontology. Round rectangles represent
    nodes (GO terms) and arrows stand for edges
    indicating the relationship between two nodes.
  • Is it a tree structure? Why?

6
Tree traversal
  • Tree traversal a manner of visiting each node in
    a tree once.
  • Visiting a node performing an action (such as
    printing, examining, updating,) on a node.
  • Always starting from the root
  • Four ways
  • Preorder traversal
  • Postorder traversal
  • Inorder traversal
  • Level order traversal

7
Pre-order traversal
  • Definition Visit each node before recursively
    visiting its children left to right.
  • Root visited first
  • Each node visited only once, so a pre-order
    traversal takes O(n) times, where n is the number
    of nodes in a tree.
  • Examples
  • Printing out the file directory
  • in a natural way

8
Post-order Traversal
  • Visiting each nodes children (from left to
    right) before the node itself.
  • Visit the root last
  • To do a post-order traversal of a general tree
  • Do a post-order traversal each of the subtrees of
    the root one-by-one in the order given and then
  • visit the root.
  • Examples
  • Expression tree
  • Calculation of total disk space
  • taken by a file directory

9
Level-order traversal
  • Visit the root (depth zero), then all the nodes
    at depth one, then all the nodes at depth two,
    and so on.
  • At each depth the nodes are visited from left to
    right
  • Non recursive traversal
  • Queue-based implementation
  • Enqueue the root
  • while (queue is not empty)
  • Dequeue a node
  • Visist it
  • Enqueue its children from left to right
  • Until queue is empty

10
In-order traversal
  • Inorder traversal only makes sense for binary
    trees
  • Traverse left child, and then
  • Visit node, and then
  • Traverse right child
  • Recursive implementation

11
Trees types and examples
  • Types of tree
  • A general tree
  • A tree has no limit to the number of children
  • Binary tree
  • Each node has at most 2 children
  • Balanced tree
  • If all the leaves of the tree are on the same
  • level or at least within one level of each other
  • AVL trees, binary search tree
  • Everyday life
  • A family tree
  • The organization of sport tournaments
  • Computer applications
  • A file system
  • A web page
  • Expression tree the parent nodes are the
    operators, and the children are the operands

12
Exercises
  • Given the following tree

A
C
B
G
D
E
F
I
H
  • List the nodes of the above general tree in the
    following order
  • Pre-order
  • Post-order
  • Level-order
Write a Comment
User Comments (0)
About PowerShow.com