递归实现对半搜索
int recursion_BinSearch(ListSet L, ElemType x, int low, int high){//递归对半搜索
if(low<=high){
int mid = (low+high)/2;
if(x<L.element[mid])
return recursion_BinSearch(L,x,low,mid-1);
else if(x>L.element[mid])
return recursion_BinSearch(L,x,mid+1,high);
else
return mid;
}
return -1;
}
int recursion_BinSearch(ListSet L, ElemType x){ //递归对半搜索
int i = recursion_BinSearch(L,x,0,L.n-1);
return i;
}
递归实现求树高
int Height(BinaryTreeNode *t){ //递归实现求树高
if(t == NULL){
return 0;
}
if(t->lChild == NULL && t->rChild == NULL){
return 1;
}
return max(Height(t->lChild),Height(t->rChild)) + 1;
}
递归交换子树
void Swap(BinaryTreeNode *t){ //递归交换子树
if(t == NULL)
return;
else{
BinaryTreeNode *temp = t->lChild;
t->lChild = t->rChild;
t->rChild = temp;
Swap(t->lChild);
Swap(t->rChild);
}
}
递归获取度为1的结点的个数
int getOne(BinaryTreeNode *t){ //递归获取度为1的结点的个数
if( t == NULL ){
return 0;
}
if( t->lChild==NULL && t->rChild==NULL ){
return 1;
}
else
return getOne(t->lChild) + getOne(t->rChild);
}
二叉搜索树插入的递归实现
int BSTreeNodeInsertR(BSTreeNode **tree,DataType x) //搜索树的插入
{
if(*tree == NULL)
{
*tree = BuyTreeNode(x);
return 0;
}
if ((*tree)->_data > x)
return BSTreeNodeInsertR(&(*tree)->_left,x);
else if ((*tree)->_data < x)
return BSTreeNodeInsertR(&(*tree)->_right,x);
else
return -1;
}