Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.

Slides:



Advertisements
Similar presentations
Linear Lists – Linked List Representation
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
© 2004 Goodrich, Tamassia Using Recursion1 Programming with Recursion.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Revision Using Recursion1 Recursion. Revision Using Recursion2 Recall the Recursion Pattern Recursion: when a method calls itself Classic example--the.
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
Chapter 3: Arrays, Linked Lists, and Recursion
Programming with Recursion
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Fall 2006CSC311: Data Structures1 Chapter 3 Arrays, Linked Lists, and Recursion Objectives –Using Arrays –Singly Linked Lists –Doubly Linked Lists –Circularly.
Chapter 3: Recursive Algorithms
Recursion.
Recursion Chapter 5 Outline Induction Linear recursion –Example 1: Factorials –Example 2: Powers –Example 3: Reversing an array Binary recursion –Example.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Data Structures Using C++ 2E Chapter 6 Recursion.
Introduction to Recursion by Dr. Bun Yue Professor of Computer Science 2013
Using Recursion1 Recursion © 2010 Goodrich, Tamassia.
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to Recursion CSCI 3333 Data Structures.
Chapter 12 Recursion, Complexity, and Searching and Sorting
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Recursion1 © 2013 Goodrich, Tamassia, Goldwasser.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
Lecture 6 of Computer Science II
Recursion 5/4/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Chapter 15 Recursion.
Recursion 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
5.13 Recursion Recursive functions Functions that call themselves
Searching – Linear and Binary Searches
Chapter 15 Recursion.
Recursions.
EEL 4854 IT Data Structures Linked Lists
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Programming with Recursion
Programming with Recursion
CS210- Lecture 3 Jun 6, 2005 Announcements
Sequences 6/1/2019 7:49 PM Using Recursion Using Recursion.
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Presentation transcript:

Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute the following steps: 1. make z's prev link refer to v 2. make z's next link refer to w 3. make w's prev link refer to z 4. make v's next link refer to z 3. Insertion in the Middle of a Doubly Linked List v z W

4. Removing from the Middle of a Doubly Linked List To remove a node v in the middle of a doubly linked list. We access the nodes u and w on either side of v using v's getPrev and getNext. To remove node v, we simply have u and w point to each other instead of to v. We refer to this operation as the linking out of v. We also null out v's prev and next pointers so as not to retain old references into the list.

v UW

Implementation of a Doubly Linked List

Circularly Linked Lists and Linked-List Sorting Circularly linked list : the same as singly linked list, each node in a circularly linked list has a next pointer and a reference to an element. There is no head or tail in a circularly linked list. The last node's next points back to the first node. There is no first or last node. If we traverse the nodes of a circularly linked list from any node by following next pointers, we will cycle through the nodes. Even though a circularly linked list has no beginning or end, we nevertheless need some node to be marked as a special node, which we call the cursor.

Some simple update methods for a circularly linked list: add(v): Insert a new node v immediately after the cursor; if the list is empty, then v becomes the cursor and its next pointer points to itself. remove(): Remove and return the node v immediately after the cursor,if the list becomes empty, the cursor is set to null. advance(): Advance the cursor to the next node in the list.

Java implementation of a circularly linked list

Recursion A way to fulfill repetition which occurs when a function refer to itself in its own definition. Example : Factorial Function For example, 5! = 5·4·3·2·1 = 120. To make the connection with methods clearer, we use the notation factorial(n) to denote n!. The factorial function can be defined in a manner that suggests a recursive formulation. factorial(5) = 5 · (4 · 3 · 2 · 1) = 5 · factorial(4). Thus, we can define factorial(5) in terms of factorial(4). In general, for a positive integer n, we can define factorial(n) to be n·factorial(n − 1).

This leads to the following recursive definition. The definition contains base cases, which are defined nonrecursively in terms of fixed quantities It also contains one or more recursive cases, which are defined by appealing to the definition of the function being defined.

Sorting Link List We show in following code the insertion-sort algorithm for a doubly linked list.

Recursion Trace

1. Linear Recursion A method is defined so that it makes at most one recursive call each time it is invoked. This type of recursion is useful when we view an algorithmic problem in terms of a first or last element plus a remaining set that has the same structure as the original set. Example1 : Summing the Elements of an Array Recursively

Recursion trace for an execution of LinearSum(A,n) with input parameters A = {4,3,6,2,5} and n = 5.

Example2 : Reversing an Array by Recursion Note that, in this algorithm, we actually have two base cases, namely, when i = j and when i > j. If n is odd, we will eventually reach the i = j case, and if n is even, we will eventually reach the i > j case.

Converting recursive algorithm to non- recursive algorithm. Easily done to algorithms uses tail recursion Tail recursion : when the algorithms use a liner recursion and make a recursive call as its very last operation. When an algorithm uses tail recursion, we can convert the recursive algorithm into a non-recursive one, by iterating through the recursive calls rather than calling them explicitly.

2. Binary Recursion When an algorithm makes two recursive calls, we say that it uses binary recursion. These calls can, for example, be used to solve two similar halves of some problem. Example 1: Summing Array elements we can sum the elements in A by: (i) recursively summing the elements in the first half of A; (ii) recursively summing the elements in the second half of A; (iii) adding these two values together

Recursion trace for the execution of BinarySum(0,8). the value of parameter n is halved at each recursive call. Thus, the depth of the recursion, that is, the maximum number of method instances that are active at the same time, is 1 + log 2 n Algorithm BinarySum uses an amount of additional space roughly proportional to this value. The running time of Algorithm BinarySum is still roughly proportional to n, however, since each box is visited in constant time when stepping through our algorithm and there are 2n − 1 boxes.

Example 2: Computing Fibonacci Numbers via Binary Recursion Recall that the Fibonacci numbers are recursively defined as follows: F 0 = 0 F 1 = 1 F i = F i − 1 + F i − 2 for i > 1.

In spite of the Fibonacci definition looking like a binary recursion, using this technique is inefficient in this case. It takes an exponential number of calls to compute the kth Fibonacci number in this way. Specifically, let n k denote the number of calls performed in the execution of BinaryFib(k). Then, we have the following values for the n k 's: n 0 = 1 n 1 = 1 n 2 = n 1 + n = = 3 n 3 = n 2 + n = = 5 n 4 = n 3 + n = = 9 n 5 = n 4 + n = = 15 n 6 = n 5 + n = = 25 n 7 = n 6 + n = = 41 n 8 = n 7 + n = = 67. we see that the number of calls more than doubles for each two consecutive indices. Thus, n k > 2 k/2, which means that BinaryFib(k) makes a number of calls that are exponential in k. In other words, using binary recursion to compute Fibonacci numbers is very inefficient.

Computing the kth Fibonacci number using linear recursion. Computing the kth Fibonacci number via linear recursion requires k method calls

3. Multiple Recursion Multiple recursion : when a method may make multiple recursive calls with that number potentially being more than two. Example : summation puzzles ( self study)

Recursion trace for an execution of PuzzleSolve(3,S,U), where S is empty and U = {a, b, c}. This execution generates and tests all permutations of a, b, and c. We show the permutations generated directly below their respective boxes