二叉排序树(Java)
来源:互联网 发布:苹果电脑怎么用淘宝 编辑:程序博客网 时间:2024/05/16 10:00
二叉排序树的删除代码,在网上看到很多,都感觉有点繁杂难以理解,于是,我结合了TreeMap的remove()方法写出二叉排序树的实现,如果有错误的地方,还请大家多多指正~~
性质
二叉排序树又称“二叉查找树”、“二叉搜索树”。二叉排序树:或者是一棵空树,或者是具有下列性质的二叉树:
- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值。
- 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值。
- 它的左、右子树也分别为二叉排序树。
代码实现
package com.xqq.二叉排序树BST;import java.util.Stack;public class MyBST { private Node head; private int size; public MyBST() { } public MyBST(int value) { head = new Node(value); size++; } public MyBST(int[] values) { size += values.length; for (int i = 0; i < values.length; i++) { insert(values[i]); } } /** * 插入节点 */ public void insert(int value) { if (head == null) { head = new Node(value); } else { insert(head, value); } } /** * 递归插入元素 */ public void insert(Node head, int value) { if (value <= head.value) { if (head.left == null) { head.left = new Node(value); size++; } else { insert(head.left, value); } } else { if (head.right == null) { head.right = new Node(value); size++; } else { insert(head.right, value); } } } /** * 检查是否包含该元素 */ public boolean contains(int value) { Node curr = head; while(curr != null){ if(curr.value > value){ curr = curr.left; }else if(curr.value < value){ curr = curr.right; }else { return true; } } return false; } /** * 移除节点:分3种情况 * 1. 该节点为叶子节点,直接让其父节点指向的该节点置为空 * 2. 该节点有一个孩子节点,则让其父节点指向其孩子节点 * 3. 该节点有两个孩子,则让其右孩子节点的最左节点与该节点交换位置, * 转换为情况1或2,删除该节点 * * 注意:若该节点为根节点时,需要对head进行相应的操作 */ public boolean remove(int value){ // 查找节点 Node curr = search(value); // 节点不存在,直接返回false if(curr == null) return false; // 查找其父亲节点 Node father = getFather(value); // 如果该节点有两个孩子,则找到其右孩子节点的最左节点 if(curr.left != null && curr.right != null){ // 找到右孩子节点的最左节点 Node temp = getMostLeft(curr); // 找到其父亲节点 father = getFather(temp.value); // 将找到的该节点上移至需要删除节点的位置 curr.value = temp.value; // 准备删除找到的节点 curr = temp; } // 找到需要删除的节点替补的孩子节点 Node replacement = (curr.left != null ? curr.left : curr.right); if(replacement != null){ if(father == null){ head = replacement; }else if(father.left == curr){ father.left = replacement; }else { father.right = replacement; } curr.left = curr.right = null; }else if(father == null){//为根节点 head = null; }else {//无孩子,为叶子节点 if(father.left == curr) father.left = null; else father.right = null; } return true; } /** * 找到节点的右孩子的最左节点 */ private Node getMostLeft(Node curr) { Node p = curr.right; while(p.left != null){ p = p.left; } return p; } /** * 获取父节点 */ public Node getFather(int value){ Node curr = head; Node father = null; while(curr != null){ if(curr.value < value){ father = curr; curr = curr.right; }else if(curr.value > value){ father = curr; curr = curr.left; }else { return father; } } return null; } /** * 查找节点 */ public Node search(int value){ Node curr = head; while(curr != null){ if(curr.value < value){ curr = curr.right; }else if(curr.value > value){ curr = curr.left; }else { return curr; } } return null; } public int size() { return size; } /** * 非递归:中序遍历 */ public void inOrder(){ System.out.print("非递归中序遍历: "); Node curr = head; if(curr == null) return ; Stack<Node> stack = new Stack<Node>(); while(!stack.isEmpty() || curr != null){ if(curr != null){ stack.push(curr); curr = curr.left; }else { curr = stack.pop(); System.out.print(curr.value + " "); curr = curr.right; } } System.out.println(); } @Override public String toString() { inOrder(); return ""; } public static class Node { int value; Node left, right; public Node(int value) { this.value = value; } @Override public String toString() { return value + " "; } }}测试代码:package com.xqq.二叉排序树BST;public class Test { public static void main(String[] args) { MyBST bst = new MyBST(new int[]{3, 2, 1, 4}); bst.toString(); System.out.println(bst.contains(2)); System.out.println(bst.remove(3)); System.out.println(bst.remove(6)); bst.toString(); }}运行结果:非递归中序遍历: 1 2 3 4 truetruefalse非递归中序遍历: 1 2 4
二叉排序树性能分析
- 每个结点的Ci为该结点的层次数。
- 最好的情况是二叉排序树的形态和折半查找的判定树相同,其平均查找长度和logn成正比(O(log2(n)))。
- 最坏情况下,当先后插入的关键字有序时,构成的二叉排序树为一棵斜树,树的深度为n,其平均查找长度为(n + 1) / 2。也就是时间复杂度为O(n),等同于顺序查找。
- 因此,如果希望对一个集合按二叉排序树查找,最好是把它构建成一棵平衡的二叉排序树(平衡二叉树)。
0 0
- java二叉排序树(BST)
- 二叉排序树(Java)
- 二叉排序树(java)
- Java二叉排序树
- JAVA-二叉排序树
- 二叉排序树的判断(hackerrank) java
- 二叉查找树(二叉排序树)Java实现
- Java-源代码-二叉排序树
- 二叉排序树的建立-java
- 二叉排序树 查找算法 java
- Java实现二叉排序树
- java实现二叉排序树
- JAVA --- BST二叉排序树实现
- Java实现二叉排序树
- java实现二叉排序树
- 二叉排序树Java版
- Java实现二叉排序树
- 二叉排序树-Java实现
- Counting Sort
- Git系列 --- git-dir & work-tree
- 精彩的一天
- HDU 3366 Passage (dp)
- 53. Maximum Subarray
- 二叉排序树(Java)
- ProgressBar(进度条)
- Codeforces 274A:k-Multiple Free Set(初识set集合)
- 用普通类模拟枚举--枚举的原理
- memcached入门(1):开篇
- 2016第三场多校联盟训练1011 Teacher Bo(曼哈顿距离)
- (24)HTML标签详解之<audio><video><source>
- 2016多校赛 Teacher Bo(曼哈顿距离)
- Shader特效——“水彩画”的实现【GLSL】