算法学习20-如何直观的打印一颗二叉树

算法学习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个字符的位置*/
void printInorder(Node *Root, int Height, string String, int Len)
{
    if(NULL == Root)
        return ;
    printInorder(Root->right, Height + 1, "v", Len);//先打印右子树
    //打印跟节点
    stringstream ss;
    /*<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;
}*/
    ss << Root->value;
    string val = String + ss.str() + String;    //构造H*H或v*v或^*^
    int LenString = val.length();//  总长度
    int Lenleft = (Len - LenString) / 2;//  1/2的长度
    int Lenright = Len - LenString - Lenleft;//右距离
    val = string(Lenleft, ' ') + val + string(Lenright, ' ');
    cout << string(Height * Len, ' ') << val << endl;
    printInorder(Root->left, Height + 1, "^", Len);
    return ;
}

void PrintTree(Node *Root)
{
    cout << "Binary Tree:" << endl;
    printInorder(Root, 0, "H", 17);
    cout << endl;
}

void CreatTree(Node **head, int *Array, int Len, int Index)
{
    if(Index > Len - 1 || -1 == Array[Index] )
        return;
    (*head) = new Node(Array[Index]);
    CreatTree(&((*head)->left), Array, Len, 2 * Index + 1);
    CreatTree(&((*head)->right), Array, Len, 2 * Index + 2);
}

int main()
{
    int Array[] = {1,2,3,4,-1,5,6,-1,7};
    Node *Root = NULL;
    CreatTree(&Root, Array, 9, 0);
    PrintTree(Root);
    return 0;
}

运行结果如下:

Binary Tree:
                                         v6v
                        v3v
                                         ^5^
       H1H
                        ^2^
                                                          v7v
                                         ^4^


文章到最后,祝大家元宵节快乐。

发布了23 篇原创文章 · 获赞 1 · 访问量 990

猜你喜欢

转载自blog.csdn.net/qq_45205390/article/details/104225943