假設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);*/ 【上海大學 一 (分)】[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/22921.html