資料結構課程設計5

2021-10-22 23:58:33 字數 3561 閱讀 4498

二.判斷一棵二叉樹是否為二叉排序樹

1.node*lchild,*rchild(兩個都要加星號)

2.(1)序建立二叉樹的public部分以建構函式和呼叫creat函式為主,private部分以宣告creat函式為主,詳細定義是對creat函式。

(2)ctrat函式有返回值,建構函式無返回值

(3)creat函式有引數,建構函式不寫引數

(4)建構函式要把樹的根交給root,creat函式要返回根。

3.以下**是錯誤的,因為宣告乙個函式時,這個函式裡面呼叫的函式的引數要是實參,實實在在的已經知道型別的乙個簡簡單單的引數。

void

creat()

4.定義函式時:返回值型別類名::函式名(形參){}

5.int n; cin >> n; if (n == -1)

這裡不應該return null,直接說明這個位置結點是乙個null值即可,把這個結點交給他的雙親的工作在else的bt->lchild=,和最後的return bt就可以。

(1)else裡的返回不空的結點,最後的return返回空節點和最後的根結點

6.二叉樹習慣public裡的函式呼叫private裡面的函式,把root作為呼叫private裡的函式時的實參

#include

using namespace std;

struct node

;class bitree

int isbst(

)private:

node* root;

node* creat(node *bt)

; int isbst(int i,node* bt)

; int flag = 1;};

node* bitree::creat(node* bt)

else

return bt;

}int bitree::isbst(int j,node* bt)

else}if

(bt->rchild != null&&

(flag==1))

else

} cout <<

"第"<< j <<

"次遞迴,flag="

<< flag <<

"bt->data等於"

}int main(

)else

}

8.通過上面的程式和列印結果看一下遞迴的每一層發生了什麼:

876

4-15

-13-

1-1-

1-19

-110-

1-1第0次遞迴flag=

1bt-

>data:

8第1次遞迴flag=

1bt-

>data:

7第2次遞迴flag=

1bt-

>data:

6第3次遞迴flag=

1bt-

>data:

4第4次遞迴flag=

1bt-

>data:

5第4次遞迴,flag=

0bt-

>data等於5

第3次遞迴,flag=

0bt-

>data等於4

第2次遞迴,flag=

0bt-

>data等於6

第1次遞迴,flag=

0bt-

>data等於7

第0次遞迴,flag=

0bt-

>data等於8

noc:\users\夷夷\desktop\order4t2\debug\order4t2.exe (程序 28680

)已退出,**為 0。

要在除錯停止時自動關閉控制台,請啟用「工具」-

>「選項」-

>「除錯」-

>「除錯停止時自動關閉控制台」。

按任意鍵關閉此視窗.

..

三.折半查詢——對比次數

1.在main函式裡輸入的一組數,要經過某乙個演算法來找到某乙個數,要想看遞迴呼叫了那個演算法幾次,可以在c++中在對應using namespace std;下面直接設定乙個函式之外的全域性變數num來計數。另外max的設定可以放在#include上面面。

const int max = 20; #includeusing namespace std; int num = 0;

2.區別於上乙個題,flag設定在class類定義的private中,是因為flag只在類成員函式裡面用,時時刻刻都在改變flag.這個題如果a a;a.num作為引數傳入isbst()函式,那麼當在main函式裡輸出a.num是還是初始值,因為只是改變了a.num的副本,原值不變。

3.整體**如下:

const int max = 20;

#include

using namespace std;

int num = 0;

void binsearch(int a[

], int n,int l,int h,int k)

int mid =

(l + h) / 2;

if(a[mid]

== k)

else

if(k > a[mid]

)else

if(k < a[mid])}

int main(

) cin >> k;

binsearch(a, n,1, n, k)

; cout <}

四.線性探測解決衝突----求雜湊位址

1.定義函式時,即使函式沒有返回的東西,也要寫上void,除了建構函式。

2.不要先放到乙個陣列,而是要一邊輸一邊放進表裡面去。

3.突然用下面的方法得到的是第乙個陣列元素值是-1,其他的全是0;

int h[size]

=;

4.所有**如下:

#include

using namespace std;

int hash(int divi, int size,int h[

],int num)

else

j=(j+1)%size;

count++;}}

}int find(int t,int h[

],int size,int divi)

else

m=(m+1)%size;

c++;}}

return -1;

}int main(

)// int h[size]=;

// for(int i=0;ifor(int i = 0; i < n; i++)

// for(int i=0;icout>t;

int i=find(t,h,size,divi)

; cout<}

資料結構課程設計

資料結構課程設計 include define n 30 include include includeusing namespace std struct node typedef node phone,mingzi node hashnumble node hashname int hash1 ...

資料結構課程設計

小明是乙個計算機專業top student 祝賀他畢業了。並準備到銀行參加工作。上班第一天,經理叫他編制乙個實現乙個活期儲蓄處理程式,算作考查。上班第一天,一定要給領導乙個好印象,小明二話沒說,就答應了。現要你是小明了,請完成如下題目功能。儲戶開戶 銷戶 存入 支出活動頻繁,系統設計要求 1 能比較...

資料結構課程設計

一 實訓目的 通過課程設計,學會運用資料結構知識,針對具體應用,自己設計合理資料結構,確定儲存結構,並能設計具體操作演算法,選擇使用具體語言進行實現。掌握c 較複雜程式的組織和設計過程,除錯技巧。學習解決實際問題的能力。題目1.小明是乙個計算機專業top student,祝賀他畢業了。並準備到銀行參...