二叉排序树
|字数总计:1.2k|阅读时长:5分钟|阅读量:|
二叉排序树(BST)
基本介绍
二叉排序树(Binary Sort Tree),又称为二叉查找树。它或者是一棵空树,或者是具有下列性质的二叉树:
- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
- 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
- 它的左、右子树也分别为二叉排序树。
- 特别说明:如果有相同的值,可以将该节点放在左子节点或右子节点
构造一棵二又排序树的目的,其实并不是为了排序,而是为了提高查找和插入删除关键字的速度。不管怎么说,在一个有序数据集上的查找,速度总是要快于无序的数据集的,而二叉排序树这种非线性的结构,也有利于插入和删除的实现。
二叉排序树的创建和遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| package com.nanzx.binarysorttree;
public class BinarySortTreeDemo {
public static void main(String[] args) { int[] arr = { 7, 3, 10, 12, 5, 1, 9}; BinarySortTree binarySortTree = new BinarySortTree(); for (int i = 0; i < arr.length; i++) { binarySortTree.add(new Node(arr[i])); }
System.out.println("中序遍历二叉排序树~"); binarySortTree.infixOrder();
}
}
class BinarySortTree { private Node root;
public Node getRoot() { return root; }
public void add(Node node) { if (root == null) { root = node; } else { root.add(node); } }
public void infixOrder() { if (root != null) { root.infixOrder(); } else { System.out.println("二叉排序树为空,不能遍历"); } } }
class Node { int value; Node left; Node right;
public Node(int value) { super(); this.value = value; }
@Override public String toString() { return "Node [value=" + value + "]"; }
public 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); } } }
public void infixOrder() { if (this.left != null) { this.left.infixOrder(); } System.out.println(this.value); if (this.right != null) { this.right.infixOrder(); } } }
|
查找要删除的节点以及它的父节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| class BinarySortTree { public Node search(int value) { if (root == null) { return null; } else { return root.search(value); } }
public Node searchParent(int value) { if (root == null) { return null; } else { return root.searchParent(value); } } }
class Node { public Node search(int value) { if (this.value == value) { return this; } else if (this.value > value) { if (this.left == null) { return null; } return this.left.search(value); } else { if (this.right == null) { return null; } return this.right.search(value); } }
public Node searchParent(int value) { if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) { return this; } else { if (this.left != null && this.value > value) { return this.left.searchParent(value); } else if (this.right != null && this.value <= value) { return this.right.searchParent(value); } else { return null; } } } }
|
删除节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| class BinarySortTree {
public int delRightTreeMin(Node node) { Node target = node; while (target.left != null) { target = target.left; } delete(target.value); return target.value; }
public void delete(int value) { if (root == null) { return; } else { Node targetNode = search(value); if (targetNode == null) { return; } if (root.left == null && root.right == null) { root = null; return; }
Node parent = searchParent(value); if (targetNode.left == null && targetNode.right == null) { if (parent.left == targetNode) { parent.left = null; } else if (parent.right == targetNode) { parent.right = null; } } else if (targetNode.left != null && targetNode.right != null) { int minVal = delRightTreeMin(targetNode.right); targetNode.value = minVal; } else { if (targetNode.left != null) { if (parent == null) { root = targetNode.left; } else { if (parent.left == targetNode) { parent.left = targetNode.left; } else { parent.right = targetNode.left; } } } else { if (parent == null) { root = targetNode.right; } else { if (parent.left == targetNode) { parent.left = targetNode.right; } else { parent.right = targetNode.right; } } } } } } }
|