递归与迭代算法(二)

#include  < stdio.h >

/* ********************************************************************** */
/*                                                                        */
/*  Recursive data structures (structural recursion)                      */
/*  打印链表 (Linked lists)                                               */
/*                                                                        */
/* ********************************************************************** */
struct  list_node
{
    
int  n;
    
struct  list_node  * next;
};
typedef 
struct  list_node  * LIST;

void  printList(LIST lst)
{
    
if  (lst)
    {
        printf(
" %d  " , lst -> n);
        printList(lst
-> next);
    }
}

/* ********************************************************************** */
/*                                                                        */
/*  Recursive data structures (structural recursion)                      */
/*  判断二叉树中是否存在指定值 (Binary trees)                                */
/*                                                                        */
/* ********************************************************************** */
struct  bt_node
{
    
int  n;
    
struct  bt_node  * left;
    
struct  bt_node  * right;
};
typedef 
struct  bt_node  * TREE;

//  Inorder traversal:
void  printTree(TREE t)
{
    
if  (t)
    {
        printTree(t
-> left);
        printf(
" %d  " , t -> n);
        printTree(t
-> right);
    }
}

//  Test if t contains i; return 1 if so, 0 if not.
int  contains(TREE t,  int  i) {
    
if  ( ! t)
        
return   0 ;
    
else   if  (t -> ==  i)
        
return   1 ;
    
else
        
return  contains(t -> left, i)  ||  contains(t -> right, i);
}


/* ********************************************************************** */
/*                                                                        */
/*  Tail-recursives VS Not tail-recursives                                */
/*  Tail-recursives: all recursive calls are tail calls and hence do      */
/*                   not build up any deferred operations                 */
/*  Not tail-recursives: its recursive call is not in tail position,      */
/*                       it builds up deferred multiplication operations  */
/*                       that must be performed after the final recursive */
/*                       call completes                                   */
/*                                                                        */
/* ********************************************************************** */

// INPUT: Integers x, y such that x >= y and y > 0
int  gcd( int  x,  int  y)     /*  Tail recursion  */
{
    
if  (y  ==   0 )
        
return  x;
    
else
        
return  gcd(y, x  %  y);
}
// INPUT: n is an Integer such that n >= 1
int  fact( int  n)           /*  Augmenting recursion  */
{
    
if  (n  ==   1 )
        
return   1 ;
    
else
        
return  n  *  fact(n  -   1 );
}

/* ********************************************************************** */
/*                                                                        */
/*  Order of function calling                                             */
/*                                                                        */
/* ********************************************************************** */

// function 1
void  recursiveFunction1( int  num) {
    printf(
" %d\n " , num);
    
if  (num  <   4 )
        recursiveFunction1(num 
+   1 );
}
/*
recursiveFunction(0)
printf(0)
         recursiveFunction(0+1)
         printf(1)
                  recursiveFunction(1+1)
                  printf(2)
                           recursiveFunction(2+1)
                           printf(3)
                                    recursiveFunction(3+1)
                                    printf(4)
*/

// function 2
void  recursiveFunction2( int  num) {
    
if  (num  <   4 )
        recursiveFunction2(num 
+   1 );
    printf(
" %d\n " , num);
}
/*
recursiveFunction(0)
         recursiveFunction(0+1)
                  recursiveFunction(1+1)
                           recursiveFunction(2+1)
                                    recursiveFunction(3+1)
                                    printf(4)
                           printf(3)
                  printf(2)
         printf(1)
printf(0)
*/

void  main()
{
    LIST lst 
=   new  list_node;
    lst
-> =   10 ;
    lst
-> next  =   new  list_node;
    lst
-> next -> =   20 ;
    lst
-> next -> next  =  NULL;
    printList(lst); printf(
" \n " );

    TREE t 
=   new  bt_node;
    t
-> =   100 ;
    t
-> left  =   new  bt_node;
    t
-> right  =   new  bt_node;
    t
-> left -> =   90 ;
    t
-> left -> left  =  NULL;
    t
-> left -> right  =   new  bt_node;
    t
-> left -> right -> =   95 ;
    t
-> left -> right -> left  =  NULL;
    t
-> left -> right -> right  =  NULL;
    t
-> right -> =   110 ;
    t
-> right -> right  =  NULL;
    t
-> right -> left  =   new  bt_node;
    t
-> right -> left -> =   105 ;
    t
-> right -> left -> left  =  NULL;
    t
-> right -> left -> right  =  NULL;
    printTree(t);
    printf(
" %s\n " , contains(t,  105 ?   " Contain 105! "  :  " Not contain 105! " );
    printf(
" %s\n " , contains(t,  106 ?   " Contain 106! "  :  " Not contain 106! " );

    recursiveFunction1(
5 );
    recursiveFunction2(
6 );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值