Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,167 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,886 people online right now. Registration is fast and FREE... Join Now!




Binary tree

 
Reply to this topicStart new topic

Binary tree, binary tree implentation in c++

harshakirans
14 Dec, 2007 - 07:52 AM
Post #1

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 113


Dream Kudos: 150
My Contributions
Hi this is basic binary Tree implementation in C++, am doin this after a long time. It doesnt work fine. Help me debug this out.Thanks

CODE

#include<iostream>
#include<conio.h>
using namespace std;

typedef struct NODE
{
      int data;
      NODE * lnode;
      NODE * rnode;
};




void insert(int data,int right,NODE * parent)
{

    NODE * node;
    NODE * first;
    node=parent;
    if(right==1 && node->rnode==NULL)
    {
    first=new struct NODE;
    node->rnode=first;
    first->data=data;
    first->lnode=first->rnode=NULL;
    }
    else if(right==0 && node->lnode==NULL)
    {
    first=new struct NODE;
    node->lnode=first;
    first->data=data;
    first->lnode=first->rnode=NULL;
    }
    else if(right)
    {
         insert(data,right,node->rnode);
    }
    else
    {
       insert(data,right,node->lnode);
    }
}


int main()
{
NODE parent;
NODE * cur;
cur=&parent;
parent.data=10;
parent.rnode=parent.lnode=NULL;
int choice,ele,right;
while(1)
{
       cout<<"\n1-insert \n2-display \n3-exit\nEnter ur choice ";
       cin>>choice;
       switch(choice)
       {
              case 1:
                   cout<<"\nenter the element";
                   cin>>ele;
                   cout<<"\nenter which side right=1 left=0\n";
                   cin>>right;
                   insert(ele,right,cur);
                   break;
              case 2:
                   cout<<"\n elements are";
                    cur=&parent;
                    cout<<"\n left node elements are";
                   while(1)
                   {
                           if(cur->lnode==NULL)
                           {
                                       cout<<" "<<cur->data<<"\nend";
                                        break;
                           }

                           cout<<cur->data;
                           cur=cur->lnode;
                   }
                   cout<<"\n right node elements are";
                   cur=&parent;
                   while(1)
                   {
                           if(cur->rnode==NULL)
                           {
                                        cout<<" "<<cur->data<<"\nend";
                                        break;
                           }

                           cout<<cur->data;
                           cur=cur->rnode;
                   }
                   break;
                   case 3:getch();
                   exit(0);
       }
}


}

User is online!Profile CardPM
+Quote Post

Amadeus
RE: Binary Tree
14 Dec, 2007 - 08:29 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Can you please elaborate? what is meant by 'doesn't work fine'? Are you getting compiler errors? If so, can you please post them? Are you getting unexpected output? If so, can you post the expected output as well as the actual output?
User is offlineProfile CardPM
+Quote Post

harshakirans
RE: Binary Tree
14 Dec, 2007 - 09:48 AM
Post #3

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 113


Dream Kudos: 150
My Contributions
QUOTE(Amadeus @ 14 Dec, 2007 - 09:29 AM) *

Can you please elaborate? what is meant by 'doesn't work fine'? Are you getting compiler errors? If so, can you please post them? Are you getting unexpected output? If so, can you post the expected output as well as the actual output?


No compiler errors


Purpose of the code: Implementation of simple tree data structure.

Work mechanism: insertion of integer data into right or left nodes of the tree to be constructed.

Anamoly : The insertion on the right nodes are working fine, but not in the left node.

Expected Help : To run the code once, i have tried it with DEV c++ , and any suggestions of making the tree grow both the sides.

Thank You.. ph34r.gif
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:07AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month