假设root是一棵给定的非空查找树对于下面给出的子程序当执行注释中给出的调用语句时就可以实现如下的操作在非空查找树root中查找值为k 的结点;若值为k的结点在树中且是一个叶子结点则删除此叶子结点同时置success
为真;若值为k的结点不在树中或者虽然在树中但不是叶子结点则不进行删除仅置success为假应注意到非空查找树只包含一个结点情况此时树中的唯一结点既是根结点也是叶子结点
#include <stdioh>
typedef struct node {
int key;
struct node *left *right;
} node;
node *root; int ksuccess;
void del_leaf(node **t int k int *sn)
{ node *p *pf; p=*t; *sn=;
while(_()_&&!*sn)
if (k==p>key) *sn =;
else { _()_;
if (k<p>key ) p=p>left; else p=p>right; }
if (*sn && p>left==NULL && p>right==null)
{ if (_()_ )
if (pf>left ==p ) pf >left=null; else pf>right=null;
else _()_ ;
free(p); }
else *sn=;
/*call form :del_leaf( &root k &success);*/ 【上海大学 一 (分)】
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []