牛哥123 2016-02-04 09:55 采纳率: 100%
浏览 1661
已采纳

windows编程 树的画图问题

我的B-树是使用链表作为存储结构的,在绘制B-树的图像的时候,如何确定结点在画布上的位置?我想的是利用树的遍历操作,每遍历一个结点就画出这个结点的图像,但是这个结点的位置怎么确定?图片说明
今天想了好久,希望大神帮忙看一下,谢谢!

  • 写回答

4条回答

  • NoicFank 2016-02-06 06:58
    关注

    http://download.csdn.net/detail/qq_28410301/9275927

    看这个 全写里面了。
    我可以给你大概讲讲,首先你向右边遍历,在遍历的函数中添加一个额外的int类型的参数a,当你在遍历的过程中,一直让这个数值a=a+3,加多少你自己订就好,不多说了,直接看代码,不会的看上面的链接,里面有详细的说明文档和想法。

    #include
    #include "binaryTreeNode.h"
    #include
    #include
    #include
    using namespace std;

    bool isOperator (char a)//判断是否为操作符
    {
    if(a=='+'||a=='-'||a=='*'||a=='/'||a=='('||a==')')
    return true;
    return false;
    }

    string inToPost (string s)//中序转后序
    {
    char temp;
    string post="";
    stack h;//运算符进栈

    for(int i=0;i<s.size();i++)
    {
        temp=s[i];
        //cout<<"S[i] :"<<s[i]<<endl;
    
        if(isOperator(temp))   //是操作符 分两种情况 h为空和不空
        {
            //cout<<"ff"<<endl;
            if(h.empty()) //h为空
            {
                //cout<<"kk"<<endl;
                h.push(temp);
                continue;
            }
            else     //h不为空
            {
                if(temp=='(')
                {
                    h.push(temp);
                    continue;
                }
                if(temp==')')
                {
                    while((!h.empty()) && h.top()!='(' )
                    {
                        post+=h.top();
                        h.pop();
                    }
                    h.pop();
                    continue;               
                }
                if(temp=='+'||temp=='-')
                {
                    //cout<<"Operator :"<<temp<<endl;
                    //cout<<"tt"<<endl;
                    while((!h.empty()) && h.top()!='(' )
                    {
                        //cout<<h.top()<<endl;
                        post+=h.top();
                        //cout<<post<<endl;
                        h.pop();
                        //bool r=h.empty();
                        //cout<<r<<endl;
                        //cout<<"pp"<<endl;
                    }
                    h.push(temp);
                    continue;
                }
                if(temp=='*'||temp=='/')
                {
                    while((h.top()=='*' || h.top()=='/') && (!h.empty()) )
                    {
                        post+=h.top();
                        h.pop();
                    }
                    h.push(temp);
                    continue;
                }
            }
        }
        else//不是操作符 是数字
            post+=s[i];
    
    }
    
    for(;!h.empty();)
    {
        post+=h.top();
        h.pop();
    }
    
    return post;
    

    }

    void postToBinary (binaryTreeNode* &tree,string s)//后序转二叉树
    {
    char temp;
    int i=0;
    temp=s[i];

    stack<binaryTreeNode*> k;//存几个有用的节点 (就是还没有相关联的节点)
    
    while(temp!='\0')//当s没有扫到右边尾部时
    {
        tree = new binaryTreeNode(temp);
    
        if(!isOperator(temp))//直接是数字 不是字符的情况
        {
            k.push(tree);
            temp=s[i++];
        }
        else//为字符的情况
        {
            if(!k.empty())
            {
                tree->rightChild=k.top();
                k.pop();
            }
            if(!k.empty())
            {
                tree->leftChild=k.top();
                k.pop();
            }
            k.push(tree);
            temp=s[i++];
        }
    }
    

    }

    //ofstream ofs("target.txt", ios::out);

    ofstream out ("a.txt",ios::out);

    void outPutTree (binaryTreeNode* tree,int theDistance)
    {
    if(tree)//树不为空
    {
    outPutTree (tree->rightChild, theDistance+3);
    for(int i=0; i out cout coutelement < outelement < for(int j=0;j out cout outPutTree (tree->leftChild , theDistance+3);
    }
    }

    void freeTree(binaryTreeNode* & tree)//释放树
    {
    if(tree->leftChild!=NULL)
    freeTree(tree->leftChild);
    if(tree->rightChild!=NULL)
    freeTree(tree->rightChild);
    delete(tree);
    }

    void main ()
    {
    string s;
    cin>>s;
    binaryTreeNode* tree;

    s=inToPost(s);
    
    postToBinary(tree,s);
    
    outPutTree(tree,0);
    
    freeTree(tree);
    system("pause");
    

    }

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

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)