ch09_ex33 从大到小输出给定…

来源:互联网 发布:如何隐藏域名信息 编辑:程序博客网 时间:2024/06/10 01:22
9.33③ 编写递归算法,从大到小输出给定二叉排序树
中所有关键字不小于x的数据元素。要求你的算法的时
间复杂度为O(log2n+m),其中n为排序树中所含结点数,
m为输出的关键字个数。

实现下列函数:
void OrderOut(BiTree t, KeyType x,void(*visit)(TElemType));

二叉树的类型BiTree定义如下:
typedef struct {
    KeyType key; 
    ... ...  // 其他数据域
} ElemType;

typedef struct BiTNode {
    ElemType data;
    BiTNode *lchild,*rchild;
}BiTNode, *BiTree;

答案:
void OrderOut(BiTree t, KeyType x,void(*visit)(TElemType))
{

    if(t) //log2n 
    
       OrderOut(t->rchild,x,visit);  
       if(t->data.key>=x)     //m              //line 0008
       {    
          visit(t->data); 
           //if t ismore than x,it exists that t->lchild may be not less x
          OrderOut(t->lchild,x,visit);  //it is not useto compare again before line 0008 cores which will be executed inthe next loop
       }
    }
}  
   
0 0
原创粉丝点击