Bsts ‣ Iteration Algorithms ‣ Ordered Operations FOURTH EDITION ‣ Deletion (See Book Or Videos)

Bsts ‣ Iteration Algorithms ‣ Ordered Operations FOURTH EDITION ‣ Deletion (See Book Or Videos)

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE 3.2 BINARY SEARCH TREES ‣ BSTs ‣ iteration Algorithms ‣ ordered operations FOURTH EDITION ‣ deletion (see book or videos) ROBERT SEDGEWICK | KEVIN WAYNE https://algs4.cs.princeton.edu Last updated on 3/1/19 3:45 PM 3.2 BINARY SEARCH TREES ‣ BSTs ‣ iteration Algorithms ‣ ordered operations ‣ deletion (see book or videos) ROBERT SEDGEWICK | KEVIN WAYNE https://algs4.cs.princeton.edu Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link a subtree A binary tree is either: Empty. ・ right child ・Two disjoint binary trees (left and right). of root null links Anatomy of a binary tree parent of A and R Symmetric order. Each node has a key, key left link S and every node’s key is: of E E X Larger than all keys in its left subtree. A R 9 value ・ C H associated ・Smaller than all keys in its right subtree. with R keys smaller than E keys larger than E Anatomy of a binary search tree 3 Differences between heaps and binary search trees Heap BST Supported insert, search, delete, Insert, delete-max operations ordered operations What is inserted Keys Key-value pairs Underlying Resizing array Linked nodes data structure Fixed shape given n Varies; Tree shape (complete binary tree) depends on data Somewhat ordered Totally ordered Ordering of keys parent > child for max heap left child < parent < right child Duplicate keys Yes No allowed? 4 Binary search trees: quiz 1 Which of the following properties hold? A. If a binary tree is heap ordered, then it is symmetrically ordered. B. If a binary tree is symmetrically ordered, then it is heap ordered. C. Both A and B. D. Neither A nor B. max key X X min key H H max key E E S S C C S S A C A R C HR H A E A R E XR X symmetricsymmetric order order heapheap order orderheap order symmetric order 5 BST representation in Java A BST contains a reference to a root Node. A Node is composed of four fields: ・A Key and a Value. ・A reference to the left and right subtree. smaller keys larger keys private class Node { private Key key; BST private Value val; private Node left, right; Node key val public Node(Key key, Value val) { left right this.key = key; this.val = val; } BST with smaller keys BST with larger keys } Binary search tree Key and Value are generic types; Key is Comparable 6 BST implementation (skeleton) public class BST<Key extends Comparable<Key>, Value> { private Node root; root of BST private class Node { /* see previous slide */ } public void put(Key key, Value val) { /* see next slide */ } public Value get(Key key) { /* see next slide */ } public Iterable<Key> keys() { /* see slides in next section */ } public void delete(Key key) { /* see textbook */ } } 7 BST search (get) If less, go left; if greater, go right; if equal, search hit; if null node, search miss. H: search hit S E X A R G: search miss S C H E X M A R C H M 8 BST search: Java implementation Get. Return value corresponding to given key, or null if no such key. 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; } Cost. Number of compares = 1 + depth of node. 9 BST insert (put) Associate value with key. inserting L S E X A R C H Search for key, then two cases: M Key in tree ⇒ reset value. search for L ends P ・ at this null link Key not in tree ⇒ add new node. S ・ E X A R C H M create new node L P S E X A R C H M reset links L P on the way up Insertion into a BST 10 BST insert: Java implementation Put. Associate value with key. inserting L Tr i c k y ! S E X A R public void put(Key key, Value val) C H { root = put(root, key, val); } M P private Node put(Node x, Key key, Value val) search for L ends at this null link { S if (x == null) return new Node(key, val); E X int cmp = key.compareTo(x.key); A R if (cmp < 0) x.left = put(x.left, key, val); C H else if (cmp > 0) x.right = put(x.right, key, val); M else if (cmp == 0) x.val = val; create new node L P return x; S } E X A R C H M reset links L P on the way up Cost. Number of compares = 1 + depth of node. Insertion into a BST 11 best case H C S Tree shape A E R X Many BSTs correspond to same set of keys. ・ typical case S Number of compares for bestsearch/insert case = 1 + depth of node. ・ H E X C S A R A E R X C H best case typical case A worst case H S C C S E X E A E R X A R H C H R S X typical case S A worst case E X C BST possibilities A R E C H H R S X BottomA line. worstTree case shape depends on order of insertion. C E BST possibilities H 12 R S X BST possibilities BST insertion: random order visualization Ex. Insert keys in random order. 13 Binary search trees: quiz 2 Suppose that you insert n keys in random order into a BST. What is the expected height of the resulting BST? P H T A. ~ lg n D O S U B. ~ ln n height A E I Y C. ~ 2 lg n D. ~ 2 ln n C M E. ~ 4.31107 ln n L 14 Correspondence between BSTs and quicksort partitioning P H T D O S U A E I Y C M L Remark. Correspondence is 1–1 if array has no duplicate keys. 15 BSTs: mathematical analysis Proposition. 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. Pf. 1–1 correspondence with quicksort partitioning. Proposition. [Reed, 2003] If n distinct keys are inserted into a BST in random order, the expected height is ~ 4.31107 ln n. How Tall is a Tree? expected depth of How Tall is a Tree? function-call stack in quicksort Bruce Reed Bruce Reed CNRS, Paris, France CNRS, Paris, France [email protected] [email protected] ABSTRACT purpose of this note to prove that for /3 -- ½ + ~,3 we ABSTRACT Let H~ be the height of a randompurpose binary of search this note tree toon prove n thathave: for /3 -- ½ + ~,3 we Let H~ be the height of a randomnodes. binary We search show thattree onthere n existshave: constants a = 4.31107... THEOREM 1. E(H~) = ~logn -/31oglogn + O(1) nodes. We show that there existsand/3 constants = 1.95... a = 4.31107... such that E(H~) = c~logn -/31oglogn + and Var(Hn) = O(1) . and/3 = 1.95... such that E(H~)O(1), = c~logn We also -/31oglogn show that +Var(H~) =THEOREM O(1). 1. E(H~) = ~logn -/31oglogn + O(1) and O(1), We also show that Var(H~) = O(1). Var(Hn) = O(1) . Remark By the definition of a, /3 = 3~ Categories and Subject Descriptors 7"g~" The first defi- Remark By the definition of a, nition/3 = 7"g~"given3~ Theis more first suggestive defi- of why this value is correct, Categories and Subject Descriptors as we will see. But… Worst-case height is n – 1. E.2 [Data Structures]: Trees nition given is more suggestive of why this value is correct, E.2 [Data Structures]: Trees as we will see. For more information on random binary search trees, one 1. THE RESULTS For more information on randommay binary consult search [6],[7], trees, [1], one [2], [9], [4], and [8]. may consult [6],[7], [1], [2], [9], [4],Remark and [8]. After I announced these results, Drmota(unpublished) Unlike quicksort, worst case matters —1. clientTHE RESULTS may notA binary insertsearch tree is a inbinary random tree to each node oforder. which developed an alternative proof of the fact that Var(Hn) = we have associated a key; these Remarkkeys axe drawnAfter I fromannounced some these results, Drmota(unpublished) A binary search tree is a binary tree to each node of which developed an alternative proof ofO(1) the factusing that completely Var(Hn) different= techniques. As our two totally ordered set and the key at v cannot be larger than proofs illuminate different aspects of the problem, we have we have associated a key; these keys axe drawn from some O(1) using completely different techniques. 16 As our two totally ordered set and the key atthe v keycannot at its be right larger child than nor smaller than the key at its left decided to submit the journal versions to the same journal child. Given a binary search treeproofs T and illuminate a new key different k, we aspects of the problem, we have the key at its right child nor smaller than the key at its left decided to submit the journal versionsand asked to the that same they journal be published side by side. child. Given a binary search treeinsert T and k intoa new T bykey traversing k, we the tree starting at the root and inserting k into the first emptyand asked position that at they which be publishedwe side by side.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    33 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us