刘放空 2015-12-27 14:57 采纳率: 50%
浏览 1572
已采纳

二叉树遍历全都是出ASCII码

 #include <iostream>

using namespace std;
const int Maxsize = 100;

struct Node
{
    int data;
    Node*lchild,*rchild;
};

class Tree
{
public:
     Tree(){cout << "输入节点信息,如果是空请输入“#”" << endl;         
     root = Creat(root); }
     void PreOrder(){cout <<"前序遍历得到:";PreOrder(root); cout<<endl;} 
     int Depth(){cout <<"二叉树的深度是:"<<Depth(root)<<endl;}             
     void Count(){Count(root); cout << "二叉树的结点数是:" << count << endl;}   //计算二叉树节点数
     void Exchange(){Exchange(root);cout<<"把左右子树进行交换"<<endl;}                                  //把二叉树左右子树进行交换
     void LeverOrder();                                                //再次层序遍历
private:
     int count = 0;
     Node*root;
     Node*Creat(Node*root);
     void PreOrder(Node *root);
     int Depth(Node *root);
     void Count(Node *root);
     void Exchange(Node*root);
};
                  //实现构建一颗二叉树
Node*Tree::Creat(Node*root)
{
    char ch;

    cin >> ch;
    if (ch == '#')root = NULL;
    else{
        root = new Node;
        root->data = ch;
        root->lchild = Creat(root->lchild);
        root->rchild = Creat(root->rchild);
    }
    return root;
}

             //实现前序遍历
void Tree::PreOrder(Node *root)
{
    if(root==NULL)return;
    else{
        cout<<root->data;
        PreOrder(root->lchild);
        PreOrder(root->rchild);
    }
}

int Tree::Depth(Node *root)//求二叉树深度
        {
     int hl=0,hr=0;
            if(root==NULL)return 0;
            else{
                hl=Depth(root->lchild);
                hr=Depth(root->rchild);

            return max(hl,hr)+1;
            }
        }

void Tree::Count(Node *root)//实现求二叉树叶子节点数
    {

        if(root!=NULL)
        {
        Count(root->lchild);
        count++;
        Count(root->rchild);
        }

    }

void Tree::Exchange(Node*root)//交换左右子树
    {
        Node *t;
        if(root!=NULL){
            Exchange(root->lchild);
            Exchange(root->rchild);
            t=root->lchild;
            root->lchild=root->rchild;
            root->rchild=t;
        }
    }
        //层序遍历
void Tree::LeverOrder()
{cout<<"层序遍历:";
    int rear,front;
    Node *Q[50];
    Node *q;
    front=rear=-1;
    if(root==NULL)return;
    Q[++rear]=root;
    while(front!=rear)
    {
        q=Q[++front];
        cout<<q->data;
        if(q->lchild!=NULL)Q[++rear]=q->lchild;
        if(q->rchild!=NULL)Q[++rear]=q->rchild;
    }
}
int main()
{
    Tree B;
    B.PreOrder();
    B.Depth();
    B.Count();
    B.Exchange();
    B.LeverOrder();
    return 0;

}

  • 写回答

6条回答 默认 最新

  • 刘放空 2015-12-27 15:01
    关注

    已解决= =, 一开始的DATA应该定义为CHAR类型的= =

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。