Binary Search Trees

Binary Search Trees

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE 3.2 BINARY SEARCH TREES ‣ BSTs ‣ ordered operations Algorithms ‣ deletion FOURTH EDITION ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu 3.2 BINARY SEARCH TREES ‣ BSTs ‣ ordered operations Algorithms ‣ deletion ROBERT SEDGEWICK | KEVIN WAYNE http://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 Binary search tree demo Search. If less, go left; if greater, go right; if equal, search hit. successful search for H S E X A R C H M 4 Binary search tree demo Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R C H G M 5 BST representation in Java Java definition. A BST is 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; Node key private Node left, right; 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 slides */ } public Value get(Key key) { /* see next slides */ } public void delete(Key key) { /* see next slides */ } public Iterable<Key> iterator() { /* see next slides */ } } 7 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 is equal to 1 + depth of node. 8 BST insert Put. Associate value with key. inserting L S E X A R Search for key, then two cases: C H 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 9 BST insert: Java implementation Put. Associate value with key. concise, but tricky, public void put(Key key, Value val) recursive code; read carefully! { 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; } Cost. Number of compares is equal to 1 + depth of node. 10 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 is equal to 1 + depth of node. ・ H E X C S A R A E R X C H A best case typical case S worst case H 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 Bottom Aline. Treeworst shape case depends on order of insertion. C E BST possibilities H 11 R S X BST possibilities BST insertion: random order visualization Ex. Insert keys in random order. 12 Sorting with a binary heap Q. What is this sorting algorithm? 0. Shuffle the array of keys. 1. Insert all keys into a BST. 2. Do an inorder traversal of BST. A. It's not a sorting algorithm (if there are duplicate keys)! Q. OK, so what if there are no duplicate keys? Q. What are its properties? 13 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. 14 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 in random order, expected height of tree is ~ 4.311 ln N. How Tall is a Tree? How Tall is a Tree? 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. 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) [ exponentially small chance when keys are Ainserted binary search tree inis a binaryrandom tree to each ordernode of which ] 1. THE RESULTS Remark After I announced these developedresults, Drmota(unpublished) an alternative proof of the fact that Var(Hn) = A binary search tree is a binary treewe haveto each associated node of awhich key; these keys axe drawn from some O(1) using completely different techniques. As our two totally ordered set and the key developedat v cannot an be alternative larger than proof of the fact that Var(Hn) = we have associated a key; these keys axe drawn from some O(1) using completely different proofstechniques. illuminate As15 differentour two aspects of the problem, we have 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. insert k into T by traversing the tree starting at the root 2. A MODEL and inserting k into the first emptyarrive. position We traverse at which the treewe by moving to the left child of the If we construct a binary search tree from a permutation arrive. We traverse the tree by movingcurrent to thenode left if kchild is smaller of the than the2. currentA MODEL key and moving of 1, ..., n and i is the first key in the permutation then: current node if k is smaller than theto currentthe right key child and movingotherwise. GivenIf we someconstruct permutation a binary of search tree from a permutation i appears at the root of the tree, the tree rooted at the to the right child otherwise. Givena set someof keys, permutation we construct of a binaryof 1, ...,search n and tree i isfrom the thisfirst key in the permutation then: left child of i contains the keys 1, ..., i - 1 and its shape a set of keys, we construct a binarypermutation search tree by insertingfrom this them iin appears the given at orderthe root into of an the tree, the tree rooted at the depends only on the order in which these keys appear in permutation by inserting them ininitially the given empty order tree.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    36 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