Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'tree': 0.05; 'algorithms,': 0.07; 'amounts': 0.07; 'great.': 0.07; 'builtin': 0.09; 'essential.': 0.09; 'insertion': 0.09; 'processing,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '"small"': 0.16; 'algorithm.': 0.16; 'cc:name:python list': 0.16; 'collections': 0.16; 'ordereddict': 0.16; 'rarely': 0.16; 'sorting': 0.16; 'tends': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'language': 0.16; 'wrote:': 0.18; "python's": 0.19; 'things.': 0.19; 'thu,': 0.19; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'order.': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'needed.': 0.30; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; '13,': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'cases': 0.33; 'table': 0.34; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; '+0200,': 0.36; 'acceptable': 0.36; 'data,': 0.36; 'ordered': 0.36; 'tight': 0.36; 'yield': 0.36; 'level': 0.37; 'implement': 0.38; 'awesome': 0.38; 'whatever': 0.38; 'fact': 0.38; 'pm,': 0.38; 'rather': 0.38; 'itself': 0.39; 'how': 0.40; 'even': 0.60; 'matter': 0.61; "you're": 0.61; 'high': 0.63; 'mar': 0.68; 'results': 0.69; 'superior': 0.69; 'million': 0.74; 'cream': 0.84; 'fast,': 0.84; 'items,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=BzpHwJX4OKzjGBYkZ1m3K/lA2fmSNwZViFZlNp1VaRo=; b=TMBFlEwFBxahH30IX9BT94dnqbzRvNB2ta78uw4wx8HBGbnw3w6C1xUD8p05y1JcS6 ATX1JWKHiZwi7eIVdtjnCrw00M1pTAICloGQWgihGD8lHVN/daHZhPyVNXMMawAPPqiS btiu+Pj1liGPYVJNnTBAdU9e3bhvpfHsno6jhEQi+WZhfaHFNMzaOnmMwiniKfGzIkQ6 IqWGgkh+SNThLBEanjCH7/5txhxEuvH4cWcPCxtfeTEPWdYUodETkKVU5AEBxqfg1Qwf N1raHJou8EwBtjyxIzAbChZqsw9vpfSushEoUym7TPUGchPtzyptAj31v4FGEx2Fnp7T OtWw== MIME-Version: 1.0 X-Received: by 10.236.170.137 with SMTP id p9mr7256960yhl.105.1394766762031; Thu, 13 Mar 2014 20:12:42 -0700 (PDT) In-Reply-To: <532245fa$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <87eh2d3x8h.fsf_-_@elektro.pacujo.net> <87eh2ctmht.fsf@elektro.pacujo.net> <87zjkz3eo3.fsf@elektro.pacujo.net> <87a9czrrax.fsf@elektro.pacujo.net> <531d3058$0$29994$c3e8da3$5496439d@news.astraweb.com> <87eh2atw6s.fsf@elektro.pacujo.net> <8761nmrnfk.fsf@elektro.pacujo.net> <87zjkyq7nr.fsf@elektro.pacujo.net> <532245fa$0$29994$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 13 Mar 2014 20:12:41 -0700 Subject: Re: Balanced trees From: Dan Stromberg To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394766770 news.xs4all.nl 2857 [2001:888:2000:d::a6]:49416 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68354 On Thu, Mar 13, 2014 at 4:57 PM, Steven D'Aprano wrote: > On Mon, 10 Mar 2014 19:34:48 +0200, Marko Rauhamaa wrote: > >>> With a high level language like Python, using the provided hash table >>> will almost always cream any hand-built tree, no matter how >>> advantageous the data is to the tree. >> >> The main thing is there are use cases where order is essential. That's >> why I have had to implement the AVL tree in Python myself. > > from collections import OrderedDict > > gives you a fast, ordered mapping. In this case, the order is that of > insertion order. If you want sort order instead, for "small" amounts of > data, say below a million or ten million items, you're likely to have > acceptable if not superior results by just sorting them items when and as > needed. This is one of my pet things. Sorting the same (slightly tweaked) data inside of a tight loop is rarely a good idea - despite the fact that the "sort" itself tends to be O(n) with Python's rather awesome builtin sorting algorithm. This is because sorting inside a loop tends to yield O(n^2) algorithms, or even O((n^2)*logn). But if you're sorting once at the end of whatever other processing, sorting is great. It's (still) O(nlogn), but it's got a terrific constant.