二叉排序树及实现
二叉排序树(Binary Sort Tree, BST)又称为二叉查找树。在一般情况下,查询效率要比链表结构要高。对于二叉排序树中的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。如果有相同的值,可以将该节点放在左子节点或右子节点。
- 二叉排序树的java实现
public class BSTree {private Node root;void add(Node node) {if (root == null) {root = node;} else {root.add(node);}}void infixOrderSearch() {if (root == null) {System.out.println("二叉树为空,不能遍历");return;}root.infixOrderSearch();}Node find(int value) {if (root == null) {return null;}return root.find(value);}Node findParentNode(int value) {if(root == null){return null;}return root.findParentNode(value);}//删除节点后返回node为根节点的最小节点的值int delRightTreeMin(Node node){Node tmp = node;while(tmp.left != null){tmp = tmp.left;}delNode(tmp.value);return tmp.value;}private void delNode(int value){if(root == null){return;}//查找待删除节点,如果BST只有一个节点直接删除Node targetNode = find(value);if(targetNode == null){return;}if(root.left == null && root.right == null){root = null;return;}//查找待删除节点的节点//1.如果要删除的节点是叶子节点//2.删除有两棵子树的节点//3.删除只有一棵子树的节点Node parentNode = findParentNode(value);if(targetNode.left == null && targetNode.right == null){if(parentNode.left != null && parentNode.left.value == value){parentNode.left = null;}else if(parentNode.right != null && parentNode.right.value == value){parentNode.right = null;}}else if(targetNode.left != null && targetNode.right != null){targetNode.value = delRightTreeMin(targetNode.right);}else{if(targetNode.left != null){if(parentNode != null){if(parentNode.left.value == value){parentNode.left = targetNode.left;}else{parentNode.right = targetNode.left;}}else{root = targetNode.left;}}if(targetNode.right != null){if(parentNode != null){if(parentNode.left.value == value){parentNode.left = targetNode.right;}else{parentNode.right = targetNode.right;}}else{root = targetNode.right;}}}}//节点private static class Node {int value;Node left;Node right;Node(int value) {this.value = value;}//递归形式添加节点,满足二叉排序树的要求void add(Node node) {if (node == null) {return;}//如果待添加节点值比当前节点值小放到左子树,大于等于放到右子树if (node.value < this.value) {//左子树为空直接添加,否则继续寻找if (this.left == null) {this.left = node;} else {this.left.add(node);} } else {if (this.right == null) {this.right = node;} else {this.right.add(node);}}}//查找节点Node find(int value) {if (this.value == value) {return this;}//待查找值比当前值小,在左子树中查找,若左子树为空则查不到if (this.value > value) {if (this.left == null) {return null;}return this.left.find(value);}if (this.right == null) {return null;}return this.right.find(value);}//查找待删除节点的父节点Node findParentNode(int value) {if((this.left != null && this.left.value == value)|| (this.right != null && this.right.value == value)){return this;}if (this.value > value && this.left != null) {return this.left.findParentNode(value);}if (this.value <= value && this.right != null) {return this.right.findParentNode(value);}return null;}void infixOrderSearch() {if (this.left != null) {this.left.infixOrderSearch();}System.out.println(this);if (this.right != null) {this.right.infixOrderSearch();}}@Overridepublic String toString() {return "Node{" +"value=" + value +'}';}}public static void main(String[] args) {int[] arr = {7, 3, 10, 12, 5, 1, 9};BSTree bst = new BSTree();for (int i:arr) {bst.add(new Node(i));}bst.delNode(7);bst.infixOrderSearch();}
}
执行结果:
Node{value=1}
Node{value=3}
Node{value=5}
Node{value=9}
Node{value=10}
Node{value=12}