Binary search tree(二叉查找树)(一)

2014-11-24 03:21:37 · 作者: · 浏览: 5

The search tree data structure supports many dynamic-set operations, includingSEARCH,MINIMUM,MAXIMUM,PREDECESSOR,SUCCESSOR,INSERT, andDELETE. Thus, we can use a search tree both as a dictionary and as a priority queue.


1.Definition:

a binary search tree (BST), sometimes also called anordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:

    The left subtree of a node contains only nodes with keys less than the node's key.The right subtree of a node contains only nodes with keys greater than the node's key.The left and right subtree each must also be a binary search tree.There must be no duplicate nodes. Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records.
    The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.
    Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets, and associative arrays.

    Binary-search-tree property: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then y.key < x.key. If y is a node in the right subtree of x, then y.key > x.key.


    2. operations


    2.1 Traversal(遍历)

    遍历分为三种,分别是 inorder tree walk,print out all the keys in a binary search tree in sorted order by a simple recursive algorithm. preorder tree walk ,prints the root before the values in either subtree. postorder tree walk ,print the root after the values in its subtrees. inorder tree walk的伪代码: \

    定理: \

    2.2 Querying a binary search tree

    We often need to search for a key stZ http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcmVkIGluIGEgYmluYXJ5IHNlYXJjaCB0cmVlLiBCZXNpZGVzIHRoZTxlbT5TRUFSQ0g8L2VtPm9wZXJhdGlvbiwgYmluYXJ5IHNlYXJjaCB0cmVlcyBjYW4gc3VwcG9ydCBzdWNoIHF1ZXJpZXMgYXM8ZW0+TUlOSU1VTTwvZW0+LDxlbT5NQVhJTVVNPC9lbT4sPGVtPlNVQ0NFU1NPUjwvZW0+LCBhbmQ8ZW0+UFJFREVDRVNTT1I8L2VtPi4KIEluIHRoaXMgc2VjdGlvbiwgd2Ugc2hhbGwgZXhhbWluZSB0aGVzZSBvcGVyYXRpb25zIGFuZCBzaG93IGhvdyB0byBzdXBwb3J0IGVhY2ggb25lIGluIHRpbWUgTyhoKSBvbiBhbnkgYmluYXJ5IHNlYXJjaCB0cmVlIG9mIGhlaWdodCBoLjxicj4KCjxicj4KCjxoMz4yLjIuMSBTZWFyY2g8L2gzPgpXZSB1c2UgdGhlIGZvbGxvd2luZyBwcm9jZWR1cmUgdG8gc2VhcmNoIGZvciBhIG5vZGUgd2l0aCBhIGdpdmVuIGtleSBpbiBhIGJpbmFyeSBzZWFyY2ggdHJlZS4gR2l2ZW4gYSBwb2ludGVyIHRvIHRoZSByb290IG9mIHRoZSB0cmVlIGFuZCBhIGtleSBrLCBUUkVFLVNFQVJDSCByZXR1cm5zIGEgcG9pbnRlciB0byBhIG5vZGUgd2l0aCBrZXkgayBpZiBvbmUgZXhpc3RzOyBvdGhlcndpc2UsCiBpdCByZXR1cm5zIE5JTC48YnI+Cgo8YnI+CgrU2tLUeM6quPm92rXjtcTK99bQy9HL92u1xM6xtPrC66O6CjxpbWcgc3JjPQ=="https://www.cppentry.com/upload_files/article/76/1_bje9o__.jpg" alt="\">

    上面的代码是以递归法写的,我们也可以利用一个while语句以迭代的方式实现,而且在大多数计算机中迭代的形式要比递归效率更高。 迭代伪代码: \

    2.2.2 Minimum and Maximum

    寻找最小数x伪代码:

    \


    找最大数x伪代码: \

    2.2.3 Successor and predecessor

    寻找x元素后继的伪代码: \
    伪代码中可以分为两个部分,1-2和3-7两部分。
    第一部分表示:如果x存在右孩子,那么右边最小的那个肯定是x的后继(如下图中15的后继是17)
    第二部分表示:如果x不存在右孩子,那么寻找他的后继就必须从父辈中找,此时又分为两情况。 1.当x为其parent的左孩子时,则后继即为x.p. while条件(x==y.right)就是判断这个,此时不进入循环,此时的情况如2的后继是3. 2.当x为其parent的右孩子时,则进入循环,多次循环找到后继,此时的情况如13的后继是15. (注意while判断里面的y不等NIL是防止x为根,假如x为树根且不存在右孩子的话得出的结果是y==NIL) \

    最后得出的定理是: We can implement the dynami