| | |
Coding a Complete Binary Tree
![]() |
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 0
My task is to create a complete binary tree. The only requirements are that it be a dynamic tree with nodes and pointers - I may use temporary arrays to help with the intial tree construction. Where I need help is with the insert(Node) method. Here's what I have so far:
void BinaryTree::insert(int data, TreeNode* subTreeRoot, const
TreeNode*& parent)
{
TreeNode temp;
temp = subTreeRoot;
if ( subTreeRoot == NULL ) {
subTreeRoot = new TreeNode(-1,NULL,NULL,temp);
}
else if ( subTreeRoot->left == NULL ) {
insert(data,subTreeRoot->leftLink,temp);
}
else if ( subTreeRoot->right == NULL ) {
insertNode (data,subTreeRoot->rightLink,temp);
}
else {
insert(data,subTreeRoot->leftLink,subTreeRoot->leftLink);
insert(data,subTreeRoot->rightLink,subTreeRoot->rightLink);
}
}
Each node has three links and one data field - leftLink,rightLink,parent, and data. The reason for the parent link is to help with another part of the project. Basically, the problem I have is inserting the nodes in the proper "complete binary tree" method. That is, how do I insert the nodes in such a manner that all leafs are at height n or n-1 of a n tall tree? I believe the code I have right now will simply insert nodes on the far most left side, which will not create a complete binary tree. The code correctly creates a tree with a root and two children, but I believe it falls apart after that. Also, how do I keep track of the parent so that I can store that into the parent link field? I'm not sure if my current method accomplishes that.
I am trying to use a recursive method to accomplish my task, but I may have to switch to using temporary arrays.
Any help is greatly appreciated. I will be working on this all day so please reply if you have any input at all.
Thanks.
void BinaryTree::insert(int data, TreeNode* subTreeRoot, const
TreeNode*& parent)
{
TreeNode temp;
temp = subTreeRoot;
if ( subTreeRoot == NULL ) {
subTreeRoot = new TreeNode(-1,NULL,NULL,temp);
}
else if ( subTreeRoot->left == NULL ) {
insert(data,subTreeRoot->leftLink,temp);
}
else if ( subTreeRoot->right == NULL ) {
insertNode (data,subTreeRoot->rightLink,temp);
}
else {
insert(data,subTreeRoot->leftLink,subTreeRoot->leftLink);
insert(data,subTreeRoot->rightLink,subTreeRoot->rightLink);
}
}
Each node has three links and one data field - leftLink,rightLink,parent, and data. The reason for the parent link is to help with another part of the project. Basically, the problem I have is inserting the nodes in the proper "complete binary tree" method. That is, how do I insert the nodes in such a manner that all leafs are at height n or n-1 of a n tall tree? I believe the code I have right now will simply insert nodes on the far most left side, which will not create a complete binary tree. The code correctly creates a tree with a root and two children, but I believe it falls apart after that. Also, how do I keep track of the parent so that I can store that into the parent link field? I'm not sure if my current method accomplishes that.
I am trying to use a recursive method to accomplish my task, but I may have to switch to using temporary arrays.
Any help is greatly appreciated. I will be working on this all day so please reply if you have any input at all.
Thanks.
Last edited by rabbit337; Mar 2nd, 2007 at 2:42 pm. Reason: Add more information
I always thought this was a good help. Well the concept anyway. The rest is just programming semantics.
http://cslibrary.stanford.edu/110/BinaryTrees.html
http://cslibrary.stanford.edu/110/BinaryTrees.html
*Voted best profile in the world*
![]() |
Similar Threads
- complete binary tree using an array help (C++)
- Question about binary tree & heaps (Computer Science)
- C++ complete binary tree using an array. Unexpected end file (C++)
- coding a complete binary tree with Dev-C++ (C++)
Other Threads in the C Forum
- Previous Thread: Hw its a true statement??
- Next Thread: get the coordenates of a word in a puzzle
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






