zoukankan      html  css  js  c++  java
  • 打印树


      利用了树的中序遍历,不过是从右边到左边的中序遍历。
    #include <iostream.h>
    #include 
    "tree.h"

    template
    <class NODETYPE>
    void outputTree(TreeNode<NODETYPE> *ptr,int totalSpaces)
    {
        
    if(ptr!=0)
        
    {
            outputTree(ptr
    ->rightPtr,totalSpaces+5);
            
    for(int i=0;i<totalSpaces;i++)
                cout
    <<' ';
            cout
    <<ptr->getData()<<endl;
            outputTree(ptr
    ->leftPtr,totalSpaces+5);

        }

    }



    int main()
    {

         Tree<int> intTree;
         
    int intVal;

        cout
    <<"Enter 15 integer valuse\n";
        
    for(int i=0;i<15;i++)
        
    {
            cin
    >>intVal;
            intTree.insertNode(intVal);
        }


        outputTree(intTree.rootPtr,
    0);

        
    return 0;
    }
                 
    运行结果:
                  99
              97
                   92
         83
                   72
              71
                   69
    49
                   44
              40
                   32
         28
                   19
              18
                   11
  • 相关阅读:
    利用opengl画一个水波的曲面
    Eclipse 使用OpenGL
    Javascript学习过程(二)
    Workflow Learing
    YAWL设计实例
    YAWL使用方法
    ImageJ二次开发学习纪录之初步体会
    [LeetCode 660] Remove 9
    [LeetCode 1542] Find Longest Awesome Substring
    [LeetCode 879] Profitable Schemes
  • 原文地址:https://www.cnblogs.com/junnyfeng/p/192929.html
Copyright © 2011-2022 走看看