990 likes | 1.15k Views
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
E N D
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 G = (V,E) V = { A, B, C, D, E } E = { <A,B>, <B,C>, <C,D>, <D,E> }
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.
Representing digraphs • We represented our digraph with a linked list structure. • We can also represent a digraph with an incidence matrix.
What is the underlying graph for this doubly linked list? A B C D E
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?
What is the underlying graph for this doubly linked list? A B C D E
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.
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
Tree example root node A B C D E F G H I Subtree rooted at node E.
Representing trees A B C D E F G H I Can we represent the tree with an incidence matrix?
Representing trees A B C D E F G H I
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?
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; }
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
Visiting nodes in a tree • Inorder • Process left subtree • Process data in current node • Process right subtree • An example of a recursive definition
Visiting nodes in a tree • Postorder • Process left subtree • Process right subtree • Process data in current node • An example of a recursive definition
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
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
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
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> }?
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.
BST example 5 3
BST example 5 3 92
BST example 10 5 15 1 7 11 62 40 70
Operations on BSTs • Search/find/contains • Add • Remove class Node { int mData; Node mLeft = null; Node mRight = null; Node ( int value ) { mData = value; } }
Operations on BSTs class MyBST { private Node mRoot = null; public boolean contains ( int value ) { … } }
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; } }
Operations on BSTs class MyBST { private Node mRoot = null; public boolean containsRecursive ( int value ) { … } }
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.
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, ...
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
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
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, ...
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
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.
Fibonacci numbers • The sequence of Fibonacci numbers begins: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ...
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
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
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
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
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
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 );
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?
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?
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.).