0% found this document useful (0 votes)
25 views1 page

Recursive Algorithms in Java

Uploaded by

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

Recursive Algorithms in Java

Uploaded by

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

public class RecursionMethods {

// Fibonacci sequence

public static int fibonacci(int x) {

if (x <= 1) return x;

return fibonacci(x - 1) + fibonacci(x - 2);

// Power calculation

public static int power(int base, int pow) {

if (pow == 0) return 1;

return base * power(base, pow - 1);

// Factorial calculation

public static int factorial(int n) {

if (n == 0) return 1;

return n * factorial(n - 1);

// Minimum height of an AVL tree for a given number of nodes

public static int minHeightAVL(int n) {

if (n <= 0) return 0;

return (int) [Link]([Link](n + 1) / [Link](2));

// Maximum height of an AVL tree for a given number of nodes

public static int maxHeightAVL(int n) {

if (n <= 1) return n;

return 1 + maxHeightAVL(n - 1);

You might also like