zoukankan      html  css  js  c++  java
  • 重新学习二叉树作的几道习题

     二叉树习题

    // Tree2.cpp : 定义控制台应用程序的入口点。
    //


    #include  <iostream>
    #include "stdafx.h"

    using  namespace  std;


    typedef   struct  _TreeNode
    {
     char    data;
     _TreeNode*  lChild;
     _TreeNode*  rChild;
    }TreeNode,*PTreeNode;


      void  PreWalk(TreeNode*  p)
      {
        if(p == NULL) return;
        cout<< p->data <<endl;
        if(p->lChild != NULL)
           cout<< "left of "<<p->data<<endl;
        PreWalk( p->lChild);
        if( p->rChild != NULL)
           cout<< "righ of " <<p->data<<endl;
        PreWalk(p->rChild);
      }

      void  InWalk(TreeNode*  p)
      {
       if(p == NULL) return;

       if(p->lChild != NULL)
         cout<< "left of "<<p->data<<endl;
       InWalk( p->lChild);

       cout<<p->data<<endl;

       if( p->rChild != NULL)
         cout<< "righ of " <<p->data<<endl;
        InWalk(p->rChild);
      }

      void  PostWalk(TreeNode*  p)
      {
        if(p == NULL) return;

        if(p->lChild != NULL)
         cout<< "left of "<<p->data<<endl;  
        PostWalk( p->lChild);

        if( p->rChild != NULL)
         cout<< "righ of " <<p->data<<endl;
        PostWalk(p->rChild);

        cout<<p->data<<endl;
      }

    /*前序输入创建二叉树*/ 
      bool   CreateTree(TreeNode**  pTree)
      {
       char  c;
       cout<<"Value: "<<endl;
       cin>>c;
       if( c == '#' )
       {
        *pTree = NULL;
        return  false;//空子树
       }
       else if( c == '$' )
       {
        *pTree = NULL;
        return true;//结束 
       }
       else
       {
        TreeNode*  pNode = new  TreeNode;
        pNode->data = c;
        *pTree = pNode;
        pNode->lChild = pNode->rChild= NULL;
        cout<<"Left of "<<c<<endl;
        if( CreateTree(  &(*pTree)->lChild ))
         return true;
        cout<<"Right of "<<c<<endl;
        if(CreateTree(  &(*pTree)->rChild ))
         return true;

        return false;
       }

      }


     


    int _tmain(int argc, _TCHAR* argv[])
    {
       TreeNode*   _root = NULL;
       CreateTree(&_root);

       PreWalk( _root);

     return 0;
    }

  • 相关阅读:
    Hadoop and net core a match made in docker
    JavaScript封装方法,兼容参数类型为Number和String
    HTML的input类型为hidden导致无法reset改字段的value问题
    MyBatis自动生成Java/C#的Bean(Entity)的等价MYSQL实现函数
    JMX configuration for Tomcat
    Resolved validation conflict with readonly
    FreeMarker example all in one
    Notepad++ 大小写转换
    常用编辑器列模式快捷键
    Redis应用一例(存证数量用计数器实现)
  • 原文地址:https://www.cnblogs.com/skyofbitbit/p/3569296.html
Copyright © 2011-2022 走看看