/*問題及**
*all right reserved.
*檔名稱:二叉樹排序樹中查詢的路徑.cpp
*問題描述:
設計乙個演算法,輸出在二叉排序中查詢時查詢某個關鍵字經過的路徑。
*輸入描述:無
*程式輸出:根據要求輸出
*/#include #include #define maxsize 100
typedef int keytype; //定義關鍵字型別
typedef char infotype;
typedef struct node //記錄型別
bstnode;
int path[maxsize]; //全域性變數,用於存放路徑
void dispbst(bstnode *b); //函式說明
int insertbst(bstnode *&p,keytype k) //在以*p為根節點的bst中插入乙個關鍵字為k的節點
else if (k==p->key)
return 0;
else if (kkey)
return insertbst(p->lchild,k); //插入到*p的左子樹中
else
return insertbst(p->rchild,k); //插入到*p的右子樹中
}bstnode *creatbst(keytype a,int n)
//由陣列a中的關鍵字建立一棵二叉排序樹
else
}//查詢並顯示經過的路徑
void searchresult(bstnode *bt, int k1)
void dispbst(bstnode *bt)
//以括號表示法輸出二叉排序樹bt
}}int main()
,n=10;
printf("建立的bst樹:");
bt=creatbst(a,n);
dispbst(bt);
printf("\n");
printf(" 查詢%d關鍵字:",k1);
searchresult(bt,k1);
printf(" 查詢%d關鍵字:",k2);
searchresult(bt,k2);
return 0;
}執行結果
(第十四周專案2)二叉排序樹中查詢的路徑
問題 檔名稱 專案2.cpp 作 者 黃玉嬌 完成日期 2016年12月2日 版 本 號 v1.0 問題描述 設計乙個演算法,輸出在二叉排序中查詢時查詢某個關鍵字經過的路徑。輸入描述 無 程式輸出 測試資料 include include define maxsize 100 typedef int...
第十四周 專案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...