是否二叉搜尋樹

2021-10-10 07:52:37 字數 1360 閱讀 5218

本題要求實現函式,判斷給定二叉樹是否二叉搜尋樹。

函式介面定義:

bool isbst ( bintree t )

;

其中bintree結構定義如下:

typedef

struct tnode *position;

typedef position bintree;

struct tnode

;

函式isbst須判斷給定的t是否二叉搜尋樹,即滿足如下定義的二叉樹:

定義:乙個二叉搜尋樹是一棵二叉樹,它可以為空。如果不為空,它將滿足以下性質:

1.非空左子樹的所有鍵值小於其根結點的鍵值。

2.非空右子樹的所有鍵值大於其根結點的鍵值。

3.左、右子樹都是二叉搜尋樹。

如果t是二叉搜尋樹,則函式返回true,否則返回false。

裁判測試程式樣例:

#include

#include

typedef

enum

bool;

typedef

int elementtype;

typedef

struct tnode *position;

typedef position bintree;

struct tnode

;bintree buildtree()

;/* 由裁判實現,細節不表 */

bool isbst ( bintree t )

;int

main()

/* 你的**將被嵌在這裡 */

輸入樣例1:如下圖

輸出樣例1:

yes

輸入樣例2:如下圖

輸出樣例2:

no

bool isbst ( bintree t )

else

return

isbst

(t->left);}

if(t-

>right)

else

return

isbst

(t->right);}

return

true

;}

是否二叉搜尋樹

是否二叉搜尋樹 25 分 本題要求實現函式,判斷給定二叉樹是否二叉搜尋樹。bool isbst bintree t 其中bintree結構定義如下 typedef struct tnode position typedef position bintree struct tnode 函式isbst須...

是否二叉搜尋樹

習題4.3 是否二叉搜尋樹 25 分 本題要求實現函式,判斷給定二叉樹是否二叉搜尋樹。bool isbst bintree t 其中bintree結構定義如下 typedef struct tnode position typedef position bintree struct tnode 函式...

是否二叉搜尋樹PTA

本題要求實現函式,判斷給定二叉樹是否二叉搜尋樹。bool isbst bintree t 其中bintree結構定義如下 typedef struct tnode position typedef position bintree struct tnode 函式isbst須判斷給定的t是否二叉搜尋樹...