| | |
Need Help asto 2-3 Trees
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
Hello guys!
Hope everyone's doing good. Well I desperately need the code for insertion in 2-3 trees, can anyone of you kindly help me out??? Actually I missed one of the lectures given in this regard, therefore i'm facing few daft problems, I tried to some extent but couldn't make it. I need to submit this by tomorrow, please help me out...I'll be really really grateful. Here's the code.
And I didn't put couple of functions like finding min,max,or mid for sorting, and constructors, destructors. Well split function is also incomplete.
God bless.....
Hope everyone's doing good. Well I desperately need the code for insertion in 2-3 trees, can anyone of you kindly help me out??? Actually I missed one of the lectures given in this regard, therefore i'm facing few daft problems, I tried to some extent but couldn't make it. I need to submit this by tomorrow, please help me out...I'll be really really grateful. Here's the code.
And I didn't put couple of functions like finding min,max,or mid for sorting, and constructors, destructors. Well split function is also incomplete.
God bless.....
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class Two3; class Two3Node{ friend class Two3; private: int data1, data2; Two3Node *lc, *mc, *rc, *par; //leftchild, rightchild, middlechild and parentchild public: Two3Node(); ~Two3Node(); }; class Two3{ private: Two3Node *root; public: Two3(); ~Two3(); bool Insert(int x); bool Search(int x); Two3Node *SearchNode(int x); bool IsEmpty(); bool Is2Node(Two3Node *check); bool Is3Node(Two3Node *check); void Split(Two3Node *&n, int num); int Min(int n1, int n2, int n3); int Mid(int n1, int n2, int n3); int Max(int n1, int n2, int n3); }; //Search Function bool Two3::Search(int x){ Two3Node *temp= root; while(temp!=NULL){ if(temp->data1==x || temp->data2==x) return true; else if(temp->data1!=-1 && temp->data2!=-1){ if(x<temp->data1) temp=temp->lc; else if(x<temp->data2) temp=temp->mc; else temp=temp->rc; } else if(temp->data1!=0 && temp->data2==-1){ if(x<temp->data1) temp=temp->lc; else temp=temp->mc; } } //end while return false; } //SearchNode Function Two3Node *Two3::SearchNode(int x){ Two3Node *temp=root; while(temp->lc && temp->mc){ //checking condition for leaf/terminal node if(x<temp->data1) temp=temp->lc; else if(x<temp->data2) temp=temp->mc; else if() temp=temp->rc; } return temp; } //Is2Node Function bool Two3::Is2Node(Two3Node *check){ if(check->data1!=-1 && check->data2==-1 && check->lc==NULL && check->mc==NULL) return true; } //Is3Node Function bool Two3::Is3Node(Two3Node *check){ if(check->data1!=-1 && check->data2!=-1 && check->lc==NULL && check->mc==NULL && check->rc==NULL) return false; } //Insert Function bool Two3::Insert(int x){ if(x==-1){ cerr<<"This value can't be inserted"; return false; } if(root==NULL){ root->data1=x; return true; } if(Search(x)) return false; Two3Node *curr=SearchNode(x); if(Is2Node(curr)){ //case 1: curr is a 2 node and a terminal node if(x>curr->data1) curr->data2=x; else if(x<curr->data1){ curr->data2=curr->data1; curr->data1=x; } } else if(Is2Node(curr)){ Split(curr, x); } } //Split Function void Two3::Split(Two3Node *&n, int num){ int a, b, c; a=Min(n->data1, n->data2, num); b=Mid(n->data1, n->data2, num); c=Max(n->data1, n->data2, num); Two3Node *temp; n->data1=a; temp->data1=c; }
Last edited by Umar Ali; Nov 26th, 2008 at 10:57 pm.
•
•
Join Date: May 2008
Posts: 584
Reputation:
Solved Threads: 94
You have a basic misunderstanding related to pointers. Pointers must be made to point to a valid storage location before they can be used to reference anything.
At this point, the
To be useful, the pointer needs to point 'somewhere'. Commonly in variable-length collections, the object to be pointed to is allocated.
There are several places where you use pointers without making sure they are properly initialized. The code
is an especially grevious example of this in that you have explicitly verified that the pointer does NOT point anywhere, yet you proceed to use it anyway.
You will need to fix this first before you have a chance of getting this working.
c++ Syntax (Toggle Plain Text)
class Foo { private: int mX; int my; public: Foo(); int getX(); int getY(); void setX(int x); void setY(int y); }; Foo * pMyFoo;
At this point, the
pMyFoo is declared, but it was never initialized to point anywhere. Referencing the Foo that is pointed to by this pointer would be permitted by the compiler, but it may well cause a memory fault.To be useful, the pointer needs to point 'somewhere'. Commonly in variable-length collections, the object to be pointed to is allocated.
pMyFoo = new Foo(); gives us something to point to.There are several places where you use pointers without making sure they are properly initialized. The code
c++ Syntax (Toggle Plain Text)
if(root==NULL){ root->data1=x; return true; }
You will need to fix this first before you have a chance of getting this working.
![]() |
Other Threads in the C++ Forum
- Previous Thread: How to delete the content of a file (Win32 API)
- Next Thread: Stack with Dynamic Array
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





