/*
*all rights reserved.
*檔名稱:dsitem13-1.cpp
*作 者:於子淇
*完成日期:2023年11月29日
*版 本 號:code::blocks 12.11
*問題描述:
(1)由整數序列構造二叉排序樹;
(2)輸出用括號法表示的二叉排序樹;
(3)用遞迴演算法和非遞迴演算法查詢關鍵字55;
(4)分別刪除43和55,輸出刪除後用括號法表示的二叉排序樹。
*輸入描述:無
*程式輸出:見程式執行結果演示
*/
main.cpp
#include
#include
typedef
int keytype;
typedef
char infotype[10];
typedef
struct node //記錄型別
bstnode;
//在p所指向的二叉排序樹中,插入值為k的節點
int insertbst(bstnode *&p,keytype k)
else
if (k==p->key) //樹中存在相同關鍵字的結點,返回0
return
0; else
if (kkey)
return insertbst(p->lchild,k); //插入到*p的左子樹中
else
return insertbst(p->rchild,k); //插入到*p的右子樹中
}//由有n個元素的陣列a,建立乙個二叉排序樹
bstnode *createbst(keytype a,int n) //返回bst樹根結點指標
return bt; //返回建立的二叉排序樹的根指標
}//輸出一棵排序二叉樹
void dispbst(bstnode *bt)
}}//在bt指向的節點為根的排序二叉樹中,查詢值為k的節點。找不到返回null
bstnode *searchbst(bstnode *bt,keytype k)
//二叉排序樹中查詢的非遞迴演算法
bstnode *searchbst1(bstnode *bt,keytype k)
return
null;
}void delete1(bstnode *p,bstnode *&r) //當被刪*p結點有左右子樹時的刪除過程
}void delete(bstnode *&p) //從二叉排序樹中刪除*p結點
else
if (p->lchild==null) //*p結點沒有左子樹的情況
else delete1(p,p->lchild); //*p結點既沒有左子樹又沒有右子樹的情況
}int deletebst(bstnode *&bt, keytype k) //在bt中刪除關鍵字為k的結點
}}int main()
; bt=createbst(a,n);
printf("bst:");
dispbst(bt);
printf("\n");
printf("刪除%d結點\n",x);
if (searchbst(bt,x)!=null)
return
0;}
程式執行結果演示:
第13周專案3二叉排序樹
主函式 include include typedef int keytype typedef char infotype 10 typedef struct node 記錄型別 bstnode 在p所指向的二叉排序樹中,插入值為k的節點 int insertbst bstnode p,keytyp...
第14周專案1(3)驗證二叉排序樹相關演算法
問題 檔名稱 專案1 3.cbp 作 者 張芸嘉 完成日期 2015年12月8日 版 本 號 v1.0 輸入描述 無 程式輸出 測試資料 include include typedef int keytype typedef char infotype 10 typedef struct node ...
第14周專案1 (3)驗證二叉排序樹相關演算法
問題 cpp view plain copy 檔名稱 專案1 3.cbp 作 者 程德泉 完成日期 2016年12月2日 版 本 號 v1.0 問題描述 驗證二叉排序樹相關演算法 輸入描述 無 程式輸出 測試資料 cpp view plain copy include include typedef...