版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Chapter8searchtrees17
1722
19
6
11(a)
(b)6
9
615
3(c)
(d)Figure8-3invalidbinarysearchtreesAssume:TofindT,ComparingTwithroot,T<root
goleftT>root
go
rightAlgorithm
searchBST(val
root
<pointer>,val
argument<key>)Search
a
binary
search
tree
for
a
given
value
Preroot
is
the
root
to
a
binary
tree
orsubtree
argument
is
the
key
value
requested
Return
thenode
addressif
thevalue
isfoundNullif
thenode
isnot
in
the
tree1
if(root
is
null)1
return
null2end
if3
if
(argument<root->key)1return
searchBST(root->left,argument)4
else
if(argument>root->key)1
return
searchBST
(root->right,argument)
5
else1
return
root6
end
ifOperationsonbinarysearchtree
traversalsAlgorithms
areidentical
to
theones
in
chapter
7binary
search
treesearchalgorithmsFind
smallest
nodeFind
largest
nodeFind
requested
node●Insert
nodeSteps:1.Follow
the
branches
to
an
empty
subtree2.Insert
the
new
nodeAll
inserts
take
place
ata
leaf
or
aleaflike
node
thathas
onlyone
null
branchAfterinserting23238
44
1②
20358238)
44
12
2①
35Afterinserting
192318
4
1②
20
35Afterinserting.3819Note:node
withequalvaluesarefoundinthe
right
subtree52525②1
if(root
is
null)1
root
=new2
1else
pwalk
=root2
io
p(
k
a
ull)2
1
i
pl
key)3
else4end
P*alk=pwalk>isht3
end
loopLocationfornewnodefound4
if1
(n
k->el
f
pa
>key)5
el1s
->right
=new
6end
if3
end
if4returnwt-ent<nt>e-pareweftk->llk-wpwaey<lkw-aepwf(nlknwtpotlnaparepw1oAlgorithminsertBST(ref
root<pointer>,val
new
<pointer>)Insertnodecontainingnewdata
into
BSTusing
iterationPrerootisaddressof
firstnode
in
aBSTnewisaddressof
node
containingdatatobeinsertedPostnewnodeinsertedintothe
tree23Iterativeinsert(algorithm)end
insertBSTAlgorithm
8-4
iterativebinary
search
tree
insertwalk20parent
nul812new
一
19Finalpositionwhen
pwalk
nulRecursive
insertnode(algorithm)Algorithm
addBST(refroot
<pointer>,val
new
<pointer>)InsertnodecontainingnewdataintoBSTusingrecursionPrerootisaddressof
currentnodein
a
BSTnewisaddressof
nodecontainingdatatobe
insertedPostnewnodeinsertedintothetree1
if(root
is
null)i
root=new2
elselocate
null
subtreeforinsertion1
if(new->key<root->key)1
addBST(root->left,new)2
else1
addBST(root->right,new)3
end
if3end
if4returnend
insertBSTalgorithm8-5addnodeto
BST
recursivelyDelete
node1.locate
it●
2.delete:thereare4
possible
cases-The
node
has
no
children:set
the
delete
node's
parent
to
null,recycle
itsmemory,and
return-Has
only
a
right
subtree:attach
the
right
subtree
to
the
delete
node'sparent
and
recycle
its
memory.—Has
only
a
left
subtree:attach
the
left
subtree
to
the
delete
node's
parentand
recycle
its
memory.—Has
two
subtree:to
delete
a
node
from
the
middle
of
a
tree,to
keep
thebalance,we
must
find
a
node,...,two
ways:·Find
the
largest
node
in
the
deleted
node's
left
subtree,move
its
data
toreplace
the
deleted
node's
data·Find
the
smallest
node
in
the
deleted
node's
right
subtree,move
its
data
toreplace
the
deleted
node's
data—Regardless
of
which
logic
we
use,we
will
be
moving
data
from
a
leaf
orleaflike
node
that
can
be
deleted.(a)Deletenode(44)thathasnochildrenbefore
23
dltpt(44(12
152
12(b)Delete
node(44)with
no
left
childbefore
dltpt
|231441252(c)Delete
node(18)with
no
left
childtree
dltptr12
44
~8
区4区
区2江(d)Deletenode(23)withtwochildrenafterafterafter52afterdltptr23i44beforebefore1if
(root
is
null)1
return
false
Base
case
12endif
Nodenot
find3if(ditkey<root->data.key)1return
deleteBST
(root->left,dltkey)
4
elseif(dltkey
>root->data.key)1return
deleteBST
(root->right,dltkey)5
else//deletenodefound—testforleafnode1
if(root->left
null)3recycle(dltptr)4return
true2
1el
(t
o
ght
nuI)
Base
case
2tiroo->rti
ro
cy=crloe-l
rf)t
After
node
deleted4
return
true3
elsel/
odi
f
ot
a
leaf
(or
leaflike),find
largest
node
on
left
subtree
lo)de
found,move
data
and
delete
leafnode4re
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- JJF 2464-2026可吸附有机卤素测定仪校准规范
- T/SHBX 053-2025全生物降解食品接触用一次性手套
- T/PPZL 039-2025黄河流域内蒙古段草地生态系统服务价值评估技术规程
- T/CECCEDA 8-2026人工智能 数据资产可信流通管理框架
- T/CHASA 017-2024家电上门服务全流程用户体验评价规范
- T/CSMT-YB 018-2026γ射线成像探测系统检测规范
- GB/T 591-2026船用法兰铸铁截止止回阀
- 2025~2026学年河北省廊坊市校联考冀教版六年级上册第三次月考数学试卷【附答案】
- 江苏省新沂市度第二期期2027届数学七上期末联考模拟试题含解析
- PHP工程师经典面试题(含详细答案解析)
- 湖州幄肯中欣新能源科技有限公司太阳能光伏、氢能装备关键元器件制造项目环评报告
- 物业管理委托合同大小物业CBRE模板
- 儿童青少年情绪相关精神障碍的早期识别与治疗课件
- 北京市建筑工程资料管理规程
- 《X射线管原理与应用》课件
- (必考题)高中物理必修三第十一章《电路及其应用》测试题(有答案解析)
- 全国职业院校技能大赛高职组(直播电商赛项)备赛试题库(含答案)
- (正式版)HG∕T 20644-2024 弹簧支吊架选用标准
- HGT 4205-2024《工业氧化钙》规范要求
- 铜锌锡硫硒半导体薄膜制备方法概述
- 液压缸的典型结构
评论
0/150
提交评论