1 / 98

Trees

Trees. Graphs. List and trees belong to a broader set of structures called graphs: G = (V,E) V = vertex set E = edge set. Graphs. What graph is represented by this linked list? A --> B --> C --> D --> E. Graphs. What graph is represented by this linked list? A --> B --> C --> D --> E

patia
Download Presentation

Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 = { <A,B>, <B,C>, <C,D>, <D,E> }

  5. Digraphs • Graphs can either be directed (digraph) or undirected. A <--> B <--> C - undirected A --- B --- C - undirected A <-- B --> 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 = { <A,B>, <B,A>, <B,C>, <C,B>, <C,D>, <D,C>, <D,E>, <E,D> } 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={}? • V={A} and E={}? • V={A} and E={<A,A>}? • V={A,B} and E={<A,B>}? • V={A,B,C} and E={ <A,B>, <A,C> }? • V={A,B,C} and E={ <A,B>, <B,C> }? • V={A,B,C,D} and E={ <B,C>, <B,D> }? • V={A,B,C,D,E} and E={ <A,B>,<A,C>,<D,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.mData==value) return true; else if (value<n.mData) n = n.mLeft; 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 n=0, 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, 1+2, 1+2+3, 1+2+3+4, ..., 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. Let’s 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. Let’s 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 (n==0) return 1; } base cases inductive case

  44. Let’s 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 (n==0) return 1; if (n==1) return 1; } base cases inductive case

  45. Let’s 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 (n==0) return 1; if (n==1) return 1; return n * nFactorial( n-1 ); } base cases inductive case

  46. Let’s 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 (n==0) return 1; if (n==1) 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

More Related