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

  • 相关阅读:
    4、pytest -- fixtures:明确的、模块化的和可扩展的
    CentOS -- 新建用户并使能密钥登录
    3、pytest -- 编写断言
    2、pytest -- 使用和调用
    1、pytest -- 安装和入门
    《Fluent Python》 -- 一个关于memoryview例子的理解过程
    SecureCRT 连接 Centos7.0 (NAT模式),且能连接公网。
    SecureCRT 连接 Centos7.0 (桥接模式),且能连接公网。
    Centos7.0 三种网络适配器
    Centos 7.0 界面
  • 原文地址:https://www.cnblogs.com/skyofbitbit/p/3569296.html
Copyright © 2011-2022 走看看