Re: to merge two binary tree
- From: "Douglas A. Gwyn" <DAGwyn@xxxxxxxx>
- Date: 02 Apr 2006 09:43:22 GMT
sudharsan wrote:
cud you give me logic to merge two sorted binary trees.I mean which
node has to be taken as root node in the new tree ...etc
This isn't a C programming question as such.
Surely there must be an algorithm in Knuth.
The root node of the merge will in general
not be either of the original root nodes.
If you want to develop your own algorithm,
the easy way is to just start with a new
empty tree and traverse each of the input
trees, inserting (moving) each input node
into the new tree being formed, according
to a usual binary-tree insertion algorithm.
Presumably that isn't as efficient as an
optimized algorithm, but unless your app
calls for a lot of tree merging it should
suffice. (An optimal algorithm would
presumably just insert the root node of
the second input at the appropriate point
in the first (binary tree insertion) and
then perform a tree "rebalancing" action.)
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- Prev by Date: Re: C and assembly mixed listing
- Next by Date: Re: My 'switch' isn't working properly. Why?
- Previous by thread: Re: How do I print colums?
- Next by thread: Re: to merge two binary tree
- Index(es):
Relevant Pages
|