点点小屋

博客园 首页 新随笔 联系 订阅 管理

//二叉树的递归创建&遍历

#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";
        }
       
    }
}


 

 

posted on 2009-12-14 22:44  点点小屋  阅读(1774)  评论(0)    收藏  举报