控制台打印二叉树

#include<iostream> 
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string> 
#include<iomanip>
#include<cmath>
#include<queue>
#define N 100
#define MAXN 0x3f3f3f3f
using namespace std;

struct TreeNode{
    TreeNode* child[2];
    int val;
    int d;//距离屏幕左端的宽度 
    TreeNode(){
        child[0] = child[1] = NULL;
    }
};
int width;
int step = 4;
void buildT(TreeNode* &t){
       int val;
       cin>>val;
       if(val == -1){
           t = NULL;
           return;
       }
       t = new TreeNode();
       t->val = val;
    buildT(t->child[0]);
    buildT(t->child[1]);
}

void widthT(TreeNode* t){
    if(!t) return;
    widthT(t->child[0]);
    t->d = width;
    width+=step; 
    widthT(t->child[1]);
}
void outT(TreeNode* t){
     width=0;
     widthT(t);
     queue<TreeNode*> q;
     vector<TreeNode*> v;
     q.push(t);
     int n=1;//当前层的节点个数 
     int nn=0;//统计下一次的节点的个数
     int pred;//前一个节点距离左屏幕的距离 
     while(!q.empty()){
         TreeNode* tt = q.front();
         q.pop();
         v.push_back(tt);
         //放入孩子节点 
         if(tt->child[0]) q.push(tt->child[0]), ++nn;
         if(tt->child[1]) q.push(tt->child[1]), ++nn;
         if(v.size()==n){//上一层访问完毕 
             n = nn;
             nn = 0;
             /***************************************/
                 //打印节点的数值 
             int pred = -1;//前面的节点距离屏幕左端的距离 
             for(int i=0; i<v.size(); ++i) {
                  TreeNode* x= v[i];
                  if(pred==-1){
                       cout<<setw(x->d)<<"";
                       cout<<x->val;
                       pred = x->d;
                  } else {
                      cout<<setw(x->d-pred)<<x->val;
                      pred = x->d;
                  }
             }
             cout<<endl;
             /***************************************/
             
             /***************************************/
             //打印"---------------" 
             pred = -1;
             for(int i=0; i<v.size(); ++i){
                 TreeNode* x= v[i];
                 int ld=-1, rd=-1;//左右子树距离屏幕左端的距离 
                if(x->child[0]) ld = x->child[0]->d;
                if(x->child[1]) rd = x->child[1]->d;
                if(ld != -1) {
                     if(pred==-1){//打印左孩子的分枝图案 
                           cout<<setw(ld)<<"";
                      } else {
                          cout<<setw(ld-pred-1)<<"";
                      }
                      for(int j=0; j<x->d-ld; ++j)
                          cout<<"-";
                      cout<<"-";
                } else {
                    if(pred==-1){
                         cout<<setw(x->d)<<"";
                     } else {
                         cout<<setw(x->d-pred-1)<<"";
                     }
                     cout<<"-";
                }
                pred = x->d;
                if(rd != -1){//打印右孩子的分枝图案 
                    for(int j=0; j<rd-x->d; ++j)
                          cout<<"-";
                     pred = rd;
                }
             }
             cout<<endl;
             /***************************************/
             
             /***************************************/
             //打印 "|" 
             pred = -1;
             for(int i=0; i<v.size(); ++i){//打印竖线,过程和上面一部分差不多,稍作修改就好 
                 TreeNode* x= v[i];
                 int ld=-1, rd=-1;//左右子树距离屏幕左端的距离 
                if(x->child[0]) ld = x->child[0]->d;
                if(x->child[1]) rd = x->child[1]->d;
                if(ld != -1) {
                     if(pred==-1){
                           cout<<setw(ld)<<"";
                      } else {
                          cout<<setw(ld-pred-1)<<"";
                      }
                      cout<<"|";
                      for(int j=0; j<x->d-ld; ++j)
                          cout<<" ";
                } else {
                    if(pred==-1){
                         cout<<setw(x->d)<<"";
                     } else {
                         cout<<setw(x->d-pred-1)<<"";
                     }
                     cout<<" ";
                }
                pred = x->d;
                if(rd != -1){
                    for(int j=0; j<rd-x->d-1; ++j)
                          cout<<" ";
                     cout<<"|";
                     pred = rd;
                }
             }
             cout<<endl;
             /***************************************/
             v.clear();//清空上一层数据 
         }
     }
}

int main(){
    TreeNode *T = NULL;
    buildT(T);
    outT(T);
    return 0;
}

/*
1 2 7 -1 -1 4 8 -1 -1 -1 3 6 -1 -1 5 -1 -1

1 2 4 -1 -1 5 -1 -1 3 6 8 10 14 16 -1 -1 17 -1 -1 15 -1 -1 11 -1 -1 9 12 -1 -1 13 -1 -1 7 -1 -1
*/

 

posted @ 2015-07-27 16:56  hjzqyx  阅读(698)  评论(0编辑  收藏  举报