0% found this document useful (0 votes)
10 views10 pages

Dsa Tuto4 1220501

The document discusses recursion and provides examples of recursion concepts including recursive functions, tail recursion, direct and indirect recursion, and nested and excessive recursion. It also provides programming questions to implement recursion such as printing a string multiple times recursively, reversing a linked list recursively, summing the elements of an array recursively, and reversing a stack recursively.

Uploaded by

annas.fikri003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Dsa Tuto4 1220501

The document discusses recursion and provides examples of recursion concepts including recursive functions, tail recursion, direct and indirect recursion, and nested and excessive recursion. It also provides programming questions to implement recursion such as printing a string multiple times recursively, reversing a linked list recursively, summing the elements of an array recursively, and reversing a stack recursively.

Uploaded by

annas.fikri003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SKJ2023 STRUKTUR DATA DAN ALGORITMA

(DATA STRUCTURES AND ALGORITHM)


SEMESTER 1 2023/2024

TUTORIAL AND LAB 4: RECURSIVE


GROUP LAB: MSC04
LECTURER NAME: PROF. MADYA DR. FARIDA HAZWANI MOHN RIDZUAN

NAME MATRIC NUMBER

MUHAMMAD ANNAS FIKRI BIN IDRUS 1220501


1. Define recursion.

Recursive are programming concept that define themselves or self-reference

2. State 2 points in devising a recursive solution.

• Identify a base case that is easily solvable.


• Identify a recursive case that breaks the problem into smaller problem of the same
form.

3. State the applications of recursive

• Fibonacci Series
• Linear Search
• Binary Search
• Quick Sort
• Merge Sort
• Tower of Hanoi
• Tree Traversals
• Graph Traversals

4. State the differences between tail and nontail recursion.


Tail recursive Nontail recursive

The recursive call is the last operation within the Some operations occur after the recursive call.
function and there’s no other work to do after the The function needs to perform additional
recursive call. It can directly return the result of operations using the returned value from the
the recursive call. recursive call before it can return its result.

5. State the difference between direct and non direct recursion.


Direct recursion Non direct recursion

A function calls itself directly in its own A method calls themselves indirectly
body. through calling other method and involves
at least two functions.
6. Provide an example (must not be from the lecture notes) of a nested recursion.

public class NestedRecursion { public


static void main(String[] args) {
printNumberTriangle(5); }

private static void printNumberTriangle(int n) { if


(n <= 0) { return;
}

for (int i = 1; i <= n; i++) {

printNumberLine(i); }

for (int i = n - 1; i >= 1; i--) {


printNumberLine(i);
}
}

private static void printNumberLine(int n) {


for (int i = 1; i <= n; i++) {
System.out.print(i + " ");
}
System.out.println();
}

}
7. Provide an example (must not be from the lecture notes) of an excessive recursion.

public class ExcessiveRecursion {

public static void main(String[] args) {


recursiveMethod(5);
}
private static void recursiveMethod(int n) { if
(n <= 0) { return; }

System.out.println(n);
recursiveMethod(n - 1);
}
private static void excessive(int n) {
if (n <= 0) { return;
}

System.out.println(n);
excessive(n - 1); excessive(n - 1);
}
}
Programming Questions

8. Implement iteration concept using for loop to print “Hi” three times.
public class Iteration { public
static void main(String[] args){

for (int i = 0; i < 3; i++){


System.out.println("Hi");
}
}
}
9. Use the codes given in Figure 1 to change the implementation of the same program in
Question 8 using recursion concept.

public class RecursionTutorial


{ public static void main (String[] args)
{ sayHi();
}
private static void sayHi()
{ System.out.println(“Hi!”);
sayHi();
}
}
Figure 1

public class RecursionTutorial{


public static void main(String[] args) {
sayHi(3);
}
private static void sayHi(int n) { if
(n <= 0) { return;
}
System.out.println("Hi!"); sayHi(n
- 1);
}
}

10. For these questions, you need to use recursion and iteration concept.

1. Print reverse using of a linked list


class Node {
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}

public class ReverseLinkedList {


static Node head;

public static void printList(Node head) {


Node ptr = head;
while (ptr != null) {
System.out.print(ptr.data + " -> ");
ptr = ptr.next;
}
System.out.println("NULL");
}
public static void push(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}

public static void recursiveReverse(Node


head) {
if (head == null || head.next == null) {
ReverseLinkedList.head = head;
return;
}

Node rest = head.next;


recursiveReverse(rest);
head.next.next = head;
head.next = null;
}
public static void reverse() {
recursiveReverse(head);
}

public static void main(String[] args) {

int[] keys = { 10, 20, 30, 40, 50, 60 };


int n = keys.length;

head = null;
for (int i = n - 1; i >= 0; i--) {
push(keys[i]);
}
reverse();
printList(head);
}
}
2. Sum of array elements

import java.util.Scanner;

public class ArraySumRecursion {


public static int sumOfArray(int[] array, int n)
{ if (n <= 0) { return 0;

}
return array[n - 1] + sumOfArray(array, n - 1);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter your array size: ");

int n = scanner.nextInt(); int[]


array = new int[n];

for (int j = 0; j < n; j++) {


System.out.print("Enter array[" + j + "]: ");
array[j] = scanner.nextInt();

int sum = sumOfArray(array, n);


System.out.println("Sum of Array Elements is: " + sum);

scanner.close();
}

}
3. Reverse a stack
import java.util.EmptyStackException;
import java.util.Stack;

public class ReverseStack {


static Stack<Integer> stack = new Stack<>();

public static void reverse() {


if (!stack.isEmpty()) {
int top = pop();
reverse();
insertAtBottom(top);
}
}

public static void insertAtBottom(int item) {


if (stack.isEmpty()) {
push(item);
} else {
int top = pop();
insertAtBottom(item);
push(top);
}
}

public static void printStack() {


for (int i = 0; i < stack.size(); i++) {
System.out.print(stack.get(i) + " ");
}
System.out.println();
}

public static void push(int num) {


stack.push(num);
}

public static int pop() {


try {
return stack.pop();
} catch (EmptyStackException e) {
System.out.println("Stack is Empty...");
return -1;
}
}

public static void main(String[] args) {


push(10);
push(20);
push(30);
push(40);
push(50);
push(60);

System.out.println("Before reversing the stack:");


printStack();

reverse();

System.out.println("After reversing the stack:");


printStack();
}
}
.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy