Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'third-party': 0.04; 'tree': 0.05; 'suppose': 0.07; 'insertion': 0.09; 'library?': 0.09; 'linear': 0.09; 'subtle': 0.09; 'trees': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'bug': 0.12; 'jan': 0.12; 'question.': 0.14; 'alert': 0.16; 'calendar,': 0.16; 'dict': 0.16; 'mapping,': 0.16; 'sorting': 0.16; 'task:': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.18; 'seems': 0.21; 'example': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'module,': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; "i've": 0.25; 'possibly': 0.26; 'query': 0.26; 'task': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'leave': 0.29; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; '(usually': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'figure': 0.32; 'reader': 0.33; 'maybe': 0.34; 'display': 0.35; 'etc.)': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'combination': 0.36; 'possible': 0.36; 'similar': 0.36; 'should': 0.36; 'list': 0.37; 'implement': 0.38; 'represent': 0.38; 'actions': 0.38; 'pm,': 0.38; 'little': 0.38; 'remove': 0.60; 'event.': 0.60; 'most': 0.60; 'reserved': 0.61; 'new': 0.61; 'strategy': 0.64; 'different': 0.65; 'between': 0.67; '(hint:': 0.84; '2015': 0.84; 'agenda,': 0.84; 'animation,': 0.84; 'balanced': 0.84; 'game,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=8oIvF1HVHnjZ1GeOODNdcZwTfbAPR7AiSzhx+JNSUGk=; b=wWxIsE1k3xYl1M7XAj+9TNwM+p/45DdDo0vDJcMOpmAHoi94E0Yt8ZplRhZ6IvsB5Q K8an7XxWRPq+zjvNgt/wgGN7IQ1tUGLc/HXVf5pd+6PwWJ5peo6FFeQc0CgNBEFhlAq/ G/ANWnZKVId4H0dGHyY/8cVcl2M2hGlSjG0sdqpzFqHuBLmTXTLdY0dbnN3dr01dUUiA UgWOz3VryE0S9EaZYrAlo7qGGiB0KPz0+/YZVeyGe+ajAFLEeOm88C2QTDKqnptdiOMS p6U2Bh90owIRTh4aYbWUFyVrv7Kh+mcdEmR9N3q7AJxYnUe8n+eBL/A1VYqPI7niUDr1 C1aw== X-Received: by 10.224.54.2 with SMTP id o2mr53268174qag.63.1421711601232; Mon, 19 Jan 2015 15:53:21 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <54bd8e6a$0$13009$c3e8da3$5496439d@news.astraweb.com> References: <54bd8e6a$0$13009$c3e8da3$5496439d@news.astraweb.com> From: Devin Jeanpierre Date: Mon, 19 Jan 2015 15:52:41 -0800 Subject: Re: Trees To: "Steven D'Aprano" Content-Type: text/plain; charset=UTF-8 Cc: "comp.lang.python" 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421711603 news.xs4all.nl 2846 [2001:888:2000:d::a6]:39136 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84033 On Mon, Jan 19, 2015 at 3:08 PM, Steven D'Aprano wrote: > Zachary Gilmartin wrote: > >> Why aren't there trees in the python standard library? > > Possibly because they aren't needed? Under what circumstances would you use > a tree instead of a list or a dict or combination of both? > > That's not a rhetorical question. I am genuinely curious, what task do you > have that you think must be solved by a tree? In general, any time you want to maintain a sorted list or mapping, balanced search tree structures come in handy. Here's an example task: suppose you want to represent a calendar, where timeslots can be reserved for something. Calendar events are not allowed to intersect. The most important query is: What events are there that intersect with the timespan between datetimes d1 and d2? (To draw a daily agenda, figure out if you should display an alert to the user that an event is ongoing or imminent, etc.) You also want to be able to add a new event to the calendar, that takes place between d1 and d2, and to remove a event. I leave it to the reader to implement this using a sorted map. (hint: sort by start.) This maybe seems contrived, but I've used this exact datatype, or a remarkably similar one, in a few different circumstances: sequenced actions of characters in a strategy game, animation, motion planning... There are a few possible implementations using Python data structures. You can do it using a linear scan, which gets a little slow pretty quickly. You can make insertion slow (usually OK) by sorting on insertion, but if you ever forget to resort your list you will get a subtle bug you might not notice for a while. And so on. It's better in every way to use the third-party blist module, so why bother? -- Devin