我是靠谱客的博主 丰富缘分,这篇文章主要介绍java数据结构-二叉排序树(删除只有一个子节点的节点,删除有两个节点的节点),现在分享给大家,希望可以做个参考。

在二叉排序搜索树中,要删除有子节点的节点的话。
有两种情况
第一种-要删除的节点有一个子节点:
1、判断子节点是左节点还是右节点
2、判断出来是左节点的话
2.1、看看其父节点的左节点是不是我要删除的节点
2.2、是的话就将其父节点的左节点设置为其左子节点
2.3、如果不是左节点的话,就将其父节点的右节点设置为其左子节点
3、如果是右子节点的话,同理。但是要注意将左右设置清楚
第二种-要删除的节点有两个子节点:
1、找到其的后继节点
2、将其后继节点的值赋值给要删除的节点的值

代码实现

节点类

复制代码
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
package com.demo4; public class Node { int value; Node left; Node right; public Node(int value){ this.value = value; } public void add(Node node) { if(node == null){ return; } if(node.value > this.value){ if(this.right == null){ this.right = node; } else{ this.right.add(node); } } if(node.value < this.value){ if(this.left == null){ this.left = node; } else{ this.left.add(node); } } } public void middleshow(Node node) { if(node == null){ return; } middleshow(node.left); System.out.println(node.value); middleshow(node.right); } public Node search(int value) { if(this.value == value){ return this; } else if(value < this.value){ if(this.left == null) { return null; } return left.search(value); } else{ if(this.right == null){ return null; } return right.search(value); } } public Node searchParent(int value) { if((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)){ return this; } else{ if(this.value > value && this.left != null){ return this.left.searchParent(value); } else if(this.right != null){ return this.right.searchParent(value); } else{ return null; } } } }

二叉排序树类

复制代码
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
package com.demo4; public class BinarySearchTree { Node root; public void add(Node node){ if(root == null){ root = node; } else{ root.add(node); } } public void middleshow(){ if(root == null){ System.out.println("This tree is Empty."); return; } else{ root.middleshow(root); } } public Node search(int value){ if(root == null) return null; else{ return root.search(value); } } public Node searchParent(int value){ if(root == null) return null; else{ return root.searchParent(value); } } public void delete(int value){ if(root == null){ return; } else{ Node target = search(value); if(target == null){ return; } Node parent = searchParent(value); if(target.left == null && target.right == null){ if(parent.left.value == value){ parent.left = null; } else{ parent.right = null; } }//有两个子节点 else if(target.left != null && target.right != null){ int min = deleteMin(target.right); target.value = min; } else { if(target.left != null){ if(parent.left.value == value){ parent.left = target.left; } else{ parent.right = target.left; } } else{ if(parent.right.value == value){ parent.right = target.right; } else{ parent.left = target.right; } } } } } private int deleteMin(Node node) { Node target = node; while (target.left != null){ target = node.left; } delete(target.value); return target.value; } }

测试类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public class testBST { public static void main(String[] args) { int [] arr = new int[] {7,3,10,12,5,1,9}; BinarySearchTree bst = new BinarySearchTree(); for(int i : arr){ bst.add(new Node(i)); } System.out.println(bst.root.value); bst.delete(bst.root.value); System.out.println("**************************************************删除后"); System.out.println(bst.root.value);

测试结果
我删除的是root节点来测试
在这里插入图片描述

最后

以上就是丰富缘分最近收集整理的关于java数据结构-二叉排序树(删除只有一个子节点的节点,删除有两个节点的节点)的全部内容,更多相关java数据结构-二叉排序树(删除只有一个子节点内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部