Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-2007, 08:04 PM
TAboy24 TAboy24 is offline
Newbie
 
Join Date: May 2007
Posts: 6
Rep Power: 0
TAboy24 is on a distinguished road
Default Binary to list??

I want to write a function
int Listofleaves (Tree tree,List* list)

which recieve a binary tree -"tree"(the input) and a list pointer "list"(output)...the function supposed to return the trees leaves nodes(nodes with no children) from right to left as a linked list(!) in "list"...and also return at the end the sum of all those leaves....
anyone knows???
thanks for help people
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-03-2007, 12:48 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

When you just say "List", which kind is it then? Linked List, or just some object?

You have to run through the tree, and then check each of the nodes left node-pointer and right node-pointer. If both is NULL, then the node is a leave-node. While doing that you're taking the value in the leave-node, when finding one, and add it to the sum.
I don't know about the List you were talking about, so I can't say how you're gonna using that one.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-03-2007, 02:38 AM
TAboy24 TAboy24 is offline
Newbie
 
Join Date: May 2007
Posts: 6
Rep Power: 0
TAboy24 is on a distinguished road
Default linked list

I meant linked list the problem ewas to write this code but returning a linked list tat contains the trees leafes from left to right and the function returns their sum as well
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-03-2007, 09:40 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

To add a node on your linked list, you first have to check the pointer to the start of the list. If this' pointer to the next node is NULL, you set the next-pointer to the new node.
Then you have to set the pointer to the end of the lists next-pointer to the new node, set the next-pointer in the new node to NULL, and then point the whole end-pointer to the new node. Of course, when you want to have the value of the binary trees node too, you have to set that value in the new node of the linked list.

If your linked list doesn't have an end-pointer, but only a start-pointer (which most actually have), then it's a bit harder to implement. You still have to check if the start-pointer is NULL, but if it isn't (it already had been used) you have to go through the whole list, and check if you're at the end. Here's an example:
Code:
void addNodeAtEnd(node *newNode)
{
	node *temporaryNode = new node;

	if(startNode == NULL)
		startNode = newNode;
	else
	{
		temporaryNode = startNode;
		while(temporaryNode->next != NULL)
			temporaryNode = temporaryNode->next;
		temporaryNode->next = newNode;
	}
		
	delete temporaryNode;
}
There's tons of information about this subject, around on the internet.
Just make a fast search with some of the many search engines.
__________________
05-03-2007 - 11-13-2008

Last edited by v0id; 05-03-2007 at 10:42 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending mail to a group of people (Mailing list support/Emulation) nesrait easyContact 1 07-25-2007 09:48 PM
GMail Vulnerable To Contact List Hijacking TcM Computer Software/OS 28 02-17-2007 11:36 AM
Binary, Decimal, Hex, the Manual way!! TcM Tutorials 22 01-10-2007 01:06 AM
Binary? NeedHelp Managed C++ 4 07-27-2006 03:59 PM
Binary Conversion in VB roger Visual Basic Programming 0 06-01-2006 11:50 AM


All times are GMT -5. The time now is 02:02 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads