#include
#pragma hdrstop #include "Unit1.h"
#include
#include
using namespace std;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
typedef struct student {
struct student *llink; // 左子鏈結
char name[20]; // 姓名
struct student *rlink; // 右子鏈結
} Node_type;
Node_type *root;
Node_type *ptr;
void access(char []);
Node_type *search(char []);
void inorder(Node_type *);
void show_f(void);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{ TStringList *str=new TStringList;
str->LoadFromFile(Edit1->Text);
str->CommaText=str->Text;
Memo1->Text=str->Text;
root=NULL;
char name1[20],*qtr;
for(int i=0;iCount;i++)
{
qtr= (str->Strings[i]).c_str();
strcpy(name1,qtr);
access(name1);
}
show_f();
}
void access(char name[]) //字串陣列傳進來
{
Node_type *node, *prev;
if(search(name) != NULL) // 資料已存在則顯示錯誤
{
Form1->Memo2->Text= name;
return;
}
ptr = new Node_type;
strcpy(ptr->name, name);
ptr->llink = ptr->rlink = NULL;
if(root == NULL) // 當根節點為NULL的狀況
root = ptr;
else // 當根節點不為NULL的狀況
{
node = root;
while(node != NULL) // 搜尋資料插入點
{
prev = node;
if(strcmp(ptr->name, node->name) < 0)
node = node->llink;
else
node = node->rlink;
}
if(strcmp(ptr->name, prev->name) < 0)
prev->llink = ptr;
else
prev->rlink = ptr;
}
}
Node_type *search(char target[])
{
Node_type *node;
node = root;
while(node != NULL)
{
if(strcmp(target, node->name) == 0)
return node;
else
if(strcmp(target, node->name) < 0) // target小於目前節點,往左搜尋
node = node->llink;
else // target大於目前節點,往右搜尋
node = node->rlink;
}
return node;
}
void show_f(void)
{
if(root == NULL) // 判斷根節點是否為NULL
{
Form1->Memo3->Lines->Add("No student record!") ;
return;
}
Form1->Memo3->Lines->Add( "=====SHOW DATA=====") ;
inorder(root); // 以中序法輸出資料
}
void inorder(Node_type *node)
{
ostringstream out ;
if(node != NULL)
{
inorder(node->llink);
out<name ;
Form1->Memo3->Lines->Add(out.str().c_str());
inorder(node->rlink);
}
}
這ㄍ是載入一粉多名字的文件檔
如:
mery null kack
junk pill aorden
上面資料建構二元搜尋樹 以中序法輸出
我的程式碼可以執行但執行後
Memo3顯式中序法ㄉ結果
有粉奇怪ㄉ自出現
如下圖
請問大大哪出問題了
請大大刪這篇文章
我知哪出問題了
不好意思 發表人 - joen0000 於 2004/12/26 23:56:31 發表人 - joen0000 於 2004/12/27 00:06:36