Data Structures & Algorithms Recursion and Trees.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
CS 171: Introduction to Computer Science II
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
1 Introduction to trees Instructor: Dimitrios Kosmopoulos.
Data Structures & Algorithms Recursion and Trees Richard Newman.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
IS 2610: Data Structures Recursion, Divide and conquer Dynamic programming, Feb 2, 2004.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
IS 2610: Data Structures Discuss HW 2 problems Binary Tree (continued) Introduction to Sorting Feb 9, 2004.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Recursion Continued Divide and Conquer Dynamic Programming.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Trees A non-linear implementation for collection classes.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Elementary Data Structures Jan 26, 2004
MCS680: Foundations Of Computer Science
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
UNIT III TREES.
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
TREES General trees Binary trees Binary search trees AVL trees
Data Structures & Algorithms
Trees.
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Presentation transcript:

Data Structures & Algorithms Recursion and Trees

Fundamental concept in math and CS Recursive definition Defined in terms of itself a N = a*a N-1, a 0 = 1 Recursive function Calls itself int exp(int base, int pow) { return (pow == 0 ? 1 : base*exp(base, pow- 1)); } Recursion

Recursive definition (and function) must: 1. have a base case – termination condition 2. always call a case smaller than itself All practical computations can be couched in a recursive framework! (see theory of computation) Recursion

Recursively defined structures e.g., binary tree Base case: Empty tree has no nodes Recursion: None-empty tree has a root node with two children, each the root of a binary tree Recursion

Widely used in CS and with trees... Mathematical recurrences Recursive programs Divide and Conquer Dynamic Programming Tree traversal DFS Recursion

Recursive algorithm – solves problem by solving one or more smaller instances of same problem Recurrence relation – factorial – N! = N(N-1)!, for N > 0, with 0! = 1. – In C++, use recursive functions Int factorial(int N) { if (N == 0) return 1; return N*factorial(N-1); } Recursive Algorithms

BTW, can often also be expressed as iteration E.g., can also write N! computation as a loop: int factorial(int N) { for (int t = 1, i = 1; i <= N; ++i) t *= i; return t; } Recursive Algorithms

Euclid's Algorithm is one of the oldest known algorithms Recursive method for finding the GCD of two integers int gcd(int m, int n) {// expect m >= n if (n == 0) return m; return gcd(n, m % n); } Base case Euclid’s Algorithm Recursive call to smaller instance

Recursive scheme that divides input into two (or some fixed number) of (roughly) equal parts Then makes a recursive call on each part Widely used approach Many important algorithms Depending on expense of dividing and combining, can be very efficient Divide & Conquer

Example: find the maximum element in an array a[N] (Easy to do iteratively...) Base case: Only one element – return it Divide: Split array into upper and lower halves Recursion: Find maximum of each half Combine results: Return larger of two maxima Divide & Conquer

Property 5.1: A recursive function that divides a problem of size N into two independent (non-empty) parts that it solves, recursively calls itself less than N times. Prf: T(1) = 0 T(N) = T(k) + T(N-k) + 1 for recursive call on size N divided into one part of size k and the other of size N-k Induct! Divide & Conquer

3 pegs N disks, all on one peg Disks arranged from largest on bottom to smallest on top Must move all disks to target peg Can only move one disk at a time Must place disk on another peg Can never place larger disk on a smaller one Legend has it that the world will end when a certain group of monks finishes the task in a temple with 40 golden disks on 3 diamond pegs Towers of Hanoi

Target peg Which peg should top disk go on first? Towers of Hanoi

How many moves does this take? Towers of Hanoi

Property 5.2: The recursive d&c algorithm for the Towers of Hanoi problem produces a solution that has 2N – 1 moves. Prf: T(1) = 1 T(N) = T(N-1) T(N-1) = 2 T(N-1) + 1 = 2N – 1 by induction Towers of Hanoi

Two other important D&C algorithms: Binary search MergeSort Algorithm Metric RecurrenceApprox. Soln. Binary Search comparisons C(N) = C(N/2)+1lg N MergeSort recursive calls A(N) = 2 A(N/2) + 1N MergeSort comparisons C(N) = 2 C(N/2) + NN lg N Divide & Conquer

In Divide & Conquer, it is essential that the subproblems be independent (partition the input) When this is not the case, life gets complicated! Sometimes, we can essentially fill up a table with values we compute once, rather than recompute every time they are needed. This is Dynamic Programming Issue – table may be too big! Dynamic Programming

Fibonacci Numbers: F[0] = 0 F[1] = 1 F[N] = F[N-1] + F[N-2] Horribly inefficient implementation: int F(int N) { if (N < 1) return 0; if (N == 1) return 1; return F(N-1) + F(N-2); } Dynamic Programming

How bad is this code? How many calls does it make to itself? F(N) makes F(N+1) calls! Exponential!!!! Dynamic Programming

Can we do better? How? Make a table – compute once (yellow shapes) Fill up table Dynamic Programming

