java实现二叉树查找树.docx

上传人:b****8 文档编号:11357825 上传时间:2023-02-28 格式:DOCX 页数:19 大小:73.02KB
下载 相关 举报
java实现二叉树查找树.docx_第1页
第1页 / 共19页
java实现二叉树查找树.docx_第2页
第2页 / 共19页
java实现二叉树查找树.docx_第3页
第3页 / 共19页
java实现二叉树查找树.docx_第4页
第4页 / 共19页
java实现二叉树查找树.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

java实现二叉树查找树.docx

《java实现二叉树查找树.docx》由会员分享,可在线阅读,更多相关《java实现二叉树查找树.docx(19页珍藏版)》请在冰豆网上搜索。

java实现二叉树查找树.docx

java实现二叉树查找树

二叉树(binary)是一种特殊的树。

二叉树的每个节点最多只能有2个子节点:

二叉树

由于二叉树的子节点数目确定,所以可以直接采用上图方式在内存中实现。

每个节点有一个左子节点(leftchildren)和右子节点(rightchildren)。

左子节点是左子树的根节点,右子节点是右子树的根节点。

 

如果我们给二叉树加一个额外的条件,就可以得到一种被称作二叉搜索树(binarysearchtree)的特殊二叉树。

二叉搜索树要求:

每个节点都不比它左子树的任意元素小,而且不比它的右子树的任意元素大。

(如果我们假设树中没有重复的元素,那么上述要求可以写成:

每个节点比它左子树的任意节点大,而且比它右子树的任意节点小)

二叉搜索树,注意树中元素的大小

二叉搜索树可以方便的实现搜索算法。

在搜索元素x的时候,我们可以将x和根节点比较:

1.如果x等于根节点,那么找到x,停止搜索(终止条件)

2.如果x小于根节点,那么搜索左子树

3.如果x大于根节点,那么搜索右子树

二叉搜索树所需要进行的操作次数最多与树的深度相等。

n个节点的二叉搜索树的深度最多为n,最少为log(n)。

 

下面是用java实现的二叉搜索树,并有搜索,插入,删除,寻找最大最小节点的操作。

删除节点相对比较复杂。

删除节点后,有时需要进行一定的调整,以恢复二叉搜索树的性质(每个节点都不比它左子树的任意元素小,而且不比它的右子树的任意元素大)。

∙叶节点可以直接删除。

∙删除非叶节点时,比如下图中的节点8,我们可以删除左子树中最大的元素(或者右树中最大的元素),用删除的节点来补充元素8产生的空缺。

但该元素可能也不是叶节点,所以它所产生的空缺需要其他元素补充……直到最后删除一个叶节点。

上述过程可以递归实现。

删除节点

删除节点后的二叉搜索树

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

importjava.util.ArrayList;

importjava.util.List;

 

public class BinarySearchTree{

 

    //树的根结点

    private TreeNoderoot= null;

 

    //遍历结点列表

    private Listnodelist= new ArrayList();

 

    private class TreeNode{

 

        private int key;

        private TreeNodeleftChild;

        private TreeNoderightChild;

        private TreeNodeparent;

 

        public TreeNode(int key,TreeNodeleftChild,TreeNoderightChild,

                TreeNodeparent){

            this.key=key;

            this.leftChild=leftChild;

            this.rightChild=rightChild;

            this.parent=parent;

        }

 

        public int getKey(){

            return key;

        }

 

        public StringtoString(){

            Stringleftkey=(leftChild== null ?

 "" :

String

                    .valueOf(leftChild.key));

            Stringrightkey=(rightChild== null ?

 "" :

String

                    .valueOf(rightChild.key));

            return "(" +leftkey+ "," +key+ "," +rightkey+ ")";

        }

 

    }

 

    /**

     *isEmpty:

判断二叉查找树是否为空;若为空,返回true,否则返回false.

     *

     */

    public booleanisEmpty(){

        if (root== null){

            return true;

        } else {

            return false;

        }

    }

 

    /**

     *TreeEmpty:

对于某些二叉查找树操作(比如删除关键字)来说,若树为空,则抛出异常。

     */

