今天ooxx研究院给大家发一个关于用C++ 递归算法 从小到大输出二叉排序树中所有值不小于x的关键字的源码

void Out(BiTree t, KeyType x, KeyType &a, KeyType &b)
{
if( t )
if( x == t->data.key )
{
Out( t -> lchild, x, a, b );
Out( t -> rchild, x, a, b );
}
else if( x < t->data.key )
{
b = t -> data.key;
Out( t -> lchild, x, a, b );
}
else{a = t -> data.key;
Out( t -> rchild, x, a, b );
}
}
void OutX(BiTree t, KeyType x, KeyType &a, KeyType &b)
/* a: Return the nearest and smaller value to x, */
/* but return MINV if no the value in t. */
/* b: Return the nearest and larger value to x. */
/* but return MAXV if no the value in t. */
{
a = \'<\’; b = \’>\’;
Out( t, x, a, b );
}

评论被关闭。