二叉搜索树
不仅可查找数据;还可以高效的地插入,删除数据,动态维护数据可以方便地回答很多数据之间的关系问题:
min,max,fool,ceil,rank,select
二分搜索树不一定是完全二叉树,所以用数组表示并不方便
二叉搜索树的构建
class test {
public static void main(String[] args) {
Node root = new Node(1,28) ;
root.insert(2, 16);
root.insert(3, 30);
root.insert(4, 13);
root.insert(5, 22);
root.insert(6, 29);
root.insert(7, 42);
// 查找
int a = root.search(4);
System.out.println(a);
root.removeMax();
System.out.println(1);
// // 前序遍历
// root.preOrder();
// // 中序遍历
// root.inOrder();
// // 后序遍历
// root.postOrder();
// // 层序遍历
// root.levelOrder();
// // 最小值
// System.out.println(root.minNumber());
// // 最大值
// System.out.println(root.maxNumber());
}
}
package Search;
import java.util.*;
public class Node {
private int key;
private int value;
private Node left = null;
private Node right = null;
static int count = 1;
public Node(){
}
public Node(int key, int value) {
this.key = key;
this.value = value;
}
public int getCount() {
return count;
}
public void setKey(int key) {
this.key = key;
}
public void setValue(int value) {
this.value = value;
}
public int getKey() {
return key;
}
public int getValue() {
return value;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
public int size() {
return count;
}
public boolean isEmpty() {
return count == 0;
}
public Node insert(int key, int value) {
return insert(this, key, value);
}
public boolean contain(int key) {
return contain(this, key);
}
private boolean contain(Node node, int key) {
if (node == null) {
return false;
}
if (key == node.getKey()) {
return true;
} else if (key < node.getKey()) {
return contain(node.getLeft(), key);
} else {
return contain(node.getRight().key);
}
}
public int search(int key) {
return search(this, key);
}
// 从二叉树中删除最小值所在的节点
public void removeMin(){
if(this != null){
removeMin(this);
}
}
private Node removeMin(Node node) {
if(node.getLeft() == null){
node = node.getRight();
count--;
return node;
}
node = removeMin(node.getLeft());
return node;
}
// 从二叉树中删除最小值所在的节点
public void removeMax(){
if(this != null){
removeMax(this);
}
}
// 删除最大节点
private Node removeMax(Node node) {
if(node.getRight() == null){
node = node.getLeft();
count--;
return node;
}
node = removeMax(node.getRight());
return node;
}
private int search(Node node, int key) {
if (null == node) {
return -1;
}
if (key == node.getKey()) {
return node.getValue();
} else if (key < node.getKey()) {
return search(node.getLeft(), key);
} else {
return search(node.getRight(), key);
}
}
private Node insert(Node node, int key, int value) {
if (node == null) {
count++;
return new Node(key, value);
}
if (value == node.getValue()) {
node.setValue(value);
} else if (value < node.getValue()) {
node.setLeft(insert(node.getLeft(), key, value));
} else {
node.setRight(insert(node.getRight(), key, value));
}
return node;
}
// 中序遍历
public void inOrder() {
inOrder(this);
}
private void inOrder(Node node) {
if (node != null) {
inOrder(node.getLeft());
System.out.println(node.getKey());
inOrder(node.getRight());
}
}
// 后序遍历
public void postOrder() {
postOrder(this);
}
private void postOrder(Node node) {
if (node != null) {
postOrder(node.getLeft());
postOrder(node.getRight());
System.out.println(node.getKey());
}
}
//前序遍历
public void preOrder() {
preOrder(this);
}
// 层序遍历
void levelOrder() {
List<Node> queue = new ArrayList<Node>();
queue.add(this);
while (!queue.isEmpty()) {
Node node = queue.get(0);
System.out.println(node.getValue());
queue.remove(0);
if (null != node.getLeft()) {
queue.add(node.getLeft());
}
if (null != node.getRight()) {
queue.add(node.getRight());
}
}
}
public void preOrder(Node node) {
if (node != null) {
System.out.println(node.getKey());
preOrder(node.getLeft());
preOrder(node.getRight());
}
}
// 寻找最小值
public int minNumber() {
if(count != 0){
return minNumber(this);
}
return -1;
}
private int minNumber(Node node) {
while(node.getLeft() != null){
node = node.getLeft();
}
return node.getKey();
}
// 寻找最大值
public int maxNumber() {
if(count != 0){
return maxNumber(this);
}
return -1;
}
private int maxNumber(Node node) {
while(node.getRight() != null){
node = node.getRight();
}
return node.getKey();
}
}