MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: LeafCount

  • To: mathgroup at smc.vnet.net
  • Subject: [mg25949] Re: LeafCount
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Tue, 14 Nov 2000 03:46:43 -0500 (EST)
  • Organization: Universitaet Leipzig
  • References: <8ug9h6$idn@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

your expression is

+-----+-----------+
|     |           | 
Plus  x           |
           +------+-----+
           |      |     |
         Times    y     z


and I can't help myself -- I see five leafs not three

You don't count the head's of an expression but expr[[0]] 
is not different from expr[[1]].

Regards
  Jens

Will Self wrote:
> 
> In the expression Plus[x, Times[y, z]] I would say that there are 3
> terminal nodes (leaves), x, y, and z.  The built-in function LeafCount,
> which purports to count the number of terminal nodes in the expression
> tree, actually counts *all* the nodes.  So
> LeafCount[Plus[x, Times[y, z]]]  --->  5
> Am I missing something here, or is it true that LeafCount is wrongly
> described in the Mathematica Book?
> 
> Here is mine:
> 
> myLeafCount[expr_]:=
>   Length[Flatten[Hold[expr] /. x_ :> List /; Head[x] == Symbol]]
> 
> Does anyone see anything wrong with this?  Or have another suggestion?
> 
> Will Self
> private email replies appreciated
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.


  • Prev by Date: Problem with Integrate
  • Next by Date: Re: Recursion
  • Previous by thread: LeafCount
  • Next by thread: Re: LeafCount