Trees - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Trees

Description:

Binary tree recursive definition. An empty tree is a binary tree. ... traverse (BinTree T) if ( T is not empty) 1. traverse (leftSubtree(T)) 2. traverse ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 15
Provided by: IRE62
Category:
Tags: traverse | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
2
General out tree

root

siblings children of
the same node
subtree
internal nodes
or nonleaves
external nodes, or leaves


3
Binary Tree
  • Binary tree recursive definition.
  • An empty tree is a binary tree.
  • If L and R are binary trees then one node called
    a root joined with L and R is also a binary tree.
  • Depth or level of a node distance from the
    root.
  • Root has depth 0. Any other node has distance
    that equals 1 plus distance of its parent.
  • Height of a tree maximal depth of all leaves.
  • Height of a node is height of a subtree where
    this node is a root.

4
Definitions
  • Degree of a node number of children for that
    node
  • Internal nodes nodes with at least one child.
  • Internal nodes have positive degree
  • External nodes nodes with no children.
  • External nodes have degree zero.

5
Definitions
  • Complete Binary Tree is a binary tree in which
    all internal nodes have degree 2 and all leaves
    are at the same hepth.

6
Lemmas
  • There are at most 2d nodes at depth d of a binary
    tree.
  • A binary tree with height h has at most
  • 2 h1 1 nodes.
  • A binary tree with n nodes has height at least
    ceiling lg(n1) -1.

7
Binary TreeTraversal
  • void traverse (BinTree T)
  • if ( T is not empty)
  • 1
  • traverse (leftSubtree(T))
  • 2
  • traverse (rightSubtree(T))
  • 3
  • process root(T) is either in point 1 or in
    point 2 or in point 3 respectively to get
    preorder, inorder or postorder traversal.

8
General trees
  • A General out-tree is a nonempty structure with
    nodes and directed edges, such that one node
    called a root has no incoming edges and all other
    nodes have exactly one incoming edge.
  • A forest is a collection of separate trees.
  • Every node in a tree is a root of its own
    subtree.
  • In - tree has edges oriented from child toward
    the parent instead of away from it as in out tree.

9
Definitions
  • Tree rooted at w is called a principal subtree of
    the tree rooted at v
  • v
  • w

10
Principal Subtree Properties
  • Each principal subtree has fewer nodes than the
    whole tree.
  • Subtrees do not have to be ordered for general
    trees.
  • There is a set of subtrees for every node of a
    general tree.
  • Subtrees in a binary tree are ordered.
  • There is left and right subtree. This is a
    sequence of two subtrees.

11
General Tree ADT
  • Tree buildTree( object newRoot, TreeList
    oldTrees)
  • Preconditions none
  • Postconditions If xbuildTree( object newRoot,
    TreeList oldTrees) then
  • X refers to a newly created object
  • root(x) newRoot
  • Subtrees(x) oldTrees

12
General Tree ADT
  • Assume that subtrees are ordered.
  • Object root(Tree t)
  • PreconditionNone
  • TreeList subtrees (tree t)
  • PreconditionNone

13
TreeList ADT
  • TreeList ADT is the analog of IntList with class
    Tree in place of int for the element type.
  • TreeList cons (Tree t, TreeList rSiblings)
  • Tree first (TreeList siblings)
  • TreeList rest(TreeList siblings)
  • TreeList nil

14
General Tree Traversal
  • Page 85 Fig 2.13
Write a Comment
User Comments (0)
About PowerShow.com