算法学习20-如何直观的打印一颗二叉树
算法思路
- 树的打印方法是用中序的方法进行输出
- 在输出其实是图形输出的问题了,在这里就不做过多阐述了。
- 用到了sstream库函数的内容
/*库定义了三种类:
* istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。
* 1.stringstream::str(); returns a string object with a copy of the current contents of the stream.
* 2.stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.
* 3.stringstream清空,stringstream s; s.str("");
* 4.实现任意类型的转换
*
* stringstream stream;
stream<<t;//向流中传值
out_type result;//这里存储转换结果
stream>>result;//向result中写入值
*
*
int main(){
string s = "1 23 # 4";
stringstream ss;
ss<<s;
while(ss>>s){
cout<<s<<endl;
int val = convert<int>(s);
cout<<val<<endl;
}
return 0;
}*/
程序代码:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Node
{
public:
Node(int data)
{
value = data;
left = NULL;
right = NULL;
}
public:
int value;
Node *left;
Node *right;
};
/*Root为根节点,Height为当前处于二叉树的那一层(从0层开始);
* String为根、左、右节点的区分符号;
* len为格式化打印参数,固定为17,
* 即每个节点都将占有17个字符的位置*/