一、二叉搜索树
二叉搜索树相关操作函数
- Find,查找
TreeNode* Find(ElementType x,TreeNode* BST)
{
while(BST){
if(x > BST->data)
BST = BST->right;
else if(x < BST->data)
BST = BST->left;
else
return BST;
}
return NULL;
}
- FindMin and FindMax,寻找最大最小元素
TreeNode* FindMin( TreeNode* BST)
{
if(BST){
while( BST->left) BST = BST->left;
}
return BST;
}
- insert ,插入
TreeNode* Insert( TreeNode* BST, ElementType X )
{
if( !BST ){
/* 若原树为空,生成并返回一个结点的二叉搜索树 */
BST = new TreeNode(X);
}
else {
/* 开始找要插入元素的位置 */
if( X < BST->Data )
BST->Left = Insert( BST->Left, X ); /*递归插入左子树*/
else if( X > BST->Data )
BST->Right = Insert