1. Binary Search Tree:
a) Definition : A BST is a binary tree in symmetric order.
A binary tree is either:
-- Empty.
-- Two disjoint binary trees (left and right).
b) Symmetric order: Each node has a key, and every node’s key is:
-- Larger than all keys in its left subtree.
-- Smaller than all keys in its right subtree.
2. Java definition: A BST is a reference to a root Node.
private class Node { private Key key; private Value val; private Node left, right; public Node(Key key, Value val) { this.key = key; this.val = val; } }
3. Search:
a) If less, go left; if greater, go right; if equal, search hit.
b) Cost : Number of compares is equal to 1 + depth of node.
c) Java Implementation :
public Value get(Key key) { Node x = root; while (x != null) { int cmp = key.compareTo(x.key); if (cmp < 0) x = x.left; else if (cmp > 0) x = x.right; else if (cmp == 0) return x.val; } return null; }
4. Put:
a) If less, go left; if greater, go right; if equal, update value; if null, insert
b) Cost : Number of compares is equal to 1 + depth of node.
c) Java Implementation :
public void put(Key key, Value val) { root = put(root, key, val); } private Node put(Node x, Key key, Value val) { if (x == null) return new Node(key, val); int cmp = key.compareTo(x.key); if (cmp < 0) x.left = put(x.left, key, val); else if (cmp > 0) x.right = put(x.right, key, val); else if (cmp == 0) x.val = val; return x; }
5. Tree shape :
a) Many BSTs correspond to same set of keys.
b) Tree shape depends on order of insertion.
6. Correspondence between BSTs and quicksort partitioning : Correspondence is 1-1 if array has no duplicate keys.
7. Mathematical Analysis :
a) If N distinct keys are inserted into a BST in random order, the expected number of compares for a search/insert is ~ 2 ln N. (1-1 correspondence with quicksort partitioning.)
b) If N distinct keys are inserted in random order, expected height of tree ( the longest path) is ~ 4.311 ln N.
c) Worst-case height is N when keys are inserted in ascending order.
8. Ordered Operations:
a) Minimum : most left key
b) Maximum : most right key
c) Floor : Largest key ≤ a given key k:
1) k equals the key at root -- The floor of k is k.
2) k is less than the key at root -- The floor of k is in the left subtree.
3) k is greater than the key at root -- The floor of k is in the right subtree (if there is any key ≤ k in right subtree); otherwise it is the key in the root.
4) Java Implementation :
public Key floor(Key key) { Node x = floor(root, key); if (x == null) return null; return x.key; } private Node floor(Node x, Key key) { if (x == null) return null; int cmp = key.compareTo(x.key); if (cmp == 0) return x; if (cmp < 0) return floor(x.left, key); Node t = floor(x.right, key); if (t != null) return t; else return x; }
d) Ceiling : Smallest key ≥ a given key k. ( similar to Floor )
e) Size of Subtree : In each node, we store the number of nodes in the subtree rooted at that
node:
private class Node { private Key key; private Value val; private Node left; private Node right; private int count; } public int size() { return size(root); } private int size(Node x) { if (x == null) return 0; return x.count; } private Node put(Node x, Key key, Value val) { if (x == null) return new Node(key, val); int cmp = key.compareTo(x.key); if (cmp < 0) x.left = put(x.left, key, val); else if (cmp > 0) x.right = put(x.right, key, val); else if (cmp == 0) x.val = val; x.count = 1 + size(x.left) + size(x.right); return x; }
f) Rank. How many keys < k
public int rank(Key key) { return rank(key, root); } private int rank(Key key, Node x) { if (x == null) return 0; int cmp = key.compareTo(x.key); if (cmp < 0) return rank(key, x.left); else if (cmp > 0) return 1 + size(x.left) + rank(key, x.right); else if (cmp == 0) return size(x.left); }
9. Inorder Tranversal
a) traverse left subtree; enqueue key; traverse right subtree.
b) Inorder traversal of a BST yields keys in ascending order.
c) Java Implementation :
public Iterable<Key> keys() { Queue<Key> q = new Queue<Key>(); inorder(root, q); return q; } private void inorder(Node x, Queue<Key> q) { if (x == null) return; inorder(x.left, q); q.enqueue(x.key); inorder(x.right, q); }
10. Deletion:
a) lazy approach :
1) Set the value of node to be deleted to null.
2) Leave key in tree to guide searches (but don't consider it equal in search).
3) Cost : ~ 2 ln N' per insert, search, and delete (if keys in random order), where N' is the number of key-value pairs ever inserted in the BST.
4) Unsatisfactory solution: Tombstone (memory) overload.
b) Deleting Minimum:
1) Go left until finding a node with a null left link.
2) Replace that node by its right link.
3) Update subtree counts.
4) Java Implementation :
public void deleteMin() { root = deleteMin(root); } private Node deleteMin(Node x) { if (x.left == null) return x.right; x.left = deleteMin(x.left); x.count = 1 + size(x.left) + size(x.right); return x; }
c) Hibbard Deletion :
1) [0 children] Delete t by setting parent link to null.
2) [1 child] Delete t by replacing parent link.
3) [2 children] Find successor x of t. Delete the minimum x in t's right subtree.Put x in t's spot.
4) Java Implementation:
public void delete(Key key) { root = delete(root, key); } private Node delete(Node x, Key key) { if (x == null) return null; int cmp = key.compareTo(x.key); if (cmp < 0) x.left = delete(x.left, key); else if (cmp > 0) x.right = delete(x.right, key); else { if (x.right == null) return x.left; if (x.left == null) return x.right; Node t = x; x = min(t.right); x.right = deleteMin(t.right); x.left = t.left; } x.count = size(x.left) + size(x.right) + 1; return x; }
5) Unsatisfactory solution: Not symmetric. Trees not random (!) ⇒ sqrt (N) per op.
相关推荐
1975年,来自斯坦福大学的Jon Louis Bentley在ACM杂志上发表的一篇论文:Multidimensional Binary Search Trees Used for Associative Searching 中正式提出和阐述的了把空间划分为多个部分的k-d树。
Self-Adjusting Binary Search TreesDANIEL DOMINIC SLEATOR A N D ROBERT ENDRE TARJANA T&T Bell Laboratories, Murray Hill, NJAbstract. The splay tree, a self-adjusting form of binary search tree, is ...
在Knuth的经典论文《Optimum Binary Search Trees》中,他首先回顾了二叉查找树的基本概念及工作原理,即通过比较节点值来决定搜索方向,从而实现快速查找。具体来说,当需要在一个二叉查找树中查找某个元素时,可以...
Both the left and right subtrees must also be binary search trees. A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled ...
此外,二叉搜索树(Binary Search Trees,BSTs)是二叉树的一个特例,其中每个节点的左子树只包含比其小的元素,右子树只包含比其大的元素,这使得查找、插入和删除操作的时间复杂度可以达到对数级别。 在本章的...
二元搜索树(Binary Search Tree, BST)是一种特殊的二元树,其中每个节点的值大于其左子树中的所有节点值,小于其右子树中的所有节点值。BST支持高效的查找、插入和删除操作,时间复杂度可达到O(log n)。 在`...
Static and dynamic weighted binary search trees (Section 10.1) AVL-trees (Section 10.2) Red-black trees (Section 10.3) Splay trees (Section 10.4) B-, B+ and B*-trees (Sections 11.1-11.3) Tries ...
Both the left and right subtrees must also be binary search trees. A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled ...
Chapter 14 Binary Search Trees Chapter 15 Balanced Search Trees Chapter 16 Graphs Part III Algorithm Design Methods Chapter 17 The Greedy Method Chapter 18 Divide and Conquer Chapter 19 Dynamic ...
第三部分(Part III) 数据结构(Data Structures) 第十章 基本的数据结构(Elementary Data Structures) 第十一章 散列表(Hash Tables) 第十二章 二叉查找树(Binary Search Trees) 第十三章 红-黑树(Red-...
在本项目"CatCafe"中,我们关注的是利用Java编程语言实现一个基于二叉搜索树(Binary Search Tree, BST)的数据结构,更具体地说,是三叉树(Ternary Search Tree, TST),来存储和管理猫的数据库。这个数据库采用...
The tree.hh library for C++ provides an STL-like ... If you are only interested in AVL binary search trees (Adelson,Velskii & Landis), you may want to have a look at the C++ AVL tree template page.
binary search trees including treaps, scapegoat trees, and red-black trees; integer searching structures including binary tries, x-fast tries, and y-fast tries; heaps, including implicit binary heaps...
l2.4 Randoinly built binary search trees 265 13 Red-Black Thees 273 l3.l Properties of red-black trees 273 l3.2 Rotations 277 l3.3 Insertion 280 l3.4 Deletion 288 14 Augmenting Data Structures 302 l4....
python python_leetcode题解之096_Unique_Binary_Search_Trees