利用了树的中序遍历,不过是从右边到左边的中序遍历。
#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;
}
#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