Binary tree

binary tree implentation in c++

Page 1 of 1

2 Replies - 3718 Views - Last Post: 14 December 2007 - 10:48 AM Rate Topic: -----

#1 harshakirans  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 124
  • Joined: 26-April 06

Binary tree

Posted 14 December 2007 - 08:52 AM

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

#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);
	   }
}


}



Is This A Good Question/Topic? 0
  • +

Replies To: Binary tree

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Binary tree

Posted 14 December 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?
Was This Post Helpful? 0
  • +
  • -

#3 harshakirans  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 124
  • Joined: 26-April 06

Re: Binary tree

Posted 14 December 2007 - 10:48 AM

View PostAmadeus, on 14 Dec, 2007 - 09:29 AM, said:

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:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1