Trees - PowerPoint PPT Presentation

1 / 98
About This Presentation
Title:

Trees

Description:

List and trees belong to a broader set of structures ... A set of trees is called a forest. Arity = max branching factor per node ... Binary Search Trees (BST) ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 99
Provided by: georgejgr
Category:
Tags: trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
2
Graphs
  • List and trees belong to a broader set of
    structures called graphs
  • G (V,E)
  • V vertex set
  • E edge set

3
Graphs
  • What graph is represented by this linked list?
  • A -- B -- C -- D -- E

4
Graphs
  • What graph is represented by this linked list?
  • A -- B -- C -- D -- E
  • G (V,E)
  • V A, B, C, D, E
  • E , , ,

5
Digraphs
  • Graphs can either be directed (digraph) or
    undirected.
  • A B C - undirected
  • A --- B --- C - undirected
  • A C - directed
  • We will restrict the remainder of our discussion
    to digraphs.

6
Representing digraphs
  • We represented our digraph with a linked list
    structure.
  • We can also represent a digraph with an incidence
    matrix.

7
What is the underlying graph for this doubly
linked list?
  • A B C D E

8
What is the underlying graph for this doubly
linked list?
  • A B C D E
  • G (V,E)
  • V A, B, C, D, E
  • E , , , , , ,
    ,
  • What is the corresponding incidence matrix?

9
What is the underlying graph for this doubly
linked list?
  • A B C D E

10
Is that all there is (i.e., singly linked or
doubly linked lists)?
  • A B C D E
  • Note that we can have one link per node.

11
Trees
  • Special node called the root node.
  • Appears at top of tree by convention.
  • Terminal nodes are called leaf nodes.
  • A special type of graph called a tree (AKA
    arborescence) digraph w/ exactly 1 path from
    root to all other nodes.
  • A set of trees is called a forest.
  • Arity max branching factor per node
  • Arity of 2 is called a binary tree

12
Tree example
root node
  • A
  • B C
  • D E F
  • G H I

Subtree rooted at node E.
13
Representing trees
  • A
  • B C
  • D E F
  • G H I

Can we represent the tree with an incidence
matrix?
14
Representing trees
  • A
  • B C
  • D E F
  • G H I

15
Representing trees
  • A
  • B C
  • D E F
  • G H I

left link
right link
How can we represent a tree as a linked structure?
16
Representing trees
  • A
  • B C
  • D E F
  • G H I

public class MyTree private class Node int
mData Node mLeft Node mRight private
Node mRoot
17
Visiting nodes in a tree
  • There are 3 ways do visit (process) the nodes in
    a tree preorder, inorder, and postorder.
  • Preorder
  • Process data in current node
  • Process left subtree
  • Process right subtree
  • An example of a recursive definition

18
Visiting nodes in a tree
  • Inorder
  • Process left subtree
  • Process data in current node
  • Process right subtree
  • An example of a recursive definition

19
Visiting nodes in a tree
  • Postorder
  • Process left subtree
  • Process right subtree
  • Process data in current node
  • An example of a recursive definition

20
Representing trees
  • A
  • B C
  • D E F
  • G H I
  • Preorder
  • Process data in current node
  • Process left subtree
  • Process right subtree

Preorder A B D G E H I C F
21
Representing trees
  • A
  • B C
  • D E F
  • G H I
  • Inorder
  • Process left subtree
  • Process data in current node
  • Process right subtree

Inorder G D B H E I A F C
22
Representing trees
  • A
  • B C
  • D E F
  • G H I
  • Postorder
  • Process left subtree
  • Process right subtree
  • Process data in current node

Postorder G D H I E B F C A
23
Trees?
  • G (V,E) where
  • V and E?
  • VA and E?
  • VA and E?
  • VA,B and E?
  • VA,B,C and E , ?
  • VA,B,C and E , ?
  • VA,B,C,D and E , ?
  • VA,B,C,D,E and E ,, ?

24
Binary Search Trees (BST)
  • For every subtree rooted at some node n w/ value
    v, all elements to the left are less than v and
    all elements to the right are greater than v.

