二叉排序树
二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),亦称二叉搜索树。是数据结构中的一类。在一般情况下,查询效率比链表结构要高。
一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值; (3)左、右子树也分别为二叉排序树; (4)没有键值相等的结点。
二叉排序树的删除(有以下三种情况):
情况一:删除叶子结点
(1)找到需要删除的targetNode
(2)找到需要删除的targetNode的父节点parentNode
(3)确定需删除的targetNode是左子结点还是右子结点 如果是左子结点:parentNode.left = null 如果是右子结点:parentNode.right = null
情况二:删除只有一颗子树的结点 (1)找到需要删除的targetNode
(2)找到需要删除的targetNode的父节点parentNode
(3)确定targetNode的子结点是左子结点还是右子结点
(4)确定targetNode是parent的左子结点还是右子结点
(5)如果targetNode是parentNode的左子结点 <1>如果targetNode的子结点是左子结点:parentNode.left = targetNode.left <2>如果targetNode的子结点是右子结点:parentNode.left = targetNode.right
(6)如果targetNode是parentNode的右子结点 <1>如果targetNode的子节点是左子节点:parentNode.right = targetNode.left <2>如果targetNode的子节点是右子节点:parentNode.right = targetNode.right
情况三:删除有两颗子树的结点
(1)找到需要删除的targetNode
(2)找到需要删除的targetNode的父结点parentNode
(3)从targetNode的右子树找最小的结点
(4)用一个临时变量temp接收此在右子树寻找最小的结点
(5)删除该最小的结点
(6)TargetNode.value = temp
实现代码:
package com
.tree
;
public class BinarySortTreeDemo {
public static void main(String
[] args
) {
int[] arr
= {7,3,10,12,5,1,9,2};
BinarySortTree binarySortTree
= new BinarySortTree();
for (int i
= 0; i
< arr
.length
; i
++) {
binarySortTree
.addNode(new Node(arr
[i
]));
}
binarySortTree
.delNode(7);
binarySortTree
.infixOrder();
}
}
class BinarySortTree {
private Node root
;
public Node
searchTarget(int value
)
{
if (root
== null
)
return null
;
else
return root
.searchTarget(value
);
}
public Node
searchparentNode(int value
)
{
if (root
== null
)
return null
;
else
return root
.searchParentNode(value
);
}
public int delRightTreeMin(Node node
){
Node target
= node
;
while (target
.getLeft() != null
)
{
target
= target
.getLeft();
}
delNode(target
.getValue());
return target
.getValue();
}
public void delNode(int value
)
{
if (root
== null
)
return;
else {
Node targetNode
= searchTarget(value
);
if (targetNode
== null
)
return;
if (root
.getLeft() == null
&& root
.getRight() == null
)
{
root
= null
;
return;
}
Node parentNode
= searchparentNode(value
);
if (targetNode
.getLeft() == null
&& targetNode
.getRight() == null
)
{
if (parentNode
.getLeft() != null
&& parentNode
.getLeft().getValue() == value
)
parentNode
.setLeft(null
);
else if (parentNode
.getRight() != null
&& parentNode
.getRight().getValue() == value
)
parentNode
.setRight(null
);
}
else if (targetNode
.getLeft() != null
&& targetNode
.getRight() != null
)
{
int minVal
= delRightTreeMin(targetNode
.getRight());
targetNode
.setValue(minVal
);
}
else {
if (targetNode
.getLeft() != null
) {
if (parentNode
!= null
) {
if (parentNode
.getLeft().getValue() == value
)
parentNode
.setLeft(targetNode
.getLeft());
else {
parentNode
.setRight(targetNode
.getLeft());
}
}
else
{
root
= targetNode
.getLeft();
}
}
else
{
if (parentNode
!= null
)
{
if (parentNode
.getLeft().getValue() == value
)
{
parentNode
.setLeft(targetNode
.getRight());
}else
parentNode
.setRight(targetNode
.getRight());
}
else {
root
= targetNode
.getRight();
}
}
}
}
}
public void addNode(Node node
) {
if (root
== null
)
root
= node
;
else
root
.addNode(node
);
}
public void infixOrder()
{
if (root
!= null
)
root
.infixOrder();
else
System
.out
.println("树为空");
}
}
class Node
{
private int value
;
private Node left
;
private Node right
;
public Node(int value
) {
this.value
= value
;
}
public Node
searchTarget(int value
)
{
if (value
== this.value
)
return this;
else if (value
< this.value
){
if (this.left
== null
)
return null
;
else
return this.left
.searchTarget(value
);
}else
{
if (this.right
== null
)
return null
;
else
return this.right
.searchTarget(value
);
}
}
public Node
searchParentNode(int value
)
{
if ((this.left
!= null
&& this.left
.value
== value
)||
(this.right
!= null
&& this.right
.value
== value
))
{
return this;
}else
{
if (value
< this.value
&& this.left
!= null
)
return this.left
.searchParentNode(value
);
else if (value
>= this.value
&& this.right
!= null
)
return this.right
.searchParentNode(value
);
else {
return null
;
}
}
}
public void addNode(Node node
) {
if (node
== null
)
return;
if (node
.value
< this.value
)
{
if (this.left
== null
)
this.left
= node
;
else
this.left
.addNode(node
);
}else
{
if (this.right
== null
)
this.right
= node
;
else
this.right
.addNode(node
);
}
}
public void infixOrder()
{
if (this.left
!= null
)
this.left
.infixOrder();
System
.out
.println(this);
if (this.right
!= null
)
this.right
.infixOrder();
}
public int getValue() {
return value
;
}
public void setValue(int value
) {
this.value
= value
;
}
public Node
getLeft() {
return left
;
}
public void setLeft(Node left
) {
this.left
= left
;
}
public Node
getRight() {
return right
;
}
public void setRight(Node right
) {
this.right
= right
;
}
@Override
public String
toString() {
return "Node{" +
"value=" + value
+
'}';
}
}