算法思想
首先将待查关键字key与根结点关键字t进行比较,如果:
1)key=t,则返回根结点地址;
2)key<t,则进一步查找左子树;
3)key>t,则进一步查找右子树;


对应的递归算法如下:

BSTree SearchBST(BSTree bst, ElemType key) {
	if (!bst)
		return NULL;
	else if (bst->key == key)
		return bst;
	else if (bst->key > key)
		return SearchBST(bst->lchild, key);
	else
		return SearchBST(bst->rchild, key);
}

对应的非递归算法如下:

BSTNode *BST_Search(Bitree T, ElemType key, BSTNode *&p) {
	p = NULL;//p指向待查找结点的双亲,用于插入和删除操作中
	while(T!=NULL&&key!=T->data){
		if (key < T->data)
			T = T->lchild;
		else
			T = T->rchild;
	}
}

相关文章:

  • 2021-07-31
  • 2022-01-05
  • 2021-10-19
  • 2021-10-21
  • 2021-11-13
  • 2022-01-01
  • 2022-01-12
  • 2022-02-09
猜你喜欢
  • 2022-12-23
  • 2021-12-03
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
相关资源
相似解决方案