25
BST example
  • 5
  • 3

26
BST example
  • 5
  • 3 92

27
BST example
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

28
Operations on BSTs
  • Search/find/contains
  • Add
  • Remove
  • class Node
  • int mData
  • Node mLeft null
  • Node mRight null
  • Node ( int value ) mData value

29
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean contains ( int value )

30
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean contains ( int value )
  • Node n mRoot
  • while (n!null)
  • if (n.mDatavalue) return true
  • else if (value
  • else n n.mRight
  • return false

31
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )

32
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )

Specifies the subtree to search.
33
Begin recursion review
34
n! (n factorial)
  • The number of ways n objects can be permuted
    (arranged).
  • For example, consider 3 things, A, B, and C.
  • 3! 6
  • ABC
  • ACB
  • CAB
  • CBA
  • BCA
  • BAC
  • The first few factorials for n0, 1, 2, ... are
    1, 1, 2, 6, 24, 120, ...

35
n! (n factorial)
  • n! for some non negative integer n is defined as
  • n! n (n-1) (n-2) 2 1
  • 0! is defined as 1.
  • From http//mathworld.wolfram.com/Factorial.html

36
n! (n factorial)
  • n! for some non negative integer n can be
    rewritten as
  • 0! 1 for n 0
  • 1! 1 for n 1
  • n! n (n-1)! for all other n 1

37
Triangular numbers
  • The triangular number Tn can be represented in
    the form of a triangular grid of points where the
    first row contains a single element and each
    subsequent row contains one more element than the
    previous one. The triangular numbers are
    therefore 1, 12, 123, 1234, ..., so the
    first few triangle numbers are 1, 3, 6, 10, 15,
    21, ...

38
Triangular numbers
  • which can also be expressed as
  • Tn 1 for n 1
  • Tn n Tn-1 for n 1
  • From http//mathworld.wolfram.com/TriangularNumber
    .html

39
Triangular numbers
  • A plot of the first few triangular numbers
    represented as a sequence of binary bits is shown
    below. The top portion shows T1 to T255, and the
    bottom shows the next 510 values.

40
Fibonacci numbers
  • The sequence of Fibonacci numbers begins 1, 1,
    2, 3, 5, 8, 13, 21, 34, 55, 89 ...

41
Back to n! (n factorial)
  • n! for some non negative integer n can be
    rewritten as
  • 0! 1 for n 0
  • 1! 1 for n 1
  • n! n (n-1)! for all other n 1

base cases inductive case
42
Lets code n! (n factorial)
  • n! for some non negative integer n can be
    rewritten as
  • 0! 1 for n 0
  • 1! 1 for n 1
  • n! n (n-1)! for all other n 1
  • public static int nFactorial ( int n )

base cases inductive case
43
Lets code n! (n factorial)
  • n! for some non negative integer n can be
    rewritten as
  • 0! 1 for n 0
  • 1! 1 for n 1
  • n! n (n-1)! for all other n 1
  • public static int nFactorial ( int n )
  • //base cases
  • if (n0) return 1

base cases inductive case
44
Lets code n! (n factorial)
  • n! for some non negative integer n can be
    rewritten as
  • 0! 1 for n 0
  • 1! 1 for n 1
  • n! n (n-1)! for all other n 1
  • public static int nFactorial ( int n )
  • //base cases
  • if (n0) return 1
  • if (n1) return 1

base cases inductive case
45
Lets code n! (n factorial)
  • n! for some non negative integer n can be
    rewritten as
  • 0! 1 for n 0
  • 1! 1 for n 1
  • n! n (n-1)! for all other n 1
  • public static int nFactorial ( int n )
  • //base cases
  • if (n0) return 1
  • if (n1) return 1
  • return n nFactorial( n-1 )

base cases inductive case
46
Lets code n! (n factorial)
  • n! for some non negative integer n can be
    rewritten as
  • 0! 1 for n 0
  • 1! 1 for n 1
  • n! n (n-1)! for all other n 1
  • public static int nFactorial ( int n )
  • //base cases
  • if (n0) return 1
  • if (n1) return 1
  • return n nFactorial( n-1 )

