1.在二叉树中搜索给定结点的父结点
算法 father (t,p,q)
指针t指向二叉树T之根;father使用递归在T中搜索给定结点p之父结点;q指向p之父结点。
伪代码:
father 1.(test whether t is null)
if t=null then( q<--- l.return )
Father2.(t is the answer)
if left(t)=p or right(t)=p then (q<---t.return.)
father 3.(递归)
father (left(t),p,ql);
if ql !=null then (q<----ql.return.)
father (right(t),p,qR) .q<---qR;
2.搜索二叉树中符合数据域条件的结点
算法 Find (t ,item,q)
指针t指向二叉树之根,Find在中搜值为item的结点,指针q指向该结点。
Find1.[ t=null?]
if t= null hen (q<---null);
if data(t)=item then( q<----t.return).
Find2.[递归]
Find(Left(t) ,item ,p).
if p!=null then (q<---p.return).
Find (Right(t) ,item ,q);
3.从二叉树中删除给定结点及其左右子树
DST (t)
root 指向二叉树T之根,DST从T中删除给定结点t及其左右子树。
DST1.[t=null?]
if t=null then return .
DST 2.[t=root ?]
if t=root then (del(t).root <---null.return )
DST 3.[找t的父结点q】
p<----t.father(root,p,q).
DST 4 [修改q的指针域)
if left (q) = p then left (q) <----null ;
if right (q) = p then right (q)< ----null.
DST5.[删除p及其子树】
del(p);
算法 Del (p)
删除结点 p 及左右子树
Del1. 【p= null ?】
if p=null then return .
del 2 . [递归删除】
Del ( left §).
Del (right §).
AVAIL <= P;