Trees - PowerPoint PPT Presentation

Loading...

PPT – Trees PowerPoint presentation | free to view - id: 20a96-MTE0O



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Trees

Description:

a tree is a nonlinear data structure ... family tree. class inheritance hierarchy in Java ... Tree Terminology (cont'd) Continuing the family tree analogy: ... – PowerPoint PPT presentation

Number of Views:152
Avg rating:3.0/5.0
Slides: 41
Provided by: jasonm6
Category:
Tags: family | tree | trees

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Trees


1
Topic 12
  • Trees

2
Chapter Objectives
  • define trees as data structures
  • define the terms associated with trees
  • discuss the possible implementations of trees
  • analyze tree implementations of collections
  • discuss methods for traversing trees
  • examine a binary tree example

3
Trees
  • a tree is a nonlinear data structure used to
    represent things that are in some hierarchical
    relationship, for example
  • family tree
  • class inheritance hierarchy in Java
  • computer file system (directories and
    subdirectories)
  • decision trees
  • top-down design

4
Tree Definition
  • tree a set of elements of the same type that is
    either empty or has a distinguished element
    called the root, from which descend zero or more
    subtrees
  • each subtree is itself a tree

5
Tree Terminology
  • nodes the elements in the tree
  • a node has a data portion and some reference
    portions
  • nodes are connected by edges
  • root the origin of the tree
  • leaf node a node without a reference to another
    node
  • interior node a node that is not a leaf node

6
Tree Terminology
7
Tree Terminology (contd)
  • a tree has no cycles (i.e. a node cant refer
    back to a node that is higher in the hierarchy)
  • the simplest tree is the empty tree
  • what does that mean? the root reference is null

8
Tree Terminology (contd)
  • parent or predecessor the node directly above in
    the hierarchy
  • child or successor a node directly below in the
    hierarchy
  • siblings nodes that have the same parent
  • example diagram

9
Discussion
  • Does a leaf node have any children?
  • Does the root node have a parent?
  • How many parents does every node other than the
    root node have?

10
Tree Terminology (contd)
  • Continuing the family tree analogy
  • ancestors of a node s are the parent of s, the
    parent of the parent of s, etc.
  • descendants of a node t are the children of t,
    the children of the children of t, etc.

11
Level of a Node
  • level of a node number of edges between root
    and node
  • it can be defined recursively
  • the root node is at level 1
  • a node that is not the root node has a level that
    is 1 greater than the level of its parent
  • example

12
Path length and level
13
Height of a Tree
  • height of a tree is the number of nodes on the
    longest path from the root to a leaf
  • alternate definition
  • is a tree is empty, its height is 0
  • is a tree is not empty, its height is the maximum
    of the levels of its nodes
  • example

14
Subtrees
  • subtree a set of nodes which is itself a tree
  • a subtree consists of a node and all its
    descendants
  • example
  • c is the root node of a subtree

15
Balanced and Complete Trees
  • a tree is balanced if all the leaves are on the
    same level or at least within one level of each
    other
  • a tree is complete if it is balanced and all the
    leaves at the bottom level are on the left side
    of the tree

16
Balanced and unbalanced trees
17
Some complete trees
18
Degree or Arity
  • the degree or arity of a node is the number of
    its children
  • example
  • the degree or arity of a tree is the maximum of
    the degrees of its nodes

19
Binary Trees
  • general tree a node may have any number of
    children
  • n-ary tree a node may have no more than n
    children
  • binary tree a node may have no more than 2
    children
  • example

20
Binary Trees (contd)
  • each node of a binary tree will have
  • a data portion
  • a reference to a left child
  • a reference to a right child
  • recursive definition of a binary tree
  • a binary tree is the empty tree, or a binary tree
    whose left and right successors are binary trees
  • a binary tree is a positional tree, i.e. it
    matters whether the subtree is left or right

21
Tree Traversals
  • a traversal of a tree should visit each node of
    the tree once
  • example a typical reason to traverse a tree is
    to display the data stored at each node of the
    tree
  • traversal orderings
  • preorder
  • inorder
  • postorder
  • level-order

22
Preorder Traversal
  • start at the root
  • visit each node, followed by its children
  • recursive algorithm
  • if tree is not empty,
  • visit root node of tree
  • perform preorder traversal of its left subtree
  • perform preorder traversal of its right subtree

23
Inorder Traversal
  • start at the root
  • visit the left child of each node, then the node,
    then any remaining nodes
  • recursive algorithm
  • if tree is not empty,
  • perform inorder traversal of left subtree of root
  • visit root node of tree
  • perform preorder traversal of its right subtree

24
Postorder Traversal
  • start at the root
  • visit the children of each node, then the node
  • recursive algorithm
  • if tree is not empty,
  • perform postorder traversal of left subtree of
    root
  • visit root node of tree
  • perform postorder traversal of right subtree of
    root

25
Traversal Analysis
  • each node is processed exactly once
  • so, a traversal is O(n)

26
Iterative Traversals
  • (do we want to do this??)
  • recursive tree traversals use the Java runtime
    stack to keep track of where we are in the tree
  • in iterative tree traversals, it is up to the
    programmer!
  • etc. etc. etc.

27
Operations on a Binary Tree
  • what might we want to do with a binary tree?
  • add an element (but where?)
  • remove an element (but from where?)
  • is the tree empty?
  • size of the tree (how many elements)
  • other operations?

28
Discussion
  • difficult to have a general add operation, until
    we know the purpose of the tree (we will discuss
    binary search trees later)
  • could add randomly go either right or left,
    and add at the first available spot

29
Discussion (contd)
  • similarly, where would a general remove operation
    remove from?
  • could arbitrarily choose to remove, say, the
    leftmost leaf
  • if random choice, what would happen to the
    children and descendants of the element that was
    removed? what does the parent of the removed
    element now point to?
  • what if the removed element is the root?

30
One Set of Operations on a Binary Tree
31
UML description of the BinaryTreeADT interface
32
Binary Tree Implementation using Links
  • to represent the binary tree, we need
  • a linked list of nodes
  • root to keep track of the node that is the root
    of the tree
  • count to keep track of the number of nodes in
    the tree
  • first, how will we represent a node?

33
BinaryTreeNode class
  • this represents a node in a binary tree and
    contains
  • the data element stored in the node
  • pointer to left child of the node
  • pointer to right child of the node
  • see BinaryTreeNode.java

34
BinaryTree class
  • it implements the Binary Tree ADT interface (see
    BinaryTreeADT.java)
  • constructors
  • removeLeftSubtree method
  • find method
  • iteratorInOrder method
  • see others in BinaryTree.java

35
Using Binary Trees Expression Trees
  • expression tree represents an arithmetic
    expression
  • root and internal nodes contain operations
  • leaf nodes contain operands
  • we can use an expression tree to evaluate an
    expression evaluate from the bottom up

36
An example expression tree
37
Building Expression Trees
  • how do we build an expression tree?
  • recall stack algorithm to evaluate postfix
    expressions
  • exercise develop the algorithm

38
Building an expression tree from a postfix
expression
39
Building an expression tree from a postfix
expression (contd)
40
UML description of the Postfix example
About PowerShow.com