Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 04-27-2005, 08:06 PM   #1 (permalink)
TBNolan
Registered User
 
Join Date: Apr 2005
Posts: 2
TBNolan is on a distinguished road
Heap Tree

Hello all

i need to be practically spoon fed- i need to create a complete binary tree. each time i call this function add() i need to detect where the next empty spot in the tree is is using the two variables, count (the number of nodes in the tree) and height (the depth/number of rows to the tree). I also am passing a pointer to the root of the tree. I was planning on starting at the root and navigating, left to right, until i hit a leaf or a node with only one child.

any suggestions? maybe recursion? i just cant seem to wrap my head around the logic.

Thank you in advance
__________________
TBNolan is offline   Reply With Quote
Old 04-27-2005, 11:13 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
The value you're adding will determain where in teh tree it is going to be located, your seach tree here will always have values less than (or equal to) your value to add placed on the left branch, values higher placed on the right.

So some sort of bubble sort algorithm would do, see if the leaf value is less or equal to, then go right, if higher go left, when leaf holds an empty branch in the intended direction, place value here.

The real problem arises when deleting from the tree, then you'd need a balance function to make sure the tree structurial will allways support your adding scheme.

But most likely this tree handling, beeing adding, deleting or balancing will require some form of recursive calling.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-27-2005, 11:36 PM   #3 (permalink)
TBNolan
Registered User
 
Join Date: Apr 2005
Posts: 2
TBNolan is on a distinguished road
Quote:
Originally Posted by redhead
The value you're adding will determain where in teh tree it is going to be located, your seach tree here will always have values less than (or equal to) your value to add placed on the left branch, values higher placed on the right.
i understand that once i add it to the heap, i will need to 'reheapify' to make sure each parent is less than its child---

i plan to add the new entry to the next available location, maintaining the Complete Binary Tree property, but then it will not be a heap, so i will then re-heapify.

the problem arises when i try to find the next available place that will maintain the CBT. i can't seem to come up with the correct algorithm to do this using only count, height, and a pointer to the root node.
__________________
TBNolan is offline   Reply With Quote
Old 04-28-2005, 12:06 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
I've found a few pointers which might be of use for you.
First the theory in heap trees
Then two implementations 1; 2 in C++
Then an example in C

These might no be, exactly what you're looking for, but it should give you a clear view on how you want to implement it.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating tree control with specified directory folders and files HanaDin Platform/API C++ 2 07-14-2004 05:16 PM
news: Gobo Linux and a new Linux Tree sde Linux / BSD / OS X 0 05-19-2003 03:47 PM


All times are GMT -8. The time now is 09:06 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle