c語言實現二叉樹遍歷
『壹』 c語言實現二叉樹先序遍歷
你鐵定是哈工大
『貳』 二叉樹遍歷(c語言實現)
#include <stdio.h>
#include <malloc.h>
typedef struct node{ 
int data; 
struct node *lchild,*rchild; 
}*treetp,tree; 
treetp create (treetp t,int c); 
void print1(treetp); 
void print2(treetp);
void print3(treetp);
int number=0;
void main() 
{
  treetp t=0,r; 
  r=create (t,0); 
  printf("前序排列 :");
  print1  (r); 
  printf("\n中序排列 :");
  print2 (r);
  printf("\n後序排列 :");
  print3 (r);
} 
treetp  create(treetp t,int c) 
{ 
treetp p,di; 
do{ 
scanf("%d",&c); 
if (t==0) 
 { 
 t=(treetp)malloc(sizeof(tree)); 
 t->lchild=t->rchild=0; 
 t->data=c; 
 } 
else 
 {   p=t; 
  while(p!=0) 
  { 
  di=p; 
  if(c<(p->data)) 
  p=p->lchild; 
  else 
  p=p->rchild; 
  } 
if(c<(di->data)) 
{ 
treetp NEWdi=(treetp) malloc(sizeof(tree)); 
NEWdi->lchild=NEWdi->rchild=0; 
NEWdi->data=c; 
di->lchild=NEWdi; 
} 
else 
{ 
treetp NEWdi=(treetp) malloc(sizeof(tree)); 
NEWdi->lchild=NEWdi->rchild=0; 
NEWdi->data=c; 
di->rchild=NEWdi; 
} 
 } 
++number;
}while(c!=0); 
printf("葉子的數量:%d",number);
return t;
}   
void print1(treetp  t) 
{ 
   if (t!=0) 
  {     
   printf("%d ",t->data);
   print1(t->lchild); 
   print1(t->rchild); 
   } 
} 
void print2(treetp  t) 
{ 
   if (t!=0) 
  {     
   print2(t->lchild); 
   printf("%d ",t->data); 
   print2(t->rchild); 
   } 
} 
void print3(treetp  t) 
{ 
   if (t!=0) 
  {     
   print3(t->lchild);  
   print3(t->rchild); 
   printf("%d ",t->data);
   } 
} 
希望對你有幫助
『叄』 求二叉樹遍歷演算法C語言實現的
Status
PreOrderTraverse
(
BiTree
T,
Status
(
*Visit
)
(
TElemType
e
)
)
{
//
採用二叉鏈表存儲結構,Visit
是對數據元素操作的應用函數,先序遍歷二叉樹
T
的遞歸演算法。
if
(
T
)
{
//
若
T
不為空
if
(
Visit
(
T->data
)
)
//
調用函數
Visit
if
(
PreOrderTraverse
(
T->lchild,
Visit
)
)
//
遞歸調用左子樹
if
(
PreOrderTraverse
(
T->rchild,
Visit
)
)
return
OK;
//
遞歸調用右子樹
return
ERROR;
}
else
return
OK;
}
//
PreOrderTraverse
『肆』 用c語言實現二叉樹的層次遍歷(用非遞歸方法)
使用隊列,每出一個節點,將該節點的子節點入隊。so easy
『伍』 二叉樹的建立與遍歷(C語言)
樓主你好~~~「ф」字元的源代碼我忘記了,我這里有一個自己寫過的遍歷演算法
#include<iostream.h>
typedef struct btnode
{
 char data;
 struct btnode *Lchild,*Rchild;
}*bitreptr;
void Create(bitreptr &p)
{
 char n;
 p=new btnode;
 cin>>n;
 if(n!='#')
 {
   p->data=n;
   Create(p->Lchild);
   Create(p->Rchild);
 }
 else
  p=NULL;
 
}
void preorder(bitreptr &p)
{
 if(p)
 {
  cout<<p->data<<" ";
  preorder(p->Lchild);
  preorder(p->Rchild);
 }
}
void midorder(bitreptr &p)
{
 if(p)
 {
  midorder(p->Lchild);
  cout<<p->data<<" ";
  midorder(p->Rchild);
 }
}
void postorder(bitreptr &p)
{
 if(p)
 {
  postorder(p->Lchild);
  postorder(p->Rchild);
  cout<<p->data<<" ";
 }
}
void change(bitreptr &p)
{
 bitreptr t,q;
 if(p)
 {
  t=p->Lchild;
  q=p->Rchild;
  p->Lchild=q;
  p->Rchild=t;
  change(p->Lchild);
  change(p->Rchild);
 }
}
void main()
{
 char i;
 cout<<"請選擇所需功能('A'輸出該二叉樹序列,'B'輸出交換後二叉樹序列)"<<endl;
 cin>>i;
 bitreptr p;
 cout<<"輸入數據:";
 Create(p);
 switch(i)
 {
    case 'A':
  {
         cout<<"前序:";
         preorder(p);
         cout<<endl;
         cout<<"中序:";
         midorder(p);
         cout<<endl;
         cout<<"後序:";
         postorder(p);
         cout<<endl;
  }break;
 case 'B':
  {
   change(p);
   cout<<"交換二叉樹前序:";
         preorder(p);
         cout<<endl;
         cout<<"交換二叉樹中序:";
         midorder(p);
         cout<<endl;
         cout<<"交換二叉樹後序:";
         postorder(p);
         cout<<endl;
  }break;
 }
}
這個演算法輸入時要以「#」代表空節點,及將[測試數據]  「ABCффDEфGффFффф」改成「ABC##DE#G##F###」即可。另外我的演算法包括了二叉樹左右子樹交換的代碼「change(bitreptr &p)」,只要樓主稍作修改就可以得到你想要的完美結果~
『陸』 C語言二叉樹遍歷
你這個二叉樹是根據什麼遍歷排列的,你沒說清楚啊?
『柒』 遍歷二叉樹的實現(C語言)幫忙啊......
#include <stdio.h>
#include <stdlib.h>
#include<iostream.h>
#define maxnode 100
#define NULL  0
typedef char DataType; /*設數據域類型為字元型*/
typedef struct node{
      DataType data;
      struct node *lchild,*rchild; /*左右鏈域為指向結點結構的指針類型*/
}bintnode; /*結點類型*/
typedef bintnode *bintree; /*指向二叉樹結點的指針類型*/
void createbintree(bintree  *T);  /*構造二叉鏈表*/
void Preorder(bintree T);   /*先序*/
void inorderout(bintree T); /*中序*/
void Postorder(bintree T);  /*後序*/
void LevelOrder(bintree T); /*按層次遍歷二叉樹T*/
/*以加入結點的先序序列輸入,構造二叉鏈表*/
void createbintree(bintree  *T)
{
 char ch;
 scanf("\n%c",&ch);
 if(ch=='0')*T=NULL;
 else
 {
  *T=(bintnode*)malloc(sizeof(bintnode));
  (*T)->data=ch;
  createbintree(&(*T)->lchild);
  createbintree(&(*T)->rchild);
 }
}
/*先序遍歷二叉樹T*/
void Preorder(bintree T)
{
     if (T)
  {
   cout<<T->data; /*訪問結點數據*/
         Preorder(T->lchild); /*先序遍歷二叉樹T的左子樹*/
         Preorder(T->rchild); /*先序遍歷二叉樹T的右子樹*/
  }
}
/*中序遍歷二叉樹T*/
void inorderout(bintree T)
{
 if(T)
 {
  inorderout(T->lchild);  /*中序遍歷二叉樹T的左子樹*/
  cout<<T->data;  /*訪問結點數據*/
  inorderout(T->rchild);  /*中序遍歷二叉樹T的右子樹*/
 }
}
/*後序遍歷二叉樹T*/
void Postorder(bintree T)
{
    if (T)
 {
  Postorder(T->lchild);  /*後序遍歷二叉樹T的左子樹*/
        Postorder(T->rchild);  /*後序遍歷二叉樹T的右子樹*/
        cout<<T->data; /*訪問結點數據*/
 }
}
void LevelOrder(bintree bt)
{
 bintree queue[maxnode];
 int front,rear;
 if (bt==0)return;
 front=-1;    /*隊列初始化*/
 rear=0;
 queue[rear]=bt;  /*根結點入隊*/
 while(front!=rear)
 {
  front++;
  cout<<queue[front]->data;       /*訪問隊首結點的數據域*/
  if(queue[front]->lchild!=NULL)  /*將隊首結點的左孩子結點入隊列*/
  {
   rear++;
   queue[rear]=queue[front]->lchild;
  }//if
  if(queue[front]->rchild!=NULL)  /*將隊首結點的右孩子結點入隊列*/
  {
   rear++;
   queue[rear]=queue[front]->rchild;
  }//if
 }//while
}//LevelOrder
void main()
{
 bintree T;
    char a,b;
    cout<<"歡迎進入二叉樹基本操作測試程序,請選擇:"<<endl;
    a='y';
    while(a=='y' || a=='Y')
 {
      cout<<"1-------------------------二叉樹建立"<<endl;
         cout<<"2-------------------------先序遍歷!"<<endl;
         cout<<"3-------------------------中序遍歷!"<<endl;
         cout<<"4-------------------------後序遍歷!"<<endl;
         cout<<"5-------------------------層次遍歷!"<<endl;
         cout<<"6-------------------------退出!"<<endl;
         cin>>b;
         switch(b)
   {
               case '1': cout<<"請按帶空指針的二叉樹先序輸入建立二叉樹存儲的結點序列:"<<endl;
                         createbintree(&T);cout<<endl;break;
               case '2': cout<<"該二叉樹的先根序遍歷序列為:"<<endl;
                         Preorder(T);cout<<endl;break;
               case '3':cout<<"該二叉樹的中根遍歷序列為:"<<endl;
                        inorderout(T);cout<<endl;break;
               case '4':cout<<"該二叉樹的後根遍歷序列為:"<<endl;
                        Postorder(T);cout<<endl;break;
               case '5':cout<<"該二叉樹的層次遍歷序列為:"<<endl;
                        LevelOrder(T);cout<<endl;break;
               case '6':a='n';break;
               default:a='n';
   }//switch
 }//while
}//main
『捌』 怎麼用c語言實現二叉樹的遍歷
這是用廣義表建立二叉樹並先序和中序遍歷二叉樹
#include <stdio.h>
 #include <stdlib.h>
 #define MaxSize 100
 typedef struct node
  {
     char data;
     struct node *lchild;
     struct node *rchild;
  }BTNode,*BiTree;
  void creategeneralizelist(BiTree *b,char *str)
   {
      BTNode *St[MaxSize],*p=NULL;
      int top=-1,flag,j;
      char ch;
      for(j=0;(ch=str[j])!='#';j++)
        {
          switch(ch)
           {
             case '(':
               top++;
               St[top]=p;
               flag=1;
               break;
             case ')':
               top--;
               break;
             case ',':
               flag=2;
               break;
             default:
               p=(BiTree)malloc(sizeof(BTNode));
               p->data=ch;
               p->lchild=NULL;
               p->rchild=NULL;
             if(*b==NULL)
               *b=p;
             else
             {
               switch(flag)
               {
                case 1:
                  St[top]->lchild=p;
                  break;
                case 2:
                  St[top]->rchild=p;
                  break;
               }
             }
           }
         }
    }
