473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Algorithm problem

I have an algorithm question and don't know how to do it nicely.
Write a function to print nodes of binary tree, for each leaf node, print nodes from root node to leaf node.

For example, the following binary tree has 4 leaf nodes.
30
25 36
39 40 10 64

The resulting of print should be :
30, 25, 39
30, 25, 40
30, 36, 10
30, 36, 64
Nov 16 '05 #1
3 1642
Are the nodes supposed to be sorted? Or what order do you need them printed?
Usually going through a tree is a recursive procedure, and a node is only
accessed (i.e. printed) once. Why do you keep printing 30? A node that is a
leaf to the level above is a root to the level below. Here is a simple
algorithm.

curr = head;
printFunc( curr);

printFunc( root)
{
if root.left
printFunc(root.left);

print root;

if root.right
printFunc(root.right);
}

I think your tree would print as follows (I didn't code this, just a quick
run through in my head).

39 25 40 30 10 36 64

HTH,
kevin aubuchon

"Tracey" <Tr****@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
I have an algorithm question and don't know how to do it nicely.
Write a function to print nodes of binary tree, for each leaf node, print nodes from root node to leaf node.
For example, the following binary tree has 4 leaf nodes.
30
25 36
39 40 10 64

The resulting of print should be :
30, 25, 39
30, 25, 40
30, 36, 10
30, 36, 64

Nov 16 '05 #2
Tracey wrote:
I have an algorithm question and don't know how to do it nicely.
Write a function to print nodes of binary tree, for each leaf node, print nodes from root node to leaf node.
For example, the following binary tree has 4 leaf nodes.
30
25 36
39 40 10 64

The resulting of print should be :
30, 25, 39
30, 25, 40
30, 36, 10
30, 36, 64

printTree (tree, new Stack ());
void printTree (Tree root, Stack path)
{
// Are we at a leaf?
if ((root.left == null) && (root.right == null))
{
// It's a leaf, print the path and the leaf
print (path);
print (root);
}
else
{
// Not a leaf, so remember this node as part of the path and keep
going...
nodes.push (root);

// Stop at the light, look both ways, look both ways again...
if (root.left != null)
{
printTree (root.left, path);
}
if (root.right != null)
{
printTree (root.right, path);
}

// Well, we're done with this node, it's no longer prt of the path
node.pop ();
}
}

Off the top of my head, untested etc. Hope this helps.

Hilton
Nov 16 '05 #3
Briefly, you'd do this with a recursive depth-first traversal of the tree.
For each child of the root, pass an argument to the traversal routine that
would be a string containing the node number of the root. Each child would
add a comma and a space and its own node number, and recursively pass that
along to its child nodes, unless it had no children (i.e., was a leaf node),
in which case it would print out the string.

Thus, in the example given, the root node would pass "30" to its direct
children, nodes 25 and 36 would pass (respectively) "30, 25" and "30, 36" to
each of their children, and each of the leaf nodes would add their own node
numbers to the string they received and print it out.

Sorry I don't have time to write and include the code.
Tom Dacon
Dacon Software Consulting
"Tracey" <Tr****@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
I have an algorithm question and don't know how to do it nicely.
Write a function to print nodes of binary tree, for each leaf node, print nodes from root node to leaf node.
For example, the following binary tree has 4 leaf nodes.
30
25 36
39 40 10 64

The resulting of print should be :
30, 25, 39
30, 25, 40
30, 36, 10
30, 36, 64

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
2
by: Sherrie Laraurens | last post by:
Hi all, I'm trying to write a generic algorithm routine that will take begin and end iterators of a container, iterate through the range and perform a "calculation" of sorts. The trouble is...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
10
by: Sunway | last post by:
hello guys i have a bunsh of questions in java and algorithm most of them i didnt have a problem in them but two i had a diffculty in them could u help me in them please coz its urgent . the...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.