开发者社区> 问答> 正文

我的B-树是使用链表作为存储结构的,在绘制B-树的图像的时候,如何确定结点在画布上的位置?我想的是利用树的遍历操作,每遍历一个结点就画出这个结点的图像,但是这个结点的位置怎么确定?

screenshot
我的B-树是使用链表作为存储结构的,在绘制B-树的图像的时候,如何确定结点在画布上的位置?我想的是利用树的遍历操作,每遍历一个结点就画出这个结点的图像,但是这个结点的位置怎么确定?

展开
收起
a123456678 2016-03-24 10:19:52 2621 0
1 条回答
写回答
取消 提交回答
  • 我可以给你大概讲讲,首先你向右边遍历,在遍历的函数中添加一个额外的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");
    }
    2019-07-17 19:12:05
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载