二叉搜索树基于int类型key与Object类型value简单实现
403行代码没有一句废话,写的太累,后续闲下来了再补全注释与思路,后面可能还会实现基于泛型K,V key与value的实现
以下只提供代码(部分方法使用递归与非递归两种方法实现):
import java.util.ArrayList; public class BSTree1 { BSTNode root; static class BSTNode { int key; Object value; BSTNode left; BSTNode right; public BSTNode(int key) { this.key = key; } public BSTNode(int key, Object value) { this.key = key; this.value = value; } public BSTNode(int key, Object value, BSTNode left, BSTNode right) { this.key = key; this.value = value; this.left = left; this.right = right; } }
public Object get(int key) { BSTNode node = root; while (node != null) { if (node.key < key) { node = node.right; } else if (key < node.key) { node = node.left; } else { return node.value; } } return null; }
public Object min() { return min(root); }
private Object min(BSTNode node) { if (node == null) { return null; } BSTNode p = node; while (p.left != null) { p = p.left; } return p.value; }
public Object max() { return doMax(root); } private Object doMax(BSTNode node){ if (node == null){ return null; } if (node.right == null){ return node.value; } return doMax(node.right); }
public Object max() { return max(root); }
private Object max(BSTNode node) { if (node == null) { return null; } BSTNode p = node; while (p.right != null) { p = p.right; } return p.value; }
public void put(int key, Object value) { BSTNode node = root; BSTNode parent = null; while (node != null) { parent = node; if (node.key < key) { node = node.right; } else if (key < node.key) { node = node.left; } else { node.value = value; return; } } if (parent == null) { root = new BSTNode(key, value); return; } if (key < parent.key) { parent.left = new BSTNode(key, value); } else { parent.right = new BSTNode(key, value); } }
public Object successor(int key) { BSTNode p = root; BSTNode ancestorFromRight = null; while (p != null) { if (key < p.key) { ancestorFromRight = p; p = p.left; } else if (p.key < key) { p = p.right; } else { break; } } if (p == null) { return null; } if (p.right != null) { return min(p.right); } return ancestorFromRight != null ? ancestorFromRight.value : null; }
public Object predecessor(int key) {
BSTNode p = root; BSTNode ancestorFromLeft = null; while (p != null) { if (key < p.key) { p = p.left; } else if (p.key < key) { ancestorFromLeft = p; p = p.right; } else { break; } }
if (p == null) { return null; } if (p.left != null) { return max(p.left); } return ancestorFromLeft != null ? ancestorFromLeft.value : null; }
public Object delete(int key) { BSTNode p = root; BSTNode parent = null; while (p != null) { if (key < p.key) { parent = p; p = p.left; } else if (p.key < key) { parent = p; p = p.right; } else { break; } } if (p == null) { return null; } if (p.left == null) { shift(parent, p, p.right); } else if (p.right == null) { shift(parent, p, p.left); } else { BSTNode s = p.right; BSTNode sParent = p; while (s.left != null) { sParent = s; s = s.left; }
if (sParent != p) { shift(sParent, s, s.right); s.right = p.right; } shift(parent, p, s); s.left = p.left; } return p.value; }
public Object delete(int key) { ArrayList<Object> result = new ArrayList<>(); root = doDelete(root, key, result); return result.isEmpty() ? null : result.get(0); }
private BSTNode doDelete(BSTNode node, int key, ArrayList<Object> result) { if (node == null) { return null; } if (key < node.key) { node.left = doDelete(node.left, key, result); return node; } else if (node.key < key) { node.right = doDelete(node.right, key, result); return node; } result.add(node.value);
private void shift(BSTNode parent, BSTNode deleted, BSTNode child) { if (parent == null) { root = child; } else if (deleted == parent.left) { parent.left = child; } else { parent.right = child; } } }
|