Property 5.3: Dynamic Programming reduces the running time of a recursive function to be at most the time it takes to evaluate the functions for all arguments less than or equal to the given argument, treating the cost of a recursive call as a constant. Dynamic Programming

A mathematical abstraction Central to many algorithms Describe dynamic properties of algorithms Build and use explicit tree data structures Examples: Family tree of descendants Sports tournaments (Who's In?) Organization Charts (Army) Parse tree of natural language sentence File systems Trees

Rooted trees Ordered trees M-ary trees and binary trees Defn: A tree is a nonempty collection of vertices and edges such that there is exactly one path between each pair of vertices. Defn: A path is a list of distinct vertices such that successive vertices have an edge between them Defn: A graph in which there is at most one path between each pair of vertices is a forest. Types of Trees

Binary TreeTernary Tree internal node external node root leaf

Types of Trees Rooted TreeFree Tree root node child parent sibling

Tree Representation Binary TreeRepresentation

Tree Representation Ordered TreeRepresentation Use linked list for siblings at each level, Pointer to left child

A binary tree with N internal nodes has N+1 external nodes A binary tree with N internal nodes has 2N links: N-1 to internal nodes and N+1 to external nodes The level of a node is one higher than the level of its parent, with the root at level 0. The path length of a tree is the sum of the levels of all the tree’s nodes The internal path length is the sum of levels of internal nodes; external path length is sum of levels of external nodes. Properties of Trees

The external path length of any binary tree with N nodes is 2N greater than the internal path length The height of a binary tree with N internal nodes is at least lg N and at most N-1. The internal path length of a binary tree with N internal nodes is at least N lg(N/4) and at most N(N-1)/2. Properties of Trees

Given pointer to a tree, visit every node in the tree systematically Inorder: Visit the left subtree, visit the root, then visit the right subtree Preorder: Visit the root, visit the left subtree, visit the right subtree. Postorder: Visit the left subtree, visit the right subtree, visit the root. Tree Traversal

Generic recursive traversal code Preorder? Inorder? Postorder? Tree Traversal void traverse(link h, void visit(link)) { if (h == NULL) return; visit(h); traverse(h->left, visit); traverse(h->right, visit); }

Generic iterative traversal code Preorder? Inorder? Postorder? Tree Traversal void traverse(link h, void visit(link)) { STACK s(max); s.push(h); while (!s.empty()) { visit(h = s.pop()); if (h->right != 0) s.push(h->right); if (h->left != 0) s.push(h->left); }

Generic iterative traversal code Level order = top to bottom, left to right Tree Traversal void traverse(link h, void visit(link)) { QUEUE q(max); q.put(h); while (!q.empty()) { visit(h = q.get()); if (h->left != 0) q.put(h->left); if (h->right != 0) q.put(h->right); }

Count number of nodes Compute height Compute internal path length Display tree Basic Tree Algorithms int count(link h) { if (h == NULL) return 0; return 1 + count(h->left) + count(h->right); }

Count number of nodes Compute height Compute internal path length Display tree Basic Tree Algorithms int height(link h) { if (h == NULL) return -1; int u = height(h->left); int v = height(h->right); return 1 + (u > v ? u : v); }

Basic Tree Algorithms void printnode(Item x, int h) { for (int i=0; i<h; ++i) cout << “ “; cout << x << endl; } void show(link t, int h) { if (t == NULL) { printnode(‘*’,h); return; } show(h->left, h+1); printnode(t->item, h); show(h->right, h+1); } Display tree (order?)

Tournament construction Start with array (list of competitors) Develop into tree with matches Divide and conquer: Split in half Make tourney with left (first) half Make a tourney with right (last) half Make a new node with links to the two tourneys Single item tourney = leaf with that item Item in interior nodes? Winner of tourney! Tree Algorithms

Recursive Graph Traversal DFS = Depth-First Search Generalization of tree traversal methods Basis for many algorithms for processing graphs Code: Starting at any node v Visit v Recursively visit each unvisited neighbor of v If graph is connected, will visit every node Need to be able to mark nodes as visited Don’t need to do this for trees (why not?) Set of edges on which calls are made forms a spanning tree Graph Traversal

Recursive Graph Traversal: DFS Property 5.10: DFS requires time O(V+E) in a graph with V vertices and E edges using the adjacency lists representation Adjacency list representation: one list node corresponding to each edge in the graph, and one list head pointer corresponding to each vertex in the graph. Graph Traversal void traverse(int k, void visit(int)) { visit(k); visited[k] = TRUE; for (link t=adj[k]; k!=0; t = t->next) if (!visited[t->v]) traverse(t->v, visit); }

BFS in graph Graph Traversal void traverse(int k, void visit(int)) { QUEUE q(V*V); q.put(k); while (!q.empty()) { if (!visited[k = q.get()] ) { visit(k); visited[k] = 1; for (link t=adj[k]; t!=0; t=t->next) if (!visited[t->v]) q.put(t->v); }