Skip to content

Instantly share code, notes, and snippets.

@ochilab
Last active November 18, 2021 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/0728ab7196ce8c8be25ac9bcaff3fa6b to your computer and use it in GitHub Desktop.
Save ochilab/0728ab7196ce8c8be25ac9bcaff3fa6b to your computer and use it in GitHub Desktop.
二分探索木の検索処理
struct bNode{
int point;
struct bNode *left;
struct bNode *right;
};
struct bNode *binSearchNode(struct bNode *root, int point){
struct bNode *node;
node = root; //ルートの位置をスタートとする
while(node!=NULL){
if(point < node->point){ // 探索値がノードの値よりも小さい場合
node = node->left; //左へ
} else if(point > node->point){ //探索値がノードの値よりも大きい場合
node = node->right; //右へ
} else { //それ以外(同じ=みつかったということ)
return node;
}
}
return NULL;// ここに来るということは見つからなかったということ
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment