線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:811
推到 Plurk!
推到 Facebook!

二元搜尋樹的問題

缺席
joen0000
一般會員


發表:47
回覆:47
積分:18
註冊:2004-11-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-26 23:45:03 IP:61.230.xxx.xxx 未訂閱
  #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
系統時間:2024-09-30 17:11:45
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!