This is an example of a recursive function (a
function that calls itself)! To use this
function int result nFactorial( 10 )
47
Back to Triangular numbers
  • Tn 1 for n 1
  • Tn n Tn-1 for n 1
  • What is the base case(s)?
  • What is the inductive case?
  • How can we write the code for this?

48
Back to Fibonacci numbers
  • The sequence of Fibonacci numbers begins 1, 1,
    2, 3, 5, 8, 13, 21, 34, 55, 89 ...

What is the base case(s)? What is the inductive
case? How can we code this?
49
A note regarding recursion . . .
  • Calculations such as factorial, Fibonacci
    numbers, etc. are fine for introducing the idea
    of recursion.
  • But the real power of recursion (IMHO) is in
    traversing advanced data structures such as trees
    (covered in more advanced classes and used in
    such as applications as language parsing, games,
    etc.).

50
End recursion review
51
Recursion
  • When a function calls itself.
  • If unrestricted, this will go on forever.
  • Base case(s)
  • Restriction(s) to avoid forever
  • Typically what we should do when the value is
    null, 1, 0, the first value, and/or the last
    value.
  • Recursive case(s)
  • Actual recursive function call

52
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )
  • //base case(s)

Base case What should we do when n is null?
53
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )
  • //base case(s)
  • if (nnull) return false

54
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )
  • //base case(s)
  • if (nnull) return false

Base case What should we do if we find the
value?
55
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )
  • //base case(s)
  • if (nnull) return false
  • if (n.mDatavalue) return true

56
Operations on BSTs
Recursive case What should we do if value is
less than n.mData? (Where will the data have to
be then?)
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )
  • //base case(s)
  • if (nnull) return false
  • if (n.mDatavalue) return true

