Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Trees

Description:

3. What instance of the problem can serve as the base case? ... For each node n, a binary search tree satisfies the following three properties: ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 27
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:
Tags: serve | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
  • B.Ramamurthy
  • Chapter 9
  • CSE116A,B

2
Introduction
  • Data organizations we studied are linear in that
    items are one after another.
  • Data organization can also be hierarchical
    whereby an item can have more than one immediate
    successor.
  • Thus an ADT can be classified as linear and
    non-linear.
  • We will study non-linear ADTs tree and binary
    trees in this discussion.

3
Topics for discussion
  • Terminology
  • Tree and Binary Tree -- Formal definition
  • Tree Traversal
  • Full binary tree
  • Complete binary tree
  • Balanced Tree
  • Summary

4
Tree Formal Definition
  • A tree T is a finite, non-empty set of nodes,
  • T r U T1 U T2 U T3 U Tn
  • with the following properties
  • 1. A designated node of the set r, is called the
    root of the tree
  • 2. The remaining nodes are partitioned into n gt
    0 subsets T1, T2, .. Tn each of which is a tree.

5
Binary Tree formal definition
  • A binary tree is a set T of nodes such that
    either
  • T is empty, or
  • T is parameterized into three disjoint sets
  • A single node r, the root
  • Two possibly empty sets that are binary trees
    called left and right subtrees of r.
  • Example Binary trees to represent algebraic
    expressions.

6
Terminology
  • Trees are used to represent relationships items
    in a tree are referred to as nodes and the lines
    connecting the nodes that express the
    hierarchical relationship are referred to as
    edges.
  • The edges in a tree are directed.
  • Trees are hierarchical which means that a
    parent-child relationship exist between the nodes
    in the tree.
  • Each node has at most one parent. The node with
    no parents is the root node.
  • The nodes that have no successors (no children
    nodes) are known as leaf nodes.

7
More Definitions
  • The degree of a node is the number of subtrees
    associated with that node.
  • A node of degree 0 is the leaf node.
  • In a binary tree the degree of each node except
    the leaf nodes is 2.
  • Level of a node of tree is measured from its
    root, whereas height of a node is measured from
    its root to the leaf. Height of a leaf node is 0.

8
Basic Tree Anatomy
Level 0
Level 1
Level 2
9
N-nary Tree
  • An n-nary tree T is a finite set of nodes with
    the following properties
  • 1. Either the set id empty or
  • 2. The set consists of a root, R, and exactly N
    distinct N-ary trees. That is, the remaining
    nodes are partitioned into Ngt 0 sunbsets T0, T1,
    .. TN-1, each of which is an N-ary tree such that
    T R, T0, T1, .. TN-1.

10
Observation
  • An important concept used in the above definition
    is recursion. A tree is defined in terms of
    smaller trees.
  • This concept of defining a term by smaller of the
    kind is called recursive definition.
  • We will discuss recursion next class.

11
External and Internal Nodes
  • Theorem 9.1 (in your text) An N-ary tree with n
    gt 0 internal nodes contains (N-1)(n1) external
    nodes.
  • Theorem 9.2 Consider an N-ary tree T of height
    h gt 0. The maximum number of internal nodes in T
    is given by

(h1)
- 1
N
N - 1
12
Leaf nodes
  • Theorem 9.3 Consider an N-ary tree of height h
    gt 0. The maximum number of leaf nodes in T is
  • N

h
13
Binary Tree
  • A binary tree T is a finite set of nodes with the
    following properties
  • 1. Either the set is empty, T O. or
  • 2. The set consists of a root, r, and exactly two
    distinct binary trees T L and TR T r, TR, TL.
  • TL is called the left subtree and TR is called
    the right subtree.

14
Binary tree example
A - B / C
How do you traverse this tree?
15
Recursive Solutions
  • Recursion is an important problem solving
    approach that is an alternative to iteration.
  • These are questions to answer when using
    recursive solution
  • 1. How can you define the problem in terms of a
    smaller problem of the same type?
  • 2. How does each recursive call diminish the size
    of the problem?
  • 3. What instance of the problem can serve as the
    base case?
  • 4. As the problem size diminishes, will you reach
    this base case?

16
Example 1 Factorial of N
  • Iterative definition
  • Factorial(N) N (N-1) (N-2) 1 for any N gt
    0.
  • Factorial(0) 1
  • Recursive solution
  • 1. Factorial(N) N Factorial(N-1)
  • 2. Problem diminishing? yes.
  • 3. Base case Factorial(0) 1 base case does
    not have a recursive call.
  • 4. Can reach base case as problem diminishes? yes

17
Writing String Backwards
  • 1. Write a string of length N backwards in terms
    of writing a string of length (N-1) backwards.
  • 2. Base case
  • Choice 1 Writing empty string.
  • Choice 2 Writing a string of length 1.

18
WriteBackward(S)
  • if string is empty
  • do nothing // this is the base case
  • else
  • write the last character of S
  • WriteBackward(S - minus its last character

19
Defining Languages
  • A grammar states the rules of a language.
  • A recursive algorithm can be written based on
    this grammar that determines whether a given
    string is a member of the language gt
    recognition algorithm.
  • For expressing a grammar we use special symbols
  • X Y means X or Y
  • X.Y or simply X Y means X followed by Y
  • ltxyzgt an instance of entity xyz

20
A grammar for Java identifiers
  • ltidentifiergt ltlettergt ltidentifiergt ltlettergt
    ltidentifiergtltdigitgt
  • ltlettergt a b z A B CZ_
  • ltdigitgt 0 1 9

21
Recognition algorithm for identifiers
  • boolean IsId (w)
  • // Returns TRUE if w is a legal Java
    identifier,
  • // Otherwise returns FALSE
  • if (w is of length 1)
  • if (w is a letter)
  • return true
  • else
  • return false
  • else if (last char of w is a letter or a digit)
  • return IsId(w minus its last char)
  • else return false

22
Tree Traversals
  • Preorder, inorder and post order.
  • Pre, In and Post refer to the order in which root
    is traversed.
  • PreOder traversal Visit root, traverse TL,
    Traverse TR.
  • InOrder traversal Traverse TL, visit root,
    traverse TR.
  • PostOrder traversal Traverse TL, Traverse TR,
    and visit root.

23
Expression Trees

/

A
B
--
E
D
C
24
Implementing a Binary Tree
  • Examine Java API
  • Use the existing classes.
  • Extend existing classes.
  • Implement necessary interfaces.

25
Binary Search Tree
  • A binary search tree is a binary tree that is in
    a sense sorted according to the values in its
    nodes.
  • For each node n, a binary search tree satisfies
    the following three properties
  • 1. ns value is greater than all values in its
    left subtree TL.
  • 2. ns value is less than all its values in its
    right subtree TR.
  • 3. Both TL and TR are binary search tree.

26
Example Tree of names
Jane
Bob
Tom
Ellen
Alan
Wendy
Nancy
Where will you insert the name Randy? How about
Ian?
Write a Comment
User Comments (0)
About PowerShow.com