[Data Structure] Pre: Simple recursive procedures

Here are some simple examples to help you understand recursive procedures and their implementation. This knowledge is crucial when dealing with recursive data structures such as trees and graphs.

Factorial: Factorial is a common example of recursion. It represents multiplying a positive integer by all positive integers smaller than it. For example, the factorial of 4 is written as 4!, and its calculation is 4 × 3 × 2 × 1 = 24. Here's an example code that implements factorial using recursion:

def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

result = factorial(4)
print(result)  # Output: 24

In the above code, the factorial() function achieves recursion by calling itself. It returns 1 when n is 0 or 1. Otherwise, the function calls itself to calculate the factorial of n.

Fibonacci Sequence: The Fibonacci sequence is another common example of recursion. Each number in the sequence is the sum of the two preceding ones. For example, the sequence starts as 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Here's an example code that implements the Fibonacci sequence using recursion:

def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)

result = fibonacci(6)
print(result)  # Output: 8

In the above code, the fibonacci() function achieves recursion by calling itself. It returns n when n is less than or equal to 1. Otherwise, the function calls itself to calculate the sum of the previous two numbers.

Now, let's discuss the classic example of the Tower of Hanoi. The Tower of Hanoi puzzle involves three rods and a set of different-sized disks. The goal is to move all the disks from one rod to another while following the given rules:

  • Only one disk can be moved at a time.
  • Each move consists of taking the uppermost disk from one of the stacks and placing it on top of another stack.
  • No disk may be placed on top of a smaller disk.

Here's an example implementation of the Tower of Hanoi problem using recursion:

def tower_of_hanoi(n, source, destination, auxiliary):
    if n > 0:
        # Move n-1 disks from the source to auxiliary rod
        tower_of_hanoi(n - 1, source, auxiliary, destination)
        
        # Move the nth disk from the source to the destination rod
        print("Move disk", n, "from", source, "to", destination)
        
        # Move the n-1 disks from auxiliary to destination rod
        tower_of_hanoi(n - 1, auxiliary, destination, source)

# Testing the Tower of Hanoi function
tower_of_hanoi(3, 'A', 'C', 'B')

In the above code, the tower_of_hanoi() function recursively solves the Tower of Hanoi puzzle. It moves n-1 disks from the source rod to the auxiliary rod, then moves the nth disk from the source rod to the destination rod, and finally moves the n-1 disks from the auxiliary rod to the destination rod.

These examples illustrate the basic concepts of recursion. Recursive procedures break down a problem into smaller subproblems by calling themselves, and they terminate when the problem size becomes small enough to be directly solved. Recursion is particularly useful when dealing with data structures that exhibit recursive patterns, such as trees and graphs.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值