Binary Search Tree

二叉查找树性质:

如果y是x的左子树,那么key[y] <=key[x]

如果y是x的右子树,那么key[y]>=key[x]


java实现:

public class BinarySearchTree {


private Node head;


public Node getHead() {
return head;
}


public void setHead(Node head) {
this.head = head;
}


public Node insertion(int value) {
if (null == head) {
head = new Node(value);
return head;
}
Node parent = findInsertionPos(head, value);
Node newNode = new Node(value);
newNode.setParentNode(parent);
if (value <= parent.getValue()) {
parent.setLeftChild(newNode);
} else {
parent.setRightChild(newNode);
}
return head;
}


private Node findInsertionPos(Node head, int value) {
if (null == head) {
return null;
}
Node curNode = head;
while ((value <= curNode.getValue() && curNode.hasLeftChild())
|| (value > curNode.getValue() && curNode.hasRightChild())) {
if (value <= curNode.getValue()) {
curNode = curNode.getLeftChild();
} else {
curNode = curNode.getRightChild();
}
}
return curNode;
}


public void delete(Node node) {
if (!node.hasChild()) {
//没有孩子节点,直接删除
if (null == node.getParentNode()) {
head = null;
return;
} else {
if (node.getValue() <= node.getParentNode().getValue()) {
node.getParentNode().setLeftChild(null);
} else {
node.getParentNode().setRightChild(null);
}
node.setParentNode(null);
}
} else if (node.hasLeftChild() && node.hasRightChild()) {
//有两个孩子,则找到其后继节点,将后继节点的值赋予要删除节点,并将后继节点删除
Node replaceNode = findInsertionPos(node.getRightChild(),
node.getValue());
int value = replaceNode.getValue();
node.setValue(value);
delete(replaceNode);
} else {
//有一个孩子,将其孩子赋予该节点的父节点,将该节点删除
if (node.hasLeftChild()) {
if (null == node.getParentNode()) {
head = node.getLeftChild();
head.setParentNode(null);
} else {
if (node.getValue() <= node.getParentNode().getValue()) {
node.getParentNode().setLeftChild(node.getLeftChild());
} else {
node.getParentNode().setRightChild(node.getLeftChild());
}
}
node.setLeftChild(null);
} else {
if (null == node.getParentNode()) {
head = node.getRightChild();
head.setParentNode(null);
} else {
if (node.getValue() <= node.getParentNode().getValue()) {
node.getParentNode().setLeftChild(node.getRightChild());
} else {
node.getParentNode()
.setRightChild(node.getRightChild());
}
}
node.setRightChild(null);
}
node.setParentNode(null);
}
}


public Node search(int value) {
if (null == head) {
return null;
}
Node curNode = head;
while (null != curNode && curNode.getValue() != value) {
if (value <= curNode.getValue()) {
curNode = curNode.getLeftChild();
} else {
curNode = curNode.getRightChild();
}
}
return curNode;
}


public int getMinValue() throws Exception {
if (null == head) {
throw new Exception("tree is null");
}
Node curNode = head;
while (curNode.hasLeftChild()) {
curNode = curNode.getLeftChild();
}
return curNode.getValue();
}


public int getMaxValue() throws Exception {
if (null == head) {
throw new Exception("tree is null");
}
Node curNode = head;
while (curNode.hasRightChild()) {
curNode = curNode.getRightChild();
}
return curNode.getValue();
}
}


public class Node {
private int value;
private Node leftChild;
private Node rightChild;
private Node parentNode;

public Node(){

}

public Node(int value){
this.value = value;
}

public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node getParentNode() {
return parentNode;
}
public void setParentNode(Node parentNode) {
this.parentNode = parentNode;
}
public boolean hasLeftChild(){
if(null != leftChild){
return true;
}
return false;
}

public boolean hasRightChild(){
if(null != rightChild){
return true;
}
return false;
}

public boolean hasChild(){
if(null != rightChild || null != leftChild){
return true;
}
return false;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值