参考:https://blog.csdn.net/u013405574/article/details/51058133
https://blog.csdn.net/zyj8170/article/details/7045226
二叉搜索树的定义
二叉搜索树,也称有序二叉树,排序二叉树,是指一棵空树或者具有下列性质的二叉树:
1. 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
2. 若任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
3. 任意节点的左、右子树也分别为二叉查找树。
4. 没有键值相等的节点。
一棵二叉搜索树(BST)是以一棵二叉树来组织的,可以用链表数据结构来表示,其中,每一个结点就是一个对象,一般地,包含数据内容key和指向孩子(也可能是父母)的指针属性。如果某个孩子结点不存在,其指针属性值为空(NIL)。二叉搜索树中的关键字key的存储方式总是满足二叉搜索树的性质:设x是二叉搜索树中的一个结点。如果y是x左子树中的一个结点,那么会有y.key<=x.key;如果y是x右子树中的一个节点,那么有y.key>=x.key。
二叉搜索树查找:
顾名思义,二叉搜索树很多时候用来进行数据查找。这个过程从树的根结点开始,沿着一条简单路径一直向下,直到找到数据或者得到NIL值。
如下图所示:
由图可以看出,对于遇到的每个结点x,都会比较x.key与k的大小,如果相等,就终止查找,否则,决定是继续往左子树还是右子树查找。因此,整个查找过程就是从根节点开始一直向下的一条路径,若假设树的高度是h,那么查找过程的时间复杂度就是O(h)。
BST查找的递归算法与非递归算法伪代码分别如下:
//递归实现
Tree_Search(x, k):
if x == NIL or x.key == k :
return x
if k < x.key
return Tree_Search(x.left, k)
else return Tree_Search(x.right, k)
//非递归迭代实现
Tree_Search(x, k) :
while x!=NIL and k!=x.key:
if k < x.key
x = x.left
else x = x.right
return x
一般来说,迭代方式的效率比递归方式高很多。
前驱和后继:
对于给定的一棵二叉搜索树,如果所有结点的key均不相同,那么结点x的前驱是指小于x.key的最大关键字的结点;而一个结点x的后继是指大于x.key的最小关键字的结点。
现在,我们考虑如何求解一个结点x的后继,(求前驱也类似,对称的结构):
对于结点x,如果其右子树不为空,那么x的后继一定是其右子树的最左边的结点。而如果x的右子树为空,并且有一个后继,那么其后继必然是x的最底层的祖先,并且后继的左孩子也是x的一个祖先,因此,为了找到这样的后继结点,只需要从x开始沿着树向上移动,直到遇到一个结点,这个结点是它的双亲的左孩子。(例如,在上图的例子中,结点12的后继结点是16.)
给出求后继结点的伪代码:
Tree_Successor(x):
if x.right != NIL
return Tree_MinNode(x.right)
y = x.p
while y!=NIL and x == y.right
x = y
y = y.p
return y
Tree_MinNode(x):
while x.left != NIL
x = x.left
return x
BST插入
BST的插入过程非常简单,很类似与二叉树搜索树的查找过程。当需要插入一个新结点时,从根节点开始,迭代或者递归向下移动,直到遇到一个空的指针NIL,需要插入的值即被存储在该结点位置。这里给出迭代插入算法,递归方式的比较简单。
Tree_Insert(T, z):
y = NIL
x = T.root
while x != NIL
y = x
if z.key < x.key
x = x.left
else x = x.right
z.p = y
if y == NIL
T.root = z
else if z.key < y.key
y.left = z
else y.right = z
下图给出插入结点17的示意图:
同其他搜索树类似,二叉搜索树(BST)的插入操作的时间复杂度为O(h).
BST删除
二叉搜索树的结点删除比插入较为复杂,总体来说,结点的删除可归结为三种情况:
1、 如果结点z没有孩子节点,那么只需简单地将其删除,并修改父节点,用NIL来替换z;
2、 如果结点z只有一个孩子,那么将这个孩子节点提升到z的位置,并修改z的父节点,用z的孩子替换z;
3、 如果结点z有2个孩子,那么查找z的后继y,此外后继一定在z的右子树中,然后让y替换z。
这三种情况中,1和2比较简单,3相对棘手。
我们通过示意图,描述这几种情况:
情况1:
情况2:
情况3:
可分为两种类型,一种是z的后继y位于其右子树中,但没有左孩子,也就是说,右孩子y是其后继。如下:
另外一种类型是,z的后继y位于z的右子树中,但并不是z的右孩子,此时,用y的右孩子替换y,然后再用y替换z。如下:
二叉搜索树的删除:
二叉树的遍历:
最后,我们考虑二叉搜索树的遍历。
二叉搜索树的性质允许通过简单的递归算法来输出树中所有的关键字,有三种方式:先序遍历、中序遍历、后序遍历。其中,先序遍历中输出根的关键字在其左右子树的关键字之前;中序遍历中输出根的关键词位于其左子树的关键字和右子树的关键字之间;后序遍历中输出根的关键字在左右子树的关键字之后。
如果x是一棵有n个结点子树的根,那么调用Preorder_Tree_Walk(x)或者Inorder_Tree_Walk(x)或者Postorder_Tree_Walk(x)需要O(n)时间。
//先序遍历
Preorder_Tree_Walk(x):
if x!=NIL:
print x.key
Preorder_Tree_Walk(x.left)
Preorder_Tree_Walk(x.right)
//中序遍历
Inorder_Tree_Walk(x):
Inorder_Tree_Walk(x.left)
print x.key
Inorder_Tree_Walk(x.right)
//后序遍历
Postorder_Tree_Walk(x):
Postorder_Tree_Walk(x.left)
Postorder_Tree_Walk(x.right)
print x.key
BST的Java实现:
/**
* @author zyj8170 2011-2-13
*
* 此程序实现一个二叉查找树的功能,可以进行动态插入、删除关键字;
* 查询给定关键字、最小关键字、最大关键字;转换为有序列表(用于排序)
*
*
*/
import java.util.ArrayList;
import java.util.List;
public class BinarySearchTree {
// 树的根结点
private TreeNode root = null;
// 遍历结点列表
private List<TreeNode> nodelist = new ArrayList<TreeNode>();
private class TreeNode {
private int key;
private TreeNode leftChild;
private TreeNode rightChild;
private TreeNode parent;
public TreeNode(int key, TreeNode leftChild, TreeNode rightChild,
TreeNode parent) {
this.key = key;
this.leftChild = leftChild;
this.rightChild = rightChild;
this.parent = parent;
}
public int getKey() {
return key;
}
public String toString() {
String leftkey = (leftChild == null ? "" : String
.valueOf(leftChild.key));
String rightkey = (rightChild == null ? "" : String
.valueOf(rightChild.key));
return "(" + leftkey + " , " + key + " , " + rightkey + ")";
}
}
/**
* isEmpty: 判断二叉查找树是否为空;若为空,返回 true ,否则返回 false .
*
*/
public boolean isEmpty() {
if (root == null) {
return true;
} else {
return false;
}
}
/**
* TreeEmpty: 对于某些二叉查找树操作(比如删除关键字)来说,若树为空,则抛出异常。
*/
public void TreeEmpty() throws Exception {
if (isEmpty()) {
throw new Exception("树为空!");
}
}
/**
* search: 在二叉查找树中查询给定关键字
*
* @param key
* 给定关键字
* @return 匹配给定关键字的树结点
*/
public TreeNode search(int key) {
TreeNode pNode = root;
while (pNode != null && pNode.key != key) {
if (key < pNode.key) {
pNode = pNode.leftChild;
} else {
pNode = pNode.rightChild;
}
}
return pNode;
}
/**
* minElemNode: 获取二叉查找树中的最小关键字结点
*
* @return 二叉查找树的最小关键字结点
* @throws Exception
* 若树为空,则抛出异常
*/
public TreeNode minElemNode(TreeNode node) throws Exception {
if (node == null) {
throw new Exception("树为空!");
}
TreeNode pNode = node;
while (pNode.leftChild != null) {
pNode = pNode.leftChild;
}
return pNode;
}
/**
* maxElemNode: 获取二叉查找树中的最大关键字结点
*
* @return 二叉查找树的最大关键字结点
* @throws Exception
* 若树为空,则抛出异常
*/
public TreeNode maxElemNode(TreeNode node) throws Exception {
if (node == null) {
throw new Exception("树为空!");
}
TreeNode pNode = node;
while (pNode.rightChild != null) {
pNode = pNode.rightChild;
}
return pNode;
}
/**
* successor: 获取给定结点在中序遍历顺序下的后继结点
*
* @param node
* 给定树中的结点
* @return 若该结点存在中序遍历顺序下的后继结点,则返回其后继结点;否则返回 null
* @throws Exception
*/
public TreeNode successor(TreeNode node) throws Exception {
if (node == null) {
return null;
}
// 若该结点的右子树不为空,则其后继结点就是右子树中的最小关键字结点
if (node.rightChild != null) {
return minElemNode(node.rightChild);
}
// 若该结点右子树为空
TreeNode parentNode = node.parent;
while (parentNode != null && node == parentNode.rightChild) {
node = parentNode;
parentNode = parentNode.parent;
}
return parentNode;
}
/**
* precessor: 获取给定结点在中序遍历顺序下的前趋结点
*
* @param node
* 给定树中的结点
* @return 若该结点存在中序遍历顺序下的前趋结点,则返回其前趋结点;否则返回 null
* @throws Exception
*/
public TreeNode precessor(TreeNode node) throws Exception {
if (node == null) {
return null;
}
// 若该结点的左子树不为空,则其前趋结点就是左子树中的最大关键字结点
if (node.leftChild != null) {
return maxElemNode(node.leftChild);
}
// 若该结点左子树为空
TreeNode parentNode = node.parent;
while (parentNode != null && node == parentNode.leftChild) {
node = parentNode;
parentNode = parentNode.parent;
}
return parentNode;
}
/**
* insert: 将给定关键字插入到二叉查找树中
*
* @param key
* 给定关键字
*/
public void insert(int key) {
TreeNode parentNode = null;
TreeNode newNode = new TreeNode(key, null, null, null);
TreeNode pNode = root;
if (root == null) {
root = newNode;
return;
}
while (pNode != null) {
parentNode = pNode;
if (key < pNode.key) {
pNode = pNode.leftChild;
} else if (key > pNode.key) {
pNode = pNode.rightChild;
} else {
// 树中已存在匹配给定关键字的结点,则什么都不做直接返回
return;
}
}
if (key < parentNode.key) {
parentNode.leftChild = newNode;
newNode.parent = parentNode;
} else {
parentNode.rightChild = newNode;
newNode.parent = parentNode;
}
}
/**
* insert: 从二叉查找树中删除匹配给定关键字相应的树结点
*
* @param key
* 给定关键字
*/
public void delete(int key) throws Exception {
TreeNode pNode = search(key);
if (pNode == null) {
throw new Exception("树中不存在要删除的关键字!");
}
delete(pNode);
}
/**
* delete: 从二叉查找树中删除给定的结点.
*
* @param pNode
* 要删除的结点
*
* 前置条件: 给定结点在二叉查找树中已经存在
* @throws Exception
*/
private void delete(TreeNode pNode) throws Exception {
if (pNode == null) {
return;
}
if (pNode.leftChild == null && pNode.rightChild == null) { // 该结点既无左孩子结点,也无右孩子结点
TreeNode parentNode = pNode.parent;
if (pNode == parentNode.leftChild) {
parentNode.leftChild = null;
} else {
parentNode.rightChild = null;
}
return;
}
if (pNode.leftChild == null && pNode.rightChild != null) { // 该结点左孩子结点为空,右孩子结点非空
TreeNode parentNode = pNode.parent;
if (pNode == parentNode.leftChild) {
parentNode.leftChild = pNode.rightChild;
pNode.rightChild.parent = parentNode;
} else {
parentNode.rightChild = pNode.rightChild;
pNode.rightChild.parent = parentNode;
}
return;
}
if (pNode.leftChild != null && pNode.rightChild == null) { // 该结点左孩子结点非空,右孩子结点为空
TreeNode parentNode = pNode.parent;
if (pNode == parentNode.leftChild) {
parentNode.leftChild = pNode.leftChild;
pNode.rightChild.parent = parentNode;
} else {
parentNode.rightChild = pNode.leftChild;
pNode.rightChild.parent = parentNode;
}
return;
}
// 该结点左右孩子结点均非空,则删除该结点的后继结点,并用该后继结点取代该结点
TreeNode successorNode = successor(pNode);
delete(successorNode);
pNode.key = successorNode.key;
}
/**
* inOrderTraverseList: 获得二叉查找树的中序遍历结点列表
*
* @return 二叉查找树的中序遍历结点列表
*/
public List<TreeNode> inOrderTraverseList() {
if (nodelist != null) {
nodelist.clear();
}
inOrderTraverse(root);
return nodelist;
}
/**
* inOrderTraverse: 对给定二叉查找树进行中序遍历
*
* @param root
* 给定二叉查找树的根结点
*/
private void inOrderTraverse(TreeNode root) {
if (root != null) {
inOrderTraverse(root.leftChild);
nodelist.add(root);
inOrderTraverse(root.rightChild);
}
}
/**
* toStringOfOrderList: 获取二叉查找树中关键字的有序列表
*
* @return 二叉查找树中关键字的有序列表
*/
public String toStringOfOrderList() {
StringBuilder sbBuilder = new StringBuilder(" [ ");
for (TreeNode p : inOrderTraverseList()) {
sbBuilder.append(p.key);
sbBuilder.append(" ");
}
sbBuilder.append("]");
return sbBuilder.toString();
}
/**
* 获取该二叉查找树的字符串表示
*/
public String toString() {
StringBuilder sbBuilder = new StringBuilder(" [ ");
for (TreeNode p : inOrderTraverseList()) {
sbBuilder.append(p);
sbBuilder.append(" ");
}
sbBuilder.append("]");
return sbBuilder.toString();
}
public TreeNode getRoot() {
return root;
}
public static void testNode(BinarySearchTree bst, TreeNode pNode)
throws Exception {
System.out.println("本结点: " + pNode);
System.out.println("前趋结点: " + bst.precessor(pNode));
System.out.println("后继结点: " + bst.successor(pNode));
}
public static void testTraverse(BinarySearchTree bst) {
System.out.println("二叉树遍历:" + bst);
System.out.println("二叉查找树转换为有序列表: " + bst.toStringOfOrderList());
}
public static void main(String[] args) {
try {
BinarySearchTree bst = new BinarySearchTree();
System.out.println("查找树是否为空? " + (bst.isEmpty() ? "是" : "否"));
int[] keys = new int[] { 15, 6, 18, 3, 7, 13, 20, 2, 9, 4 };
for (int key : keys) {
bst.insert(key);
}
System.out.println("查找树是否为空? " + (bst.isEmpty() ? "是" : "否"));
TreeNode minkeyNode = bst.minElemNode(bst.getRoot());
System.out.println("最小关键字: " + minkeyNode.getKey());
testNode(bst, minkeyNode);
TreeNode maxKeyNode = bst.maxElemNode(bst.getRoot());
System.out.println("最大关键字: " + maxKeyNode.getKey());
testNode(bst, maxKeyNode);
System.out.println("根结点关键字: " + bst.getRoot().getKey());
testNode(bst, bst.getRoot());
testTraverse(bst);
System.out.println("****************************** ");
testTraverse(bst);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
题目:二叉搜索树的第K个节点:
思路:二叉搜索树按照中序遍历的顺序打印出来正好就是排序好的顺序。
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class test {
public class test {
private static class BinaryTreeNode {
private int val;
private BinaryTreeNode left;
private BinaryTreeNode right;
public BinaryTreeNode() {
}
public BinaryTreeNode(int val) {
this.val = val;
}
@Override
public String toString() {
return val + "";
}
}
public static BinaryTreeNode kthNode(BinaryTreeNode root, int k) {
if (root == null || k < 1) {
return null;
}
int[] tmp = {k};
return kthNodeCore(root, tmp);
}
private static BinaryTreeNode kthNodeCore(BinaryTreeNode root, int[] k) {
BinaryTreeNode result = null;
// 先成左子树中找
if (root.left != null) {
result = kthNodeCore(root.left, k);
}
// 如果在左子树中没有找到
if (result == null) {
// 说明当前的根结点是所要找的结点
if(k[0] == 1) {
result = root;
} else {
// 当前的根结点不是要找的结点,但是已经找过了,所以计数器减一
k[0]--;
}
}
// 根结点以及根结点的右子结点都没有找到,则找其右子树
if (result == null && root.right != null) {
result = kthNodeCore(root.right, k);
}
return result;
}
public static void main(String[] args) {
BinaryTreeNode n1 = new BinaryTreeNode(1);
BinaryTreeNode n2 = new BinaryTreeNode(2);
BinaryTreeNode n3 = new BinaryTreeNode(3);
BinaryTreeNode n4 = new BinaryTreeNode(4);
BinaryTreeNode n5 = new BinaryTreeNode(5);
BinaryTreeNode n6 = new BinaryTreeNode(6);
BinaryTreeNode n7 = new BinaryTreeNode(7);
BinaryTreeNode n8 = new BinaryTreeNode(8);
BinaryTreeNode n9 = new BinaryTreeNode(9);
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
n3.left = n6;
n3.right = n7;
n4.left = n8;
n4.right = n9;
print(n1);
System.out.println();
for (int i = 0; i <= 10; i++) {
System.out.printf(kthNode(n1, i) + ", ");
}
}
/**
* 中序遍历一棵树
* @param root
*/
private static void print(BinaryTreeNode root) {
if (root != null) {
print(root.left);
System.out.printf("%-3d", root.val);
print(root.right);
}
}
}
参考:http://wiki.jikexueyuan.com/project/for-offer/question-sixty-three.html
题目:二叉搜索树的后序遍历序列
题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回 true。否则返回 false。假设输入的数组的任意两个数字都互不相同。
解题思路:
在后序遍历得到的序列中, 最后一个数字是树的根结点的值。数组中前面的数字可以分为两部分: 第一部分是左子树结点的值,它们都比根结点的值小: 第二部分是右子树结点的值,它们都比根结点的值大。
public class Test {
/**
* 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。
* 如果是则返回true。否则返回false。假设输入的数组的任意两个数字都互不相同。
*
* @param sequence 某二叉搜索树的后序遍历的结果
* @return true:该数组是某二叉搜索树的后序遍历的结果。false:不是
*/
public static boolean verifySequenceOfBST(int[] sequence) {
// 输入的数组不能为空,并且有数据
if (sequence == null || sequence.length <= 0) {
return false;
}
// 有数据,就调用辅助方法
return verifySequenceOfBST(sequence, 0, sequence.length - 1);
}
/**
* 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。
* 【此方法与上一个方法不同,未进行空值判断,对于数组度为0的情况返回的true也于上题不同,
* 此方法只是上面一个方法的辅助实现,对于数数组为null和数组长度为0的情况,执行结果并非相同】
* 【也就是说此方法只有数组中有数据的情况下才与上面的方法返回同样的结点,
* verifySequenceOfBST(sequence) ===
* verifySequenceOfBST(sequence, 0, sequence.length - 1)
* 当sequence中有数据才成立
* 】
*
* @param sequence 某二叉搜索树的后序遍历的结果
* @param start 处理的开始位置
* @param end 处理的结束位置
* @return true:该数组是某二叉搜索树的后序遍历的结果。false:不是
*/
public static boolean verifySequenceOfBST(int[] sequence, int start, int end) {
// 如果对应要处理的数据只有一个或者已经没有数据要处理(start>end)就返回true
if (start >= end) {
return true;
}
// 从左向右找第一个不大于根结点(sequence[end])的元素的位置
int index = start;
while (index < end - 1 && sequence[index] < sequence[end]) {
index++;
}
// 执行到此处[end, index-1]的元素都是小于根结点的(sequence[end])
// [end, index-1]可以看作是根结点的左子树
// right用于记录第一个不小于根结点的元素的位置
int right = index;
// 接下来要保证[index, end-1]的所有元素都是大于根根点的【A】
// 因为[index, end-1]只有成为根结点的右子树
// 从第一个不小于根结点的元素开始,找第一个不大于根结点的元素
while (index < end - 1 && sequence[index] > sequence[end]) {
index++;
}
// 如果【A】条件满足,那么一定有index=end-1,
// 如果不满足那说明根结点的右子树[index, end-1]中有小于等于根结点的元素,
// 不符合二叉搜索树的定义,返回false
if (index != end - 1) {
return false;
}
// 执行到此处说明直到目前为止,还是合法的
// [start, index-1]为根结点左子树的位置
// [index, end-1]为根结点右子树的位置
index = right;
return verifySequenceOfBST(sequence, start, index - 1) && verifySequenceOfBST(sequence, index, end - 1);
}
public static void main(String[] args) {
// 10
// / \
// 6 14
// /\ /\
// 4 8 12 16
int[] data = {4, 8, 6, 12, 16, 14, 10};
System.out.println("true: " + verifySequenceOfBST(data));
// 5
// / \
// 4 7
// /
// 6
int[] data2 = {4, 6, 7, 5};
System.out.println("true: " + verifySequenceOfBST(data2));
// 5
// /
// 4
// /
// 3
// /
// 2
// /
// 1
int[] data3 = {1, 2, 3, 4, 5};
System.out.println("true: " + verifySequenceOfBST(data3));
// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
int[] data4 = {5, 4, 3, 2, 1};
System.out.println("true: " + verifySequenceOfBST(data4));
// 树中只有1个结点
int[] data5 = {5};
System.out.println("true: " + verifySequenceOfBST(data5));
int[] data6 = {7, 4, 6, 5};
System.out.println("false: " + verifySequenceOfBST(data6));
int[] data7 = {4, 6, 12, 8, 16, 14, 10};
System.out.println("false: " + verifySequenceOfBST(data7));
}
}