資料結構----二叉排序樹
阿新 • • 發佈:2018-12-10
結點結構
/**********結點結構*********/
typedef struct BTNode
{
int key;
struct BTNode *lchild;
struct BTNode *rchild;
}BiNode,*BiTree;
查詢關鍵字
/********查詢關鍵字********/ BiNode* BSTSearch(BiTree bt, int key) { if (bt == NULL) return NULL; else { if (key == bt->key) printf("查詢成功"); else if (key < bt->key) return BSTSearch(bt->lchild,key); else return BSTSearch(bt->rchild,key); } }
插入關鍵字
/**********插入關鍵字*********/ int BSTInsert(BiNode *bt,int key) { if (bt == NULL) { bt =(BiNode*) malloc(sizeof(BiNode)); bt->lchild = bt->rchild = NULL; bt->key = key; return 1; } else { if (key == bt->key) //關鍵字已經存在於樹中,插入失敗 return 0; else if (key < bt->key) return BSTInsert(bt->lchild, key); else return BSTInsert(bt->rchild, key); } }