void PreOrder(BiTree T)
     {
        if(T)
          {
             printf("%2c",T->data);
             PreOrder(T->lchild);
             PreOrder(T->rchild);
          }
     }
   void InOrder(BiTree T)
     {
       if(T)
        {
          InOrder(T->lchild);
          printf("%2c",T->data);
          InOrder(T->rchild);
        }
     }
int main(void)
      {
         BiTree T=NULL;
         char str[MaxSize];/*用於保存用戶輸入的字元*/
         printf("please input a string end with #:\n");
         scanf("%s",str);
         creategeneralize_list(&T,str);
         printf("the result ofInOrder BiTree is:\n");
         /* PreOrder(T);*/
         InOrder(T);
         getch();
         return 1;
      }
『玖』 C語言二叉樹的創建和遍歷
我寫了一個二叉樹
  你給看看
一定能行的
 我自己用了
 #include "stdio.h"
 #include "malloc.h"
 #include "string.h"
 #include "stdlib.h"
 #define Max 20
  
 //結點的最大個數
 typedef struct BinTNode{
  
char data;
  
struct BinTNode *lchild,*rchild;
 }BinTNode,*BinTree;
  
    //自定義二叉樹的結點類型
    //定義二叉樹的指針
 int NodeNum,leaf;
  
    //NodeNum為結點數,leaf為葉子數
  
