home *** CD-ROM | disk | FTP | other *** search
- Organization: Applications Software, Carnegie Mellon, Pittsburgh, PA
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu!fs7.ece.cmu.edu!crabapple.srv.cs.cmu.edu!andrew.cmu.edu!rr2p+
- Newsgroups: comp.lang.c
- Message-ID: <UezhlN_00VQs4rjqFN@andrew.cmu.edu>
- Date: Mon, 9 Nov 1992 16:47:05 -0500
- From: Richard Dale Romero <rickr+@CMU.EDU>
- Subject: Re: Printing out trees
- In-Reply-To: <BxG3py.LoA@cck.coventry.ac.uk>
- References: <gezTJc_00VQsAo9Vpl@andrew.cmu.edu>
- <BxG3py.LoA@cck.coventry.ac.uk>
- Lines: 41
-
- No, that's not really what I meant--I meant in a formatted way
- so that I could tell what the child-parent relations were...
-
- I did think of an acceptable way, if anyone cares to know.
- You need to have a queue implementation also, which
- isn't hard to do, though, and if you try to queue a null
- node, it just doesn't queue anything.
-
- Step 1) Assign every node it's in-order numbering,
- node->number, and a depth variable, node->depth
- This is the one where you number your left
- children, number yourself, then number
- your right children (at least I think this
- is the in-order traversal :-)
-
- Step 2) Queue the root of the tree
-
- Step 3) Start while loop that stops when queue is empty
-
- Step 4) De-queue top element, and if this element is at a
- different depth than the previous one, print
- newline, and set cur_x = 0
-
- Step 5) Queue it's left child, then right child
-
- Step 6) Space over (cur_node->number - cur_x) "blocks"
-
- Step 7) Print current node value, then set
- cur_x = cur_node->number + 1
-
- Step 8) End of while loop.
-
- At least, I think that's what my code does....
-
- Regardless it will print out a node such that its direct children
- are on the line directly below it, *every* item in the left
- sub-tree appears to the left of the current node, and *every* item
- in the right sub-tree appears to the right of the current node.
-
- -rick
-
-