二叉搜索树的查找——递归算法:bool Find(BTreeNode BST,ElemType
二叉搜索树的查找——递归算法:bool Find(BTreeNode BST,ElemType
二叉树的存储结构为: structBTreeNode{ElemTypedata;BTreeNode* lchild;BTreeNode* rchild;};请写出其非递归的中序遍历算法voidInOrderWithoutRecursion(BTreeNode* root) {if (root == NULL) return;BTNode* p = root;stack<BTNode*> s;while (!s.empty() || p) {//入栈中,左子树的左子树在入栈while (p) {_________________;__________________;}if (!s.empty()){//当p为空时,说明根和左子树都遍历完,该进入右子树 ________________;cout<< ”“<< p->data; _________________;}}cout<<endl;}
二叉树的存储结构为: structBTreeNode{ElemTypedata;BTreeNode* lchild;BTreeNode* rchild;};请写出其非递归的中序遍历算法voidInOrderWithoutRecursion(BTreeNode* root) {if (root == NULL) return;BTNode* p = root;stack<BTNode*> s;while (!s.empty() || p) {//入栈中,左子树的左子树在入栈while (p) {_________________;__________________;}if (!s.empty()){//当p为空时,说明根和左子树都遍历完,该进入右子树 ________________;cout<< ”“<< p->data; _________________;}}cout<<endl;}
二叉搜索树的查找——递归算法: bool Find(BTreeNode* BST,ElemType& item) { if (BST==NULL) return false; //查找失败 else { if (item==BST->data){ item=BST->data;//查找成功 return ___________;} else if(itemdata) return Find(______________,item); else return Find(_______________,item); }//if }
二叉搜索树的查找——递归算法: bool Find(BTreeNode* BST,ElemType& item) { if (BST==NULL) return false; //查找失败 else { if (item==BST->data){ item=BST->data;//查找成功 return ___________;} else if(itemdata) return Find(______________,item); else return Find(_______________,item); }//if }
1