//==========以廣義表顯示二叉樹==============
 void DisTree(BinTree T)
 {
  if(T)
  {
   printf("%c",T->data);
   if((T->lchild)||(T->rchild))
   {
    if(T->lchild)
    {
  
  printf("%c",'(');
  
  DisTree(T->lchild);
    }
  
  if(T->rchild)
    {
  
  printf("%c",',');
  
  DisTree(T->rchild);
  
  printf("%c",')');
    }
   }
  }
 }
 //==========基於先序遍歷演算法創建二叉樹==============
 //=====要求輸入先序序列,其中加入虛結點"#"以示空指針的位置==========
 BinTree CreatBinTree(BinTree T)
 {
  
char ch;
   ch=getchar();
   if(ch=='#')
    T=NULL;
   else
  {
    if(!(T=(BinTNode *)malloc(sizeof(BinTNode))))
  
printf("Error!");
    T->data=ch;
    T->lchild=CreatBinTree(T->lchild);
    T->rchild=CreatBinTree(T->rchild);
   }
   return T;
   }
 //========NLR 先序遍歷=============
 void Preorder(BinTree T)
 {
    if(T)
  {
    printf("%c",T->data);
   Preorder(T->lchild);
   Preorder(T->rchild);
   }
  }
 //========LNR 中序遍歷===============
 void Inorder(BinTree T)
 {
    if(T){
   Inorder(T->lchild);
   printf("%c",T->data);
   Inorder(T->rchild);
  }
  }
 //==========LRN 後序遍歷============
 void Postorder(BinTree T)
 {
    if(T){
   Postorder(T->lchild);
   Postorder(T->rchild);
   printf("%c",T->data);
  }
  }
 //=====採用後序遍歷求二叉樹的深度、結點數及葉子數的遞歸演算法========
 int TreeDepth(BinTree T)
 {
  
int hl,hr,max;
  
if(T){
  
 hl=TreeDepth(T->lchild);
 //求左深度
   hr=TreeDepth(T->rchild);
 //求右深度
  
 max=hl>hr? hl:hr;
  
   //取左右深度的最大值
   NodeNum=NodeNum+1;
  
 //求結點數
  if(hl==0&&hr==0)
   leaf=leaf+1;  //若左右深度為0,即為葉子。
   return(max+1);
  
} 
    else return(0);
 }
 //====利用"先進先出"(FIFO)隊列,按層次遍歷二叉樹==========
 void Levelorder(BinTree T)
 {
  
int front=0,rear=1;
  
BinTNode *cq[Max],*p;
//定義結點的指針數組cq
  
cq[1]=T;
//根入隊
  
while(front!=rear)
{
   front=(front+1)%NodeNum;
   p=cq[front];
  
    //出隊
   printf("%c",p->data);
  //出隊,輸出結點的值
    if(p->lchild!=NULL){
  
 rear=(rear+1)%NodeNum;
  
  cq[rear]=p->lchild;
  //左子樹入隊
   }
   if(p->rchild!=NULL){
  
 rear=(rear+1)%NodeNum;
  
  cq[rear]=p->rchild;
  //右子樹入隊
  }
  
} 
} 
//==========主函數=================
 void main()
 {
  
BinTree T,root;
  
int i,depth;
  
printf("\n");
  
printf("輸入完全二叉樹的先序序列:"); //輸入完全二叉樹的先序序列,
// 用#代表虛結點,如ABD###CE##F##
  
root=CreatBinTree(T);
    //創建二叉樹,返回根結點
  
DisTree(root);
  printf("\n");
  do
//從菜單中選擇遍歷方式,輸入序號。
  {
   printf("\t********** 菜單 ************\n");
  
 printf("\n");
  
 printf("\t1: 先序遍歷\n");
  
  printf("\t2: 中序遍歷\n");
  
 printf("\t3: 後序遍歷\n");
   printf("\t4: 該樹的深度,結點數,葉子數\n");
  
 printf("\t5: 層次遍歷\n"); //按層次遍歷之前,先選擇4,求出該樹的結點數。
  
 p
『拾』 c語言 二叉樹的遍歷
//---------------------------------------------------------------------------
#include<iostream> 
using namespace std;
typedef struct node
{
 struct node *L,*R;
 string name;
}NODE;
//輸入
void Input(NODE **T,int num)
{
   string name;
   int L,R;
   *T = new NODE[num];
   for (int i=0;i<num;)
   {
     cout << "輸入第"<<i+1<<"個結點名稱、左右子序號:"<<endl;
     cin >> name >> L >> R;
     if(L >=num || R >=num)
     {
       cout << "輸入結點序號非法,請重新輸入"<<endl;
       continue;
     }
     (*T+i)->name = name;
     (*T+i)->L =  L==-1? NULL:*T+L;
     (*T+i)->R =  R==-1? NULL:*T+R;
     i++;
   }
}
//前序
void NLR(NODE *T)
{
  if(T==NULL)return ;
  cout <<T->name<<" ";
  NLR(T->L);
  NLR(T->R);
}
//中序
void LNR(NODE *T)
{
  if(T==NULL)return ;
  NLR(T->L);
  cout <<T->name<<" ";
  NLR(T->R);
}
//後序
void LRN(NODE *T)
{
  if(T==NULL)return ;
  NLR(T->L);
  NLR(T->R);
  cout <<T->name<<" ";
}
int main()
{
   NODE *T=NULL,t;
   int num;
   cout << "輸入結點數:"<<endl;
   cin >> num;
   Input(&T,num);
   cout <<"前序:";
   NLR(T);
   cout << endl;
   cout <<"中序:";
   LNR(T);
   cout << endl;
   cout <<"後序:";
   LRN(T);
   cout << endl;
   system("PAUSE");
   return 0;
}
