!求二叉樹中從根結點到葉子結點的路徑

來源:互聯網
上載者:User

/*exp7-3.cpp*/
#include<stdio.h>
#include<malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
 ElemType data;/*資料元素*/
 struct node *lchild;/*指向左孩子*/
 struct node *rchild;/*指向右孩子*/
}BTNode;
extern void CreateBTNode(BTNode * &b,char *str);/*在algo7-1.cpp檔案中*/
extern void DispBTNode(BTNode *b);
void AllPath(BTNode *b)/*採用非遞迴方法輸出從根結點到葉子結點的路徑*/
{
 struct snode
 {
  BTNode *node;/*存放當前結點指標*/
  int parent;/*存放雙親結點在隊列中的位置*/
 }Qu[MaxSize];/*定義順序隊列*/
 int front,rear,p;/*定義隊頭和隊尾指標*/
 front=rear=-1;/*置隊列為空白隊列*/
 rear++;
 Qu[rear].node=b;/*根結點指標進入隊列*/
 Qu[rear].parent=-1;/*根結點沒有雙親結*/
 while(front<rear)/*隊列不定*/
 {
  front++;
  b=Qu[front].node;/*隊頭出隊列*/
  if(b->lchild && b->rchild==NULL)/*b為葉子結點*/
  {
   printf("%c到根結點路徑:",b->data);
   p=front;
   while(Qu[p].parent!=-1)
   {
    printf("  %c",Qu[p].node->data);
    p=Qu[p].parent;
   }
   printf("%c\n",Qu[p].node->data);
  }
  if(b->lchild!=NULL)/*左孩子入隊列*/
  {
   rear++;
   Qu[rear].node=b->lchild;
   Qu[rear].parent=front;
  }
  if(b->rchild!=NULL)/*右孩子入隊列*/
  {
   rear++;
   Qu[rear].node=b->rchild;
   Qu[rear].parent=front;
  }
 }
}

void AllPath1(BTNode *b,ElemType path[],int pathlen)/*採用遞迴方法輸出從根結點到葉子結點的路徑*/
{
 int i;
 if(b!=NULL)
 {
  if(b->lchild==NULL && b->rchild==NULL)/**b為葉子結點*/
  {
   printf("%c到根結點路徑:%c",b->data,b->data);
   for(i=pathlen-1;i>=0;i--)
    printf("%c",path[i]);
   printf("\n");
  }
  else
  {
   path[pathlen]=b->data;/*將當前結點放入路徑當中*/
   pathlen++;/*路徑長度增加1*/
   AllPath1(b->lchild,path,pathlen);/*遞迴掃描左子樹*/
   AllPath1(b->rchild,path,pathlen);/*遞迴掃描右子樹*/
   pathlen--;/*恢複環境*/
  }
 }
}

void LongPath(BTNode *b,ElemType path[],int pathlen,ElemType longpath[],int &longpathlen)
{
 int i;
 if(b==NULL)
 {
  if(pathlen>longpathlen)/*若當前路徑更長,將路徑儲存在longpath中*/
  {
   for(i=pathlen-1;i>=0;i--)
    longpath[i]=path[i];
   longpathlen=pathlen;
  }
 }
 else
 {
  path[pathlen]=b->data;/*將當前結點放入路徑中*/
  pathlen++;/*路徑長度增1*/
  LongPath(b->lchild,path,pathlen,longpath,longpathlen);/*遞迴掃描左子樹*/
  LongPath(b->rchild,path,pathlen,longpath,longpathlen);/*遞迴掃描右子樹*/
  pathlen--;/*恢複環境*/
 }
}

void DispLeaf(BTNode *b)/*輸出葉子結點*/
{
 if(b!=NULL)
 {
  if(b->rchild==NULL && b->lchild==NULL)
   printf("%c",b->data);
  else
  {
   DispLeaf(b->lchild);
   DispLeaf(b->rchild);
  }
 }
}

void main()
{
 BTNode *b;
 ElemType path[MaxSize],longpath[MaxSize];
 int i,longpathlen=0;
 CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
 printf("二叉樹b: ");DispBTNode(b);printf("\n\n");
 printf("b的葉子結點:");DispLeaf(b);printf("\n\n");
 printf("AllPath:\n");AllPath(b);printf("\n");
 printf("AllPath1:\n");AllPath1(b,path,0);printf("\n");
 LongPath(b,path,0,longpath,longpathlen);
 printf("第一條最長路徑長度:%d\n",longpathlen);
 printf("第一條最長路徑:");
 for(i=longpathlen;i>=0;i--)
  printf(" %c",longpath[i]);
 printf("\n");
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.