954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Coding a Complete Binary Tree

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.

rabbit337
Newbie Poster
2 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I went ahead with the temporary array approach. Much more simplistic.

rabbit337
Newbie Poster
2 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Here is a code i hav tried to implement a sample complete binary tree......but using a different logic...........

U can try dis out...

#include<stdio.h>
#include<stdlib.h>

struct btree{
        int id, val;
        struct btree *left, *right;
};

typedef struct btree node;

void myparent(node *tree, int myid, node **parent){
        if(tree->id==(myid/2))
                *parent = tree;
        if(tree->left)
                myparent(tree->left, myid, parent);
        if(tree->right)
                myparent(tree->right, myid, parent);
}

void insert(node **tree, node *item){
        node *parent;
        if(item->id==1)
                *tree=item;
        else{
                myparent(*tree, item->id,&parent);
                if((item->id)%2)
                        parent->right=item;
                else
                        parent->left=item;
        }
}

void printout(node *tree){
        if(tree->left)
                printout(tree->left);
        printf("%d\n", tree->val);
        if(tree->right)
                printout(tree->right);
}

main(){
        node *root, *curr;
        int idcount=1, inp=0;
        root=NULL;
        //input 9999 is the exit point
        while(inp!=9999){
                printf("\nEnter a Node>");
                scanf("%d",&inp);
                curr=(node*)malloc(sizeof(node));
                curr->val=inp;
                curr->id=idcount++;
                curr->left = curr->right = NULL;
                insert(&root, curr);
        }


        printf("\n Entered Binary Tree is \n");
        printout(root);
        return 0;
}


The logic is explained below...........

riazcp
Newbie Poster
3 posts since May 2011
Reputation Points: 7
Solved Threads: 0
 

The logic is by putting an extra property 'id' for each node..............

Consider d eg tree

A(1)
                  B(2)                                         C(3)
        D(4)                 E(5)                     F(6)             G(7)
   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...

riazcp
Newbie Poster
3 posts since May 2011
Reputation Points: 7
Solved Threads: 0
 

What is this printout is supposed to mean?

Enter a Node (9999 to finish)>1

Enter a Node (9999 to finish)>1

Enter a Node (9999 to finish)>2

Enter a Node (9999 to finish)>21

Enter a Node (9999 to finish)>1

Enter a Node (9999 to finish)>2

Enter a Node (9999 to finish)>1

Enter a Node (9999 to finish)>2

Enter a Node (9999 to finish)>1

Enter a Node (9999 to finish)>1

Enter a Node (9999 to finish)>9999

 Entered Binary Tree is
2
21
1
1
1
1
9999
1
2
2
1

Process returned 0 (0x0)   execution time : 18.062 s
Press any key to continue.
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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(a)
                                  
                                            1(b)                                                   2(c)

                              21(d)                       1(e)                          2(f)                          1(g)

                        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)

riazcp
Newbie Poster
3 posts since May 2011
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You