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