程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> c-windows編程 樹的畫圖問題

c-windows編程 樹的畫圖問題

編輯:編程解疑
windows編程 樹的畫圖問題

我的B-樹是使用鏈表作為存儲結構的,在繪制B-樹的圖像的時候,如何確定結點在畫布上的位置?我想的是利用樹的遍歷操作,每遍歷一個結點就畫出這個結點的圖像,但是這個結點的位置怎麼確定?圖片說明
今天想了好久,希望大神幫忙看一下,謝謝!

最佳回答:


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");

}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved