944,068 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6750
  • C RSS
Mar 2nd, 2007
0

Coding a Complete Binary Tree

Expand Post »
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:
  1. void BinaryTree::insert(int data, TreeNode* subTreeRoot, const
  2. TreeNode*& parent)
  3. {
  4. TreeNode temp;
  5. temp = subTreeRoot;
  6.  
  7. if ( subTreeRoot == NULL ) {
  8. subTreeRoot = new TreeNode(-1,NULL,NULL,temp);
  9. }
  10. else if ( subTreeRoot->left == NULL ) {
  11. insert(data,subTreeRoot->leftLink,temp);
  12. }
  13. else if ( subTreeRoot->right == NULL ) {
  14. insertNode (data,subTreeRoot->rightLink,temp);
  15. }
  16. else {
  17. insert(data,subTreeRoot->leftLink,subTreeRoot->leftLink);
  18. insert(data,subTreeRoot->rightLink,subTreeRoot->rightLink);
  19. }
  20. }
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 Narue; May 19th, 2011 at 9:06 am. Reason: Added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rabbit337 is offline Offline
2 posts
since Mar 2007
Mar 2nd, 2007
0

Re: Coding a Complete Binary Tree

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
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 2nd, 2007
0

Re: Coding a Complete Binary Tree

I went ahead with the temporary array approach. Much more simplistic.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rabbit337 is offline Offline
2 posts
since Mar 2007
May 19th, 2011
0
Re: Coding a Complete Binary Tree
Here is a code i hav tried to implement a sample complete binary tree......but using a different logic...........

U can try dis out...

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct btree{
  5. int id, val;
  6. struct btree *left, *right;
  7. };
  8.  
  9. typedef struct btree node;
  10.  
  11. void myparent(node *tree, int myid, node **parent){
  12. if(tree->id==(myid/2))
  13. *parent = tree;
  14. if(tree->left)
  15. myparent(tree->left, myid, parent);
  16. if(tree->right)
  17. myparent(tree->right, myid, parent);
  18. }
  19.  
  20. void insert(node **tree, node *item){
  21. node *parent;
  22. if(item->id==1)
  23. *tree=item;
  24. else{
  25. myparent(*tree, item->id,&parent);
  26. if((item->id)%2)
  27. parent->right=item;
  28. else
  29. parent->left=item;
  30. }
  31. }
  32.  
  33. void printout(node *tree){
  34. if(tree->left)
  35. printout(tree->left);
  36. printf("%d\n", tree->val);
  37. if(tree->right)
  38. printout(tree->right);
  39. }
  40.  
  41. main(){
  42. node *root, *curr;
  43. int idcount=1, inp=0;
  44. root=NULL;
  45. //input 9999 is the exit point
  46. while(inp!=9999){
  47. printf("\nEnter a Node>");
  48. scanf("%d",&inp);
  49. curr=(node*)malloc(sizeof(node));
  50. curr->val=inp;
  51. curr->id=idcount++;
  52. curr->left = curr->right = NULL;
  53. insert(&root, curr);
  54. }
  55.  
  56.  
  57. printf("\n Entered Binary Tree is \n");
  58. printout(root);
  59. return 0;
  60. }



The logic is explained below...........
Last edited by riazcp; May 19th, 2011 at 6:36 am. Reason: Advancement
Reputation Points: 7
Solved Threads: 0
Newbie Poster
riazcp is offline Offline
3 posts
since May 2011
May 19th, 2011
-1
Re: Coding a Complete Binary Tree
The logic is by putting an extra property 'id' for each node..............

Consider d eg tree
  1. A(1)
  2. B(2) C(3)
  3. D(4) E(5) F(6) G(7)
  4. H(8) I(9) J(10) K(11) L(12) M(13) N(14) O(15)

here the no on the () are the ids of each node.....from d above eg u wil came to knw dat for every node with id x its parent node will be with id x/2...except d root

so d thing u have to do while inserting is dat identify d parent for each node...for dat u can use d basic tree taversal methods...
Last edited by riazcp; May 19th, 2011 at 6:47 am.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
riazcp is offline Offline
3 posts
since May 2011
May 19th, 2011
0
Re: Coding a Complete Binary Tree
What is this printout is supposed to mean?
  1. Enter a Node (9999 to finish)>1
  2.  
  3. Enter a Node (9999 to finish)>1
  4.  
  5. Enter a Node (9999 to finish)>2
  6.  
  7. Enter a Node (9999 to finish)>21
  8.  
  9. Enter a Node (9999 to finish)>1
  10.  
  11. Enter a Node (9999 to finish)>2
  12.  
  13. Enter a Node (9999 to finish)>1
  14.  
  15. Enter a Node (9999 to finish)>2
  16.  
  17. Enter a Node (9999 to finish)>1
  18.  
  19. Enter a Node (9999 to finish)>1
  20.  
  21. Enter a Node (9999 to finish)>9999
  22.  
  23. Entered Binary Tree is
  24. 2
  25. 21
  26. 1
  27. 1
  28. 1
  29. 1
  30. 9999
  31. 1
  32. 2
  33. 2
  34. 1
  35.  
  36. Process returned 0 (0x0) execution time : 18.062 s
  37. Press any key to continue.
Featured Poster
Reputation Points: 687
Solved Threads: 748
Industrious Poster
pyTony is offline Offline
4,208 posts
since Apr 2010
May 20th, 2011
0
Re: Coding a Complete Binary Tree
The function printout() shows the inorder traversed tree. You could verify it with the example inputs you have given.
The complete binary tree will be like this.
  1. 1(a)
  2.  
  3. 1(b) 2(c)
  4.  
  5. 21(d) 1(e) 2(f) 1(g)
  6.  
  7. 2(h) 1(i) 1(j) 9999(k)


The inorder traversal of the above tree gives the following output.
2(h),21(d),1(i),1(b),1(j),1(e),9999(k),1(a),2(f),2(c),1(g)
Last edited by riazcp; May 20th, 2011 at 1:15 am.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
riazcp is offline Offline
3 posts
since May 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Stupid Question
Next Thread in C Forum Timeline: computing the convex hull of a bunch of points





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC