/*
*第十四周(專案一)--二叉排序樹相關演算法
*all right reserved.
*writer:羅海員
*date:2023年12月17日
*問題描述:
#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樹根結點指標
}}//在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;
}
執行結果:
(第十四周專案1)驗證演算法(3)二叉排序樹相關演算法
問題 檔名稱 專案1 3 cpp 作 者 黃玉嬌 完成日期 2016年12月2日 版 本 號 v1.0 輸入描述 無 程式輸出 測試資料 include include typedef int keytype typedef char infotype 10 typedef struct node ...
第十四周 專案4 二叉排序樹
標頭檔案 ifndef graph h included define graph h included define maxv 100 最大頂點個數 define inf 32767 inf表示 typedef int infotype 以下定義鄰接矩陣型別 typedef struct vert...
第十四周專案1二叉排序樹
檔名稱 h1.cpp 完成日期 2015年12月4日 版本號 vc6.0 問題描述 二叉排序樹 輸入描述 無 程式輸出 二叉樹建立刪除結果 include include typedef int keytype typedef char infotype 10 typedef struct node...