打印树


  利用了树的中序遍历,不过是从右边到左边的中序遍历。


#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<totalSpaces;i++)
            cout
<<         cout<<ptr->getData()<<endl;
        outputTree(ptr
->leftPtr,totalSpaces+5
    }

}



int main()
{

     Tree<int> intTree;
     
int intVal;

    cout
<<Enter 15 integer valusen    for(int i=0<15++)
    
{
        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

此条目发表在未分类分类目录。将固定链接加入收藏夹。
0 0 投票数
文章评分
订阅评论
提醒
guest
0 评论
最旧
最新 最多投票