刘放空 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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器