求树中某结点的父结点(长子-兄弟表示法)

数据结构:

  struct CSNode;

  typedef struct CSNode * PCSNode;

  struct CSNode

  {

    DataType info;

    PCSNode lchild;

    PCSNode rsibling;

  };

  typedef struct CSNode *CSTree;    //树定义为根结点的指针

程序代码:

  PCSNode parent_cstree(PCSNode p,CSTree t)

  {

    PCSNode q,r;

    if(p == NULL)

      return NULL;

    if(p == t || t == NULL)

      return NULL;

    q = t->lchild;

    while(q != NULL)

    {

      if(q == p)

        return t;

      r = parent_cstree(p,q);

      if(r != NULL)

        return r;

      q = q->rsibling;

    }

    return NULL;

  }

posted @ 2012-09-10 18:56  毛毛hhmm  阅读(797)  评论(0编辑  收藏  举报