Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'explicitly': 0.05; 'ugly': 0.07; 'generators': 0.09; 'def': 0.12; 'jan': 0.12; 'enum': 0.16; 'generator.': 0.16; 'implies': 0.16; 'nodes': 0.16; 'sync': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'converting': 0.30; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; 'node': 0.31; 'skip:= 20': 0.31; 'trivial': 0.31; 'class': 0.32; 'probably': 0.32; 'implemented': 0.33; 'skip:_ 10': 0.34; 'received:google.com': 0.35; 'yield': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'entire': 0.61; 'kind': 0.63; '20,': 0.68; '2015': 0.84; 'hanging': 0.84; 'prone': 0.91 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 :content-type; bh=47g00xH74C3KlZV+pgJpvt+PgDb2uKrTztc/Mdganek=; b=FiTsXu6irmCV/eWL1YDdhHhZvxUQwdw/OR7GYCT7eSQMnFzth3GT8fncMFNt212SXF QL3MW5kqiTUnxGPLp6FQfMar8P/YwPicZ2s987mb68n/ecfrYxPljAF+x1E1zhOTSzrW ZyWFh+CWOIksa740DTfco2OVhA6QmV/aI43lOPvIWgyXOQTWaUB06yqk5bXfggzH8xPP upc99JSe8/IswndKgPyFAPpoB7cFBYAKKh8SVB7n5ok83ttNmpbZ8re1XnSIjBhF+G+8 6HrsN9CkkQR0ywgMUdH7lQW0qAWjRnxhd+fpKHCZW+t4be/90pSb2id7BlbLZjnMBQF1 Ei0g== X-Received: by 10.67.13.12 with SMTP id eu12mr55868175pad.157.1421774420766; Tue, 20 Jan 2015 09:20:20 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Tue, 20 Jan 2015 10:19:40 -0700 Subject: Re: Trees To: Python Content-Type: text/plain; charset=UTF-8 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421774424 news.xs4all.nl 2937 [2001:888:2000:d::a6]:34826 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 4064 X-Received-Body-CRC: 3990259597 Xref: csiph.com comp.lang.python:84074 On Tue, Jan 20, 2015 at 6:33 AM, Rustom Mody wrote: > from enum import Enum > class TreeTag(Enum): > I = 0 # An internal node > L = 1 # A leaf node > def __repr__(self): return self.name > > I = TreeTag.I > L = TreeTag.L Explicitly tagging nodes as internal or leaves is kind of ugly and also prone to getting out of sync with the reality, which is that a node is a leaf if it doesn't have any other nodes hanging off of it. > def dfs(t): > if t[0] == L: > return [t[1]] > else: > return [t[1]] + dfs(t[2]) + dfs(t[3]) Over the entire tree, this will do O(n) list additions which implies O(n) list *copies*, which is rather inefficient. This would probably be better implemented as a recursive generator. def dfs(t): yield t[0] yield from chain.from_iterable(map(dfs, islice(t, 1, None))) > # Converting to generators is trivial > ===================== :-)