`
leonzhx
  • 浏览: 768272 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

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.

分享到:
评论

相关推荐

    Multidimensional Binary Search Trees Used for Associative Searching

    1975年,来自斯坦福大学的Jon Louis Bentley在ACM杂志上发表的一篇论文:Multidimensional Binary Search Trees Used for Associative Searching 中正式提出和阐述的了把空间划分为多个部分的k-d树。

    Self-Adjusting Binary Search Trees (1985)-计算机科学

    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 ...

    陈越、何钦铭-数据结构作业16:Complete Binary Search Tree完全二叉搜索树

    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 ...

    CatCafe:我正在进行的一个项目是使用Java Binary Search Trees,递归和Tree Traversal提出由三叉树CatTree代表的猫的数据库

    CatCafe:我正在进行的一个项目是使用Java Binary Search Trees,递归和Tree Traversal提出由三叉树CatTree代表的猫的数据库

    高级数据结构PPT 英文版

    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 ...

    CBST.rar_The Keys_bst

    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 ...

    Data Structures, Algorithms and Applications in C++ Second Edition

    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 ...

    算法分析与设计课件01

    第三部分(Part III) 数据结构(Data Structures) 第十章 基本的数据结构(Elementary Data Structures) 第十一章 散列表(Hash Tables) 第十二章 二叉查找树(Binary Search Trees) 第十三章 红-黑树(Red-...

    算法分析与设计课件02

    第三部分(Part III) 数据结构(Data Structures) 第十章 基本的数据结构(Elementary Data Structures) 第十一章 散列表(Hash Tables) 第十二章 二叉查找树(Binary Search Trees) 第十三章 红-黑树(Red-...

    算法分析与设计课件03

    第三部分(Part III) 数据结构(Data Structures) 第十章 基本的数据结构(Elementary Data Structures) 第十一章 散列表(Hash Tables) 第十二章 二叉查找树(Binary Search Trees) 第十三章 红-黑树(Red-...

    算法导论_英文第三版

    12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3 Insertion 315 13.4 Deletion 323 14 Augmenting Data Structures 339 ...

    算法导论 答案 (英文版)

    Chapter 12: Binary Search Trees Lecture Notes 12-1 Solutions 12-12 Chapter 13: Red-Black Trees Lecture Notes 13-1 Solutions 13-13 Chapter 14: Augmenting Data Structures Lecture Notes 14-1 Solutions 14...

    Introduction to Algorithms, 3rd edtion

    12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3 Insertion 315 13.4 Deletion 323 14 Augmenting Data Structures 339 14.1...

    STL 风格的 N叉树

    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.

    Data Structures & Algorithms in Swift 4th Edition

    * Trees: Learn how to work with various types of trees, including general purpose trees, binary trees, AVL trees, binary search trees, and tries. * Sorting: Go beyond bubble and insertion sort with ...

    cpp-算法精粹

    Unique Binary Search Trees II Validate Binary Search Tree Convert Sorted Array to Binary Search Tree Convert Sorted List to Binary Search Tree LCA of BST Kth Smallest Element in a BST 二叉树的递归 ...

    Open Data Structures (in Java) Pat Morin

    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...

    Data.Structures.and.Algorithms.with.Python

    Chapter 10 Balanced Binary Search Trees Chapter 11 B-Trees Chapter 12 Heuristic Search Appendix A: Integer Operators Appendix B: Float Operators Appendix C: String Operators and Methods Appendix D: ...

    算法导论 第三版 英文原版 高清文字版

    12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3 Insertion 315 13.4 Deletion 323 14 Augmenting Data Structures 339 ...

Global site tag (gtag.js) - Google Analytics