57
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )
  • //base case(s)
  • if (nnull) return false
  • if (n.mDatavalue) return true
  • //recursive case(s)
  • if (value
  • return containsRecursive( value, n.mLeft )

58
Operations on BSTs
Recursive case What remains?
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )
  • //base case(s)
  • if (nnull) return false
  • if (n.mDatavalue) return true
  • //recursive case(s)
  • if (value
  • return containsRecursive( value, n.mLeft )

59
Operations on BSTs
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )
  • //base case(s)
  • if (nnull) return false
  • if (n.mDatavalue) return true
  • //recursive case(s)
  • if (value
  • return containsRecursive( value, n.mLeft )
  • return containsRecursive( value, n.mRight )

60
Operations on BSTs
  • containsRecursive isnt really any better than
    contains.
  • So one may question the value of recursion.
  • Consider a toString method for a BST that returns
    a string of all of the values of mData in order.
  • With recursion, this is trivial.
  • Without recursion, this is extremely difficult.

61
Recursive toString method for BSTs
  • Inorder
  • Process left subtree
  • Process data in current node
  • Process right subtree

62
Recursive toString method for BSTs
  • class MyBST
  • private Node mRoot null
  • public String toString ( )
  • return toString( mRoot )
  • //Inorder Process left subtree
  • // Process data in current node
  • // Process right subtree
  • public String toString ( Node n )
  • //base case(s)
  • //recursive case(s)

63
Recursive toString method for BSTs
  • class MyBST
  • private Node mRoot null
  • public String toString ( )
  • return toString( mRoot )
  • //Inorder Process left subtree
  • // Process data in current node
  • // Process right subtree
  • public String toString ( Node n )
  • //base case(s)
  • //recursive case(s)

What is the base case(s)?
64
Recursive toString method for BSTs
  • class MyBST
  • private Node mRoot null
  • public String toString ( )
  • return toString( mRoot )
  • //Inorder Process left subtree
  • // Process data in current node
  • // Process right subtree
  • public String toString ( Node n )
  • //base case(s)
  • if (nnull) return
  • //recursive case(s)

65
Recursive toString method for BSTs
  • class MyBST
  • private Node mRoot null
  • public String toString ( )
  • return toString( mRoot )
  • //Inorder Process left subtree
  • // Process data in current node
  • // Process right subtree
  • public String toString ( Node n )
  • //base case(s)
  • if (nnull) return
  • //recursive case(s)

What is the recursive case(s)?
66
Recursive toString method for BSTs
  • class MyBST
  • private Node mRoot null
  • public String toString ( )
  • return toString( mRoot )
  • //Inorder Process left subtree
  • // Process data in current node
  • // Process right subtree
  • public String toString ( Node n )
  • //base case(s)
  • if (nnull) return
  • //recursive case(s)
  • return toString( n.mLeft ) n.mData
    toString( n.mRight )

67
Recursive toString method for BSTs
  • class MyBST
  • private Node mRoot null
  • public String toString ( )
  • return toString( mRoot )
  • //Inorder Process left subtree
  • // Process data in current node
  • // Process right subtree
  • public String toString ( Node n )
  • //base case(s)
  • if (nnull) return
  • //recursive case(s)
  • return toString( n.mLeft ) n.mData
    toString( n.mRight )

Try to do this w/out using recursion!
68
Recursive toString method for BSTs
Modify this to 1. have just one node per line
2. be preorder 3. indent each line according
to depth of node in tree This represents the
structure of our tree (sideways).
  • class MyBST
  • private Node mRoot null
  • public String toString ( )
  • return toString( mRoot )
  • //Inorder Process left subtree
  • // Process data in current node
  • // Process right subtree
  • public String toString ( Node n )
  • //base case(s)
  • if (nnull) return
  • //recursive case(s)
  • return toString( n.mLeft ) n.mData
    toString( n.mRight )

69
Remaining BST operations
  • Add a node
  • Remove a node

70
Add -52 where?
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

71
Add -52 where?
  • 10
  • 5 15
  • 1 7 11 62
  • -52 40 70

72
Add 6 where?
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

73
Add 6 where?
  • 10
  • 5 15
  • 1 7 11 62
  • 6 40 70

74
Add 13 where?
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

75
Add 13 where?
  • 10
  • 5 15
  • 1 7 11 62
  • 13 40 70

76
Add 43 where?
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

77
Add 43 where?
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70
  • 43

78
Add method observations
  • Always add at a leaf node
  • (Dont allow duplicates.)
  • Algorithm is similar to search/contains.

79
Recall recursive contains
  • class MyBST
  • private Node mRoot null
  • public boolean containsRecursive ( int value )
  • return containsRecursive( value, mRoot )
  • public boolean containsRecursive ( int value,
    Node n )
  • //base case(s)
  • if (nnull) return false //base case
  • if (n.mDatavalue) return true //base case
  • //recursive case(s)
  • if (valuevalue, n.mLeft )
  • return containsRecursive( value, n.mRight )

80
Add method (in progress)
  • class MyBST
  • private Node mRoot null
  • public void add ( int value )
  • if (mRootnull) mRoot new Node( value )
  • else add( value, mRoot )
  • public void add ( int value, Node parent )

81
Add method (in progress)
  • class MyBST
  • private Node mRoot null
  • public void add ( int value )
  • if (mRootnull) mRoot new Node( value )
  • else add( value, mRoot )
  • public void add ( int value, Node parent )
  • //base case(s)
  • assert parent!null //should never happen!
  • if (valueparent.mData) return //disallow
    duplicates

82
Completed add method
  • class MyBST
  • private Node mRoot null
  • public void add ( int value )
  • if (mRootnull) mRoot new Node( value )
  • else add( value, mRoot )
  • public void add ( int value, Node parent )
  • //base case(s)
  • assert parent!null //should never happen!
  • if (valueparent.mData) return //disallow
    duplicates
  • //possibly recursive case(s)
  • if (value
  • if (parent.mLeftnull) parent.mLeft new
    Node( value )
  • else add( value, parent.mLeft )
  • else
  • if (parent.mRightnull) parent.mRight
    new Node( value )
  • else add( value, parent.mRight )

83
Node removal method
  • Similar to contains and add but more difficult.

84
Node removal leaf node example
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

Removal of leaf node is trivial. Now you see it .
. .
85
Node removal leaf node example
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

Removal of leaf node is trivial. Now you see it .
. . now you dont!
86
Node removal interior node example
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

Removal of interior node is tricky! We can even
remove the root! Remember only one of
11 or 62 needs to exist for 15 to be an interior
node.
87
Node removal interior node example
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

Removal of interior node is tricky! We can even
remove the root! Remember only one of
11 or 62 needs to exist for 15 to be an interior
node.
88
Node removal interior node example
  • 10
  • 5 62
  • 1 7 40 70
  • 11

Removal of interior node is tricky! We can even
remove the root! Remember only one of
11 or 62 needs to exist for 15 to be an interior
node.
89
Node removal interior node example (alternative)
  • 10
  • 5 15
  • 1 7 11 62
  • 40 70

Removal of interior node is tricky! We can even
remove the root! Remember only one of
11 or 62 needs to exist for 15 to be an interior
node.
90
Node removal interior node example (alternative)
  • 10
  • 5 11
  • 1 7 62
  • 40 70

Removal of interior node is tricky! We can even
remove the root! Remember only one of
11 or 62 needs to exist for 15 to be an interior
node.
91
Node removal (but first, we will need an
additional function)
  • //this function adds node value to subtree
    parent assuming that
  • // value's data is greater than all data in
    subtree parent
  • private void insertRight ( Node value, Node
    parent )
  • //precondition(s)
  • assert value !null //should never happen!
  • assert parent!null //should never happen!
  • assert value.mDataparent.mData //should never
    happen!
  • //base and possibly recursive cases
  • if (parent.mRightnull)
  • parent.mRight value //base
  • else
  • insertRight( value, parent.mRight )
    //recursive

92
Node removal
  • public boolean remove ( int value )
  • if (mRootnull) return false
  • if (mRoot.mDatavalue) //remove root?
  • //if possible, make left node new root and add
    right to left
  • if (mRoot.mLeft!null)
  • insertRight( mRoot.mRight, mRoot.mLeft )
  • mRoot mRoot.mLeft
  • else if (mRoot.mRight!null)
  • mRoot mRoot.mRight
  • else
  • mRoot null
  • return true
  • //not removing the root
  • if (valuemRoot, mRoot.mLeft )
  • else return remove( value, mRoot, mRoot.mRight
    )

93
Node removal helper method
  • private boolean remove ( int value, Node parent,
    Node current )
  • if (currentnull) return false
  • if (current.mDatavalue)
  • //if possible, make left node new root and add
    right to left
  • if (current.mLeft!null)
  • insertRight( current.mRight, current.mLeft )
  • if (parent.mLeftcurrent) parent.mLeft
    current.mLeft
  • else parent.mRight current.mLeft
  • else if (current.mRight!null)
  • if (parent.mLeftcurrent) parent.mLeft
    current.mRight
  • else parent.mRight current.mRight
  • else
  • if (parent.mLeftcurrent) parent.mLeft
    null
  • else parent.mRight null
  • return true
  • //keep trying to find the node to remove
  • if (valuecurrent, current.mLeft )

94
BST efficiency
  • A BST is very efficient when balanced.
  • Balancing is a topic for future classes.
  • What does a BST degrade into when not balanced?
  • Height of a balanced BST?

95
Java and trees
  • Of course, Java already contains an efficient BST
    implementation called a TreeSet.
  • This implementation provides guaranteed log(n)
    time cost for the basic operations (add, remove
    and contains).
  • See http//java.sun.com/j2se/1.5.0/docs/api/java/u
    til/TreeSet.html

96
Games and trees (not BSTs)
  • Enumerate possibilities
  • Choose best alternative

97
Games and trees (not BSTs)
  • Note the arity is sometimes 3, 2, or 5.
  • Difference between children and siblings.

98
Games and trees (not BSTs)
  • Note the arity is sometimes 3, 2, or 5.
  • Difference between children and siblings.

class Node int mData Node mChild Node mSibl
ing
Write a Comment
User Comments (0)
About PowerShow.com