二叉樹的遞歸創建&遍歷

//二叉樹的遞歸創建&遍歷

#include<iostream>

#include<cstdlib>

using namespace std;

 
//二叉樹的結構體

typedef char Datatype;

struct BinNode;

typedef BinNode *PBinNode;

struct BinNode

{

       Datatype data;

       PBinNode lchild;

       PBinNode rchild;

};

typedef BinNode *BinTree;

 

//創建二叉樹,先根創立

void createBinTree(BinTree &t)

{

       Datatype c;

       t=(BinTree)malloc(sizeof BinNode);

       cin>>c;

       if(c=='#')

              t=NULL;

       else

       {

              t->data=c;

              createBinTree(t->lchild);

              createBinTree(t->rchild);

       }

}

 

//先根遍歷

void preOrder(BinTree t)

{

       if(t==NULL)

              return ;

       else

       {

              cout<<(t->data)<<" ";

              preOrder(t->lchild);

              preOrder(t->rchild);

       }

}

 

//中根遍歷

void inOrder(BinTree t)

{

       if(t==NULL)

              return ;

       else

       {

              inOrder(t->lchild);

              cout<<(t->data)<<" ";

              inOrder(t->rchild);

       }

}

 

//後根遍歷

void postOrder(BinTree t)

{

       if(t==NULL)

              return ;

       else

       {

              postOrder(t->lchild);

              postOrder(t->rchild);

              cout<<(t->data)<<" ";

       }

}

void main()
{
 BinTree t;
 cout<<"先根創建二叉樹:"<<endl;
 createBinTree(t);
 cout<<"先根遍歷: ";
 preOrder(t);
 cout<<endl;
 cout<<"中根遍歷: ";
 inOrder(t);
 cout<<endl;
 cout<<"後根遍歷: ";
 postOrder(t);
 cout<<endl;
}

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include<stdio.h>

#include<stdlib.h>

 

#define CHAR

//爲了增強程序的多功能,定義CHAR時,用字符的處理模式

//當CHAR沒有被定義時,採用整數處理模式

//數據類型的定義

#ifdef CHAR

typedef char datatype;

#else

typedef int datatype;

#endif

 

typedef struct node

{

   datatype data;

   struct node *lchild,*rchild;

}bitree;

bitree *root;

int n;

char c;

 

//創建二叉樹

bitree *creat_preorder()

{

   bitree *t;

   datatype x;

 

   #ifdef CHAR

   printf("\n\t\t請輸入字符,以0作爲每個節點的結束標誌:");

   scanf("%c",&x);

// fflush(stdin);//清除緩衝區

   while((c=getchar())!='\n'&&c!=EOF);  //清除緩衝區另外的方法

   if(x=='0')t=NULL;

   #else

   printf("\n\t\t請輸入正整數以0作爲結束標誌:");

   scanf("%d",&x);

   if(x==0)t=NULL;

   #endif

 

   else

   {

      t=(struct node *)malloc(sizeof(bitree));

      t->data=x;

      t->lchild=creat_preorder();

      t->rchild=creat_preorder();

   }

   return(t);

}

 

//先根遍歷算法

void preorder(bitree *t)

{

   if(t!=NULL)

   {

      n=n+1;

      #ifdef CHAR

      printf("\tdata[%2d]=%3c",n,t->data);

      #else

      printf("\tdata[%2d]=%3d",n,t->data);

      #endif

      if(n%5==0)printf("\n");

      preorder(t->lchild);

      preorder(t->rchild);

   }

}

 

//中根遍歷算法

void  inorder(bitree *t)

{

   if(t!=NULL)

   {

      inorder(t->lchild);

      n=n+1;

      #ifdef CHAR

      printf("\tdata[%2d]=%3c",n,t->data);

      #else

      printf("\tdata[%2d]=%3d",n,t->data);

      #endif 

      if(n%5==0)printf("\n");

      inorder(t->rchild);

   }

}

 

//後根遍歷算法

void postorder(bitree *t)

{

   if(t!=NULL)

   {

      postorder(t->lchild);

      postorder(t->rchild);

      n=n+1;

      #ifdef CHAR

      printf("\tdata[%2d]=%3c",n,t->data);

      #else

      printf("\tdata[%2d]=%3d",n,t->data);

      #endif 

      if(n%5==0)printf("\n");

   }

}

 

main()

{

   bitree *bintree=creat_preorder();

   printf("\n先根序列:\n\n");

   preorder(bintree);

   n=0;

   printf("\n中根序列:\n\n");

   inorder(bintree);

   n=0;

   printf("\n後根序列:\n\n");

   postorder(bintree);

   n=0;

   printf("\n\n");

 

}

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

333333333333333333333333333333333333333333333333333333333333333333333333

 

#include <iostream.h>
#include <stdlib.h>

