#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 -> n == 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 -> n = 10 ;
lst -> next = new list_node;
lst -> next -> n = 20 ;
lst -> next -> next = NULL;
printList(lst); printf( " \n " );
TREE t = new bt_node;
t -> n = 100 ;
t -> left = new bt_node;
t -> right = new bt_node;
t -> left -> n = 90 ;
t -> left -> left = NULL;
t -> left -> right = new bt_node;
t -> left -> right -> n = 95 ;
t -> left -> right -> left = NULL;
t -> left -> right -> right = NULL;
t -> right -> n = 110 ;
t -> right -> right = NULL;
t -> right -> left = new bt_node;
t -> right -> left -> n = 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 );
}
/* ********************************************************************** */
/* */
/* 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 -> n == 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 -> n = 10 ;
lst -> next = new list_node;
lst -> next -> n = 20 ;
lst -> next -> next = NULL;
printList(lst); printf( " \n " );
TREE t = new bt_node;
t -> n = 100 ;
t -> left = new bt_node;
t -> right = new bt_node;
t -> left -> n = 90 ;
t -> left -> left = NULL;
t -> left -> right = new bt_node;
t -> left -> right -> n = 95 ;
t -> left -> right -> left = NULL;
t -> left -> right -> right = NULL;
t -> right -> n = 110 ;
t -> right -> right = NULL;
t -> right -> left = new bt_node;
t -> right -> left -> n = 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 );
}