0% found this document useful (0 votes)
8 views

Public Class RecursionMethods

Uploaded by

barsubiasarah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Public Class RecursionMethods

Uploaded by

barsubiasarah
Copyright
© © All Rights Reserved
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) Math.ceil(Math.log(n + 1) / Math.log(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