    public void TreeEmpty()throwsException{

        if (isEmpty()){

            throw new Exception("树为空!

");

        }

    }

 

    /**

     *search:

在二叉查找树中查询给定关键字

     *

     *@paramkey

     *           给定关键字

     *@return匹配给定关键字的树结点

     */

    public TreeNodesearch(int key){

        TreeNodepNode=root;

        while (pNode!

= null &&pNode.key!

=key){

            if (key

                pNode=pNode.leftChild;

            } else {

                pNode=pNode.rightChild;

            }

        }

        return pNode;

    }

 

    /**

     *minElemNode:

获取二叉查找树中的最小关键字结点

     *

     *@return二叉查找树的最小关键字结点

     *@throwsException

     *            若树为空,则抛出异常

     */

    public TreeNodeminElemNode(TreeNodenode)throwsException{

        if (node== null){

            throw new Exception("树为空!

");

        }

        TreeNodepNode=node;

        while (pNode.leftChild!

= null){

            pNode=pNode.leftChild;

        }

        return pNode;

    }

 

    /**

     *maxElemNode:

获取二叉查找树中的最大关键字结点

     *

     *@return二叉查找树的最大关键字结点

     *@throwsException

     *            若树为空,则抛出异常

     */

    public TreeNodemaxElemNode(TreeNodenode)throwsException{

        if (node== null){

            throw new Exception("树为空!

");

        }

        TreeNodepNode=node;

        while (pNode.rightChild!

= null){

            pNode=pNode.rightChild;

        }

        return pNode;

    }

 

    /**

     *successor:

获取给定结点在中序遍历顺序下的后继结点

     *

     *@paramnode

     *           给定树中的结点

     *@return若该结点存在中序遍历顺序下的后继结点,则返回其后继结点;否则返回null

     *@throwsException

     */

    public TreeNodesuccessor(TreeNodenode)throwsException{

        if (node== null){

            return null;

        }

 

        //若该结点的右子树不为空,则其后继结点就是右子树中的最小关键字结点

        if (node.rightChild!

= null){

            return minElemNode(node.rightChild);

        }

        //若该结点右子树为空

        TreeNodeparentNode=node.parent;

        while (parentNode!

= null &&node==parentNode.rightChild){

            node=parentNode;

            parentNode=parentNode.parent;

        }

        return parentNode;

    }

 

    /**

     *precessor:

获取给定结点在中序遍历顺序下的前趋结点

     *

     *@paramnode

     *           给定树中的结点

     *@return若该结点存在中序遍历顺序下的前趋结点,则返回其前趋结点;否则返回null

     *@throwsException

     */

    public TreeNodeprecessor(TreeNodenode)throwsException{

        if (node== null){

            return null;

        }

 

        //若该结点的左子树不为空,则其前趋结点就是左子树中的最大关键字结点

        if (node.leftChild!

= null){

            return maxElemNode(node.leftChild);

        }

        //若该结点左子树为空

        TreeNodeparentNode=node.parent;

        while (parentNode!

= null &&node==parentNode.leftChild){

            node=parentNode;

            parentNode=parentNode.parent;

        }

        return parentNode;

    }

 

    /**

     *insert:

将给定关键字插入到二叉查找树中

     *

     *@paramkey

     *           给定关键字

     */

    public void insert(int key){

        TreeNodeparentNode= null;

        TreeNodenewNode= new TreeNode(key, null, null, null);

        TreeNodepNode=root;

        if (root== null){

            root=newNode;

            return;

        }

        while (pNode!

= null){

            parentNode=pNode;

            if (key

                pNode=pNode.leftChild;

            } else if (key>pNode.key){

                pNode=pNode.rightChild;

            } else {

                //树中已存在匹配给定关键字的结点,则什么都不做直接返回

                return;

            }

        }

        if (key

            parentNode.leftChild=newNode;

            newNode.parent=parentNode;

        } else {

            parentNode.rightChild=newNode;

            newNode.parent=parentNode;

        }

 

    }

 

    /**

     *insert:

从二叉查找树中删除匹配给定关键字相应的树结点

     *

     *@paramkey

     *           给定关键字

     */

    public void delete(int key)throwsException{

        TreeNodepNode=search(key);

        if (pNode== null){

            throw new Exception("树中不存在要删除的关键字!

");

        }

        delete(pNode);

    }

 

    /**

     *delete:

从二叉查找树中删除给定的结点.

     *

     *@parampNode

     *           要删除的结点

     *

     *           前置条件:

给定结点在二叉查找树中已经存在

     *@throwsException

     */

    private void delete(TreeNodepNode)throwsException{

        if (pNode== null){

            return;

        }

        if (pNode.leftChild== null &&pNode.rightChild== null){ //该结点既无左孩子结点,也无右孩子结点

            TreeNodeparentNode=pNode.parent;

            if (pNode==parentNode.leftChild){

                parentNode.leftChild= null;

            } else {

                parentNode.rightChild= null;

            }

            return;

        }

        if (pNode.leftChild== null &&pNode.rightChild!

= null){ //该结点左孩子结点为空,右孩子结点非空

            TreeNodeparentNode=pNode.parent;

            if (pNode==parentNode.leftChild){

                parentNode.leftChild=pNode.rightChild;

                pNode.rightChild.parent=parentNode;

            } else {

                parentNode.rightChild=pNode.rightChild;

                pNode.rightChild.parent=parentNode;

            }

            return;

        }

        if (pNode.leftChild!

= null &&pNode.rightChild== null){ //该结点左孩子结点非空,右孩子结点为空

            TreeNodeparentNode=pNode.parent;

            if (pNode==parentNode.leftChild){

                parentNode.leftChild=pNode.leftChild;

                pNode.rightChild.parent=parentNode;

            } else {

                parentNode.rightChild=pNode.leftChild;

                pNode.rightChild.parent=parentNode;

            }

            return;

        }

        //该结点左右孩子结点均非空,则删除该结点的后继结点,并用该后继结点取代该结点

        TreeNodesuccessorNode=successor(pNode);

        delete(successorNode);

        pNode.key=successorNode.key;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 企业管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1