創作內容

0 GP

二元樹

作者:御│2017-03-13 00:11:35│巴幣:0│人氣:65
遞迴 非遞迴 搜尋 新增 大量新增
遞迴 輸出

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        srand(time(NULL));
}
//---------------------------------------------------------------------------

String out;//final output with recursive

struct BTreeNode{
        struct BTreeNode *leftchild;
        int data;
        struct BTreeNode *rightchild;
};//define tree node

struct BTreeNode *root;//tree root with recursive
struct BTreeNode *rootNR;//tree root with non-recursive

struct BTreeNode* insert(int x){
        struct BTreeNode *node=new struct BTreeNode;
        node->data=x;
        node->leftchild=NULL;
        node->rightchild=NULL;
        return node;
}//build new child and place number;

struct BTreeNode* placeNode(struct BTreeNode *node,int x){
        if(node==NULL){
                return insert(x);
        }
        if(x<node->data){
                node->leftchild=placeNode(node->leftchild,x);
        }else{
                node->rightchild=placeNode(node->rightchild,x);
        }
        return node;
}//find the place where new child should be;
//recursive

void placeNodeNR(int x){
        struct BTreeNode *u, *r;
        u=rootNR;
        r=NULL;
        while(u!=NULL){
                r=u;
                if(x<u->data){
                        u=u->leftchild;
                }else{
                        u=u->rightchild;
                }
        }
        u=insert(x);
        if(rootNR==NULL){
                rootNR=u;
        }else if(x<r->data){
                        r->leftchild=u;
                }else{
                        r->rightchild=u;
                }
}//find the place where new child should be;
//non-recursive

void showTree(struct BTreeNode *node){
        if(node!=NULL){
                showTree(node->leftchild);
                out+=IntToStr(node->data)+"_";
                showTree(node->rightchild);
        }
}//LVR print the child from left to right(small to large number)
//recursive

int searchTree(struct BTreeNode *node,int x){
        if(node==NULL){
                return 0;
        }
        if(x==node->data){
                return 1;
        }
        if(x<node->data){
                return searchTree(node->leftchild,x);
        }else{
                return searchTree(node->rightchild,x);
        }
}//searching data in tree with recursive

int searchTreeNR(struct BTreeNode *node,int x){
        while(node!=NULL){
                if(x==node->data){
                       return 1;
                }
                if(x<node->data){
                        node=node->leftchild;
                }else{
                        node=node->rightchild;
                }
        }
        return 0;
}//searching data in tree with non-recursive
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        Memo1->Clear();
        out="";
        root=placeNode(root,StrToInt(Edit1->Text));
        showTree(root);
        Memo1->Lines->Add(out);
}//join a new child into tree
//recursive
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
        Memo1->Clear();
        out="";
        for(int i=0; i<StrToInt(Edit2->Text); i++){
                root=placeNode(root,rand()%StrToInt(Edit3->Text)+1);
        }
        showTree(root);
        Memo1->Lines->Add(out);
}//join random child into two trees several times
//recursive
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
        Memo2->Clear();
        out="";
        placeNodeNR(StrToInt(Edit1->Text));
        showTree(rootNR);
        Memo2->Lines->Add(out);
}//join a new child into tree
//non-recursive
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
        Memo2->Clear();
        out="";
        for(int i=0; i<StrToInt(Edit2->Text); i++){
                placeNodeNR(rand()%StrToInt(Edit3->Text)+1);
        }
        showTree(rootNR);
        Memo2->Lines->Add(out);
}//join random child into two trees several times
//non-recursive
//---------------------------------------------------------------------------

void __fastcall TForm1::Button6Click(TObject *Sender)
{
        String ans;
        int target=StrToInt(Edit4->Text);
        if(searchTree(root,target)){
                ans="got "+IntToStr(target)+" in tree of recursive";
        }else{
                ans="can't find "+IntToStr(target)+" in tree of recursive";
        }
        Memo1->Lines->Add(ans);
        if(searchTree(rootNR,target)){
                ans="got "+IntToStr(target)+" in tree of non-recursive";
        }else{
                ans="can't find "+IntToStr(target)+" in tree of non-recursive";
        }
        Memo2->Lines->Add(ans);
}//search data in both tree with calling recursive function
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{
        String ans;
        int target=StrToInt(Edit4->Text);
        if(searchTreeNR(root,target)){
                ans="got "+IntToStr(target)+" in tree of recursive";
        }else{
                ans="can't find "+IntToStr(target)+" in tree of recursive";
        }
        Memo1->Lines->Add(ans);
        if(searchTreeNR(rootNR,target)){
                ans="got "+IntToStr(target)+" in tree of non-recursive";
        }else{
                ans="can't find "+IntToStr(target)+" in tree of non-recursive";
        }
        Memo2->Lines->Add(ans);
}//search data in both tree with calling non-recursive function
//---------------------------------------------------------------------------

引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3509781
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

喜歡★asd123comtw 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:CV翔鶴... 後一篇:近期戰艦世界成績紀錄(廢...

追蹤私訊切換新版閱覽

作品資料夾

xzp83502在線巴哈們
果果日記小屋更新中~歡迎進來參觀 謝謝^^看更多我要大聲說昨天15:15


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】