//#include <iostream.h>
//=#include<iostream> using namespace std;

//*************************************************************************************
//二叉樹結點類的定義
template<class T>
struct BTNode
{
    T data;
    BTNode <T> * Lchild,*Rchild;
    BTNode(T nodeValue = T(),BTNode<T>* leftNode = NULL,BTNode<T>* rightNode = NULL )
        :data(nodeValue),Lchild(leftNode),Rchild(rightNode){}       //可選擇參數的默認構造函數
};
//**************************************************************************************
//二叉樹的建立
template <class T>
void createBinTree(BTNode<T> * &root )
{
    BTNode<T>* p = root;
    BTNode<T>* k;
    T nodeValue ;
    cin>>nodeValue;
    if(nodeValue==-1)
    {
        root=NULL;
    }
    else
    {
        root=new BTNode<T>();
        root->data = nodeValue;
        createBinTree(root->Lchild);
        createBinTree(root->Rchild);
    }
}
//************************************************************************************
//二叉樹的先序遍歷
template <class T>
void preOrder( BTNode<T> * & p)
{
    if(p)
    {
        cout<<p->data<<" ";
        preOrder(p->Lchild);
        preOrder(p->Rchild);
    }
}
//**************************************************************************************
//二叉樹的中序遍歷
template <class T>
void inOrder(BTNode<T> * & p)
{
   
    if(p)
    {
        inOrder(p->Lchild);
        cout<<p->data<<" ";
        inOrder(p->Rchild);
    }
}
//**************************************************************************************
//二叉樹的後序遍歷
template <class T>
void levelOrder(BTNode<T> *& p)
{
    if(p)
    {
        levelOrder(p->Lchild);
        levelOrder(p->Rchild);
        cout<<p->data<<" ";
    }
}
//*************************************************************************************
//統計二叉樹中結點的個數
template<class T>
int countNode(BTNode<T> * & p)
{
    if(p == NULL) return 0;
    return 1+countNode(p->Lchild)+countNode(p->Rchild);
}
//***********************************************************************************
//求二叉樹的深度
template<class T>
int depth(BTNode<T> *& p)
{
    if(p == NULL)
        return -1;
    int h1 = depth(p->Lchild);
    int h2 = depth(p->Rchild);
    if(h1>h2)return (h1+1);
    return h2+1;
}
//***********************************************************************************
//二叉樹的消毀操作
template<class T>
BTNode<T>* destroy(BTNode<T>* p)                         //消毀函數,用來消毀二叉樹中的各個結點
    {
        if(p)
        {
            return destroy(p->Lchild);
            return destroy(p->Rchild);
            delete p;
        }
    }
//********************************************************************************
//主函數的設計
int main ()
{
    BTNode<int> * rootNode = NULL;
    int choiced = 0;
    while(true)
    {
        system("cls");
        cout<<"\n\n\n                              ---主界面---\n\n\n";
        cout<<"                     1、創建二叉樹                2、先序遍歷二叉樹\n";
        cout<<"                     3、中序遍歷二叉樹            4、後序遍歷二叉樹\n";
        cout<<"                     5、統計結點總數              6、查看樹深度    \n";
        cout<<"                     7、消毀二叉樹                0、退出\n\n";
        cout<<"             請選擇操作:";
        cin>>choiced;
        if(choiced == 0)
            return 0;
        else if(choiced == 1)
        {
            system("cls");
            cout<<"請輸入每個結點,回車確認,並以-1結束:\n";
            createBinTree(rootNode );
        }
        else if(choiced == 2)
        {
            system("cls");
            cout<<"先序遍歷二叉樹結果:\n";
            preOrder(rootNode);
            cout<<endl;
            system("pause");
        }
        else if(choiced == 3)
        {
            system("cls");
            cout<<"中序遍歷二叉樹結果:\n";
            inOrder(rootNode);
            cout<<endl;
            system("pause");
        }
        else if(choiced == 4)
        {
            system("cls");
            cout<<"後序遍歷二叉樹結果:\n";
            levelOrder(rootNode);
            cout<<endl;
            system("pause");
        }
        else if(choiced == 5)
        {
            system("cls");
            int count = countNode(rootNode);
            cout<<"二叉樹中結點總數爲"<<count<<endl;
            system("pause");
        }
        else if(choiced == 6)
        {
            system("cls");
            int dep = depth(rootNode);
            cout<<"此二叉樹的深度爲"<<dep<<endl;
            system("pause");
        }
        else if(choiced == 7)
        {
            system("cls");
            cout<<"二叉樹已被消毀!\n";
            destroy(rootNode);
            cout<<endl;
            system("pause");
        }
        else
        {
            system("cls");
            cout<<"\n\n\n\n\n\t錯誤選擇!\n";
        }
       
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章