二叉排序樹上的查詢

2021-04-02 23:53:45 字數 1015 閱讀 1311

#include

#include

#define null 0

#define n 100

typedef int keytype;

typedef struct nodebstnode;//二叉排序樹結點結構

typedef bstnode *bstree;//二叉排序樹型別定義

bstnode *searching(bstree tree,keytype key)

//end of searching 

main()//主函式

//end of main 

insertbst(bstree *tptr,keytype key)

//end of if

p=(bstnode *)malloc(sizeof(bstnode));//找到插入位置後,申請結點空間*p

p->key=key;p->lchild=p->rchild=null;//將key放入*p的資料域中,並將其左右孩子置空

if(*tptr==null)//若二叉排序樹為空

*tptr=p;//直接將結點*p作為根結點

else//若待插入的二叉排序樹非空

if (keykey)//若key小於f->key

f->lchild=p;//將*p結點作為*f結點的左孩子插入

else f->rchild=p;//將*p結點作為*f結點的右孩子插入

}//end of insertbst

createbst(bstree *t)

printf("/n");

}if (j==2)

printf("/n");

}if (j==3)

printf("/n");

}if (j==4)//end of while

}//end of if

}//end of createbst 

inorder(bstree t)

//end of if 

}//end of inorder

查詢 二叉排序樹

順序查詢 順序查詢又叫線性查詢,是最基本的查詢技術,它的查詢過程是 從表中第一個記錄開始,逐個進行記錄的關鍵字和給定值比較,若某個記錄的關鍵字和給定值相等,則查詢成功,找到所查的記錄 如果知道最後一個記錄,其關鍵字和給定值比較都不等時,則表中沒有所查的記錄,查詢不成功。順序查詢演算法實現 如下 順序...

查詢 二叉排序樹

動態查詢表 表結構本身是在查詢過程中動態生成的,即對於給定值key,若表中存在其關鍵值等於key的記錄,則查詢成功返回,否則插入關鍵字等於key的記錄。二叉排序樹或者是一顆空樹,或者是具有下列性質的二叉樹 1 若他的左子樹不為空,則左子樹上所有結點的值均小於它的根結點的值。2 若它的右子樹不空,則右...

二叉排序樹的查詢

1.編寫函式,建立有序表,採用折半查詢實現某一已知的關鍵字的查詢 採用順序表儲存結構 2.編寫函式,隨機產生一組關鍵字,利用二叉排序樹的插入演算法建立二叉排序樹 3.編寫函式,在以上二叉排序樹中刪除某一指定關鍵字元素 4.編寫一個主函式,在主函式中設計一個簡單的選單,分別除錯上述演算法 二叉排序樹的...

查詢和二叉排序樹

一般的查詢演算法 include stdafx.h include include using namespace std template size t search vector vec,const type x int tmain int argc,tchar argv 2.折半查詢演算法 t...

二叉排序樹查詢操作

當函式返回值為函式結果狀態 時,函式定義為status型別。遞迴查詢二叉排序樹t中是否存在key 指標 f 指向 t 的雙親,其初始呼叫值為null 若查詢成功,則指標p指向該資料元素結點,並返回true includetypedef struct bitnode bitnode,bitree bo...