Need Help asto 2-3 Trees

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 8
Reputation: Umar Ali is an unknown quantity at this point 
Solved Threads: 0
Umar Ali Umar Ali is offline Offline
Newbie Poster

Need Help asto 2-3 Trees

 
0
  #1
Nov 26th, 2008
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.....
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Two3;
  5. class Two3Node{
  6. friend class Two3;
  7. private:
  8. int data1, data2;
  9. Two3Node *lc, *mc, *rc, *par; //leftchild, rightchild, middlechild and parentchild
  10. public:
  11. Two3Node();
  12. ~Two3Node();
  13. };
  14.  
  15. class Two3{
  16. private:
  17. Two3Node *root;
  18. public:
  19. Two3();
  20. ~Two3();
  21. bool Insert(int x);
  22. bool Search(int x);
  23. Two3Node *SearchNode(int x);
  24. bool IsEmpty();
  25. bool Is2Node(Two3Node *check);
  26. bool Is3Node(Two3Node *check);
  27. void Split(Two3Node *&n, int num);
  28. int Min(int n1, int n2, int n3);
  29. int Mid(int n1, int n2, int n3);
  30. int Max(int n1, int n2, int n3);
  31. };
  32.  
  33. //Search Function
  34.  
  35.  
  36. bool Two3::Search(int x){
  37. Two3Node *temp= root;
  38. while(temp!=NULL){
  39. if(temp->data1==x || temp->data2==x)
  40. return true;
  41. else if(temp->data1!=-1 && temp->data2!=-1){
  42. if(x<temp->data1)
  43. temp=temp->lc;
  44. else if(x<temp->data2)
  45. temp=temp->mc;
  46. else
  47. temp=temp->rc;
  48. }
  49. else if(temp->data1!=0 && temp->data2==-1){
  50. if(x<temp->data1)
  51. temp=temp->lc;
  52. else
  53. temp=temp->mc;
  54. }
  55. } //end while
  56. return false;
  57. }
  58.  
  59. //SearchNode Function
  60.  
  61.  
  62. Two3Node *Two3::SearchNode(int x){
  63. Two3Node *temp=root;
  64. while(temp->lc && temp->mc){ //checking condition for leaf/terminal node
  65. if(x<temp->data1)
  66. temp=temp->lc;
  67. else if(x<temp->data2)
  68. temp=temp->mc;
  69. else if()
  70. temp=temp->rc;
  71. }
  72. return temp;
  73. }
  74.  
  75. //Is2Node Function
  76.  
  77. bool Two3::Is2Node(Two3Node *check){
  78. if(check->data1!=-1 && check->data2==-1 && check->lc==NULL && check->mc==NULL)
  79. return true;
  80. }
  81.  
  82. //Is3Node Function
  83.  
  84. bool Two3::Is3Node(Two3Node *check){
  85. if(check->data1!=-1 && check->data2!=-1 && check->lc==NULL && check->mc==NULL && check->rc==NULL)
  86. return false;
  87. }
  88.  
  89. //Insert Function
  90.  
  91.  
  92. bool Two3::Insert(int x){
  93. if(x==-1){
  94. cerr<<"This value can't be inserted";
  95. return false;
  96. }
  97. if(root==NULL){
  98. root->data1=x;
  99. return true;
  100. }
  101. if(Search(x))
  102. return false;
  103.  
  104. Two3Node *curr=SearchNode(x);
  105.  
  106. if(Is2Node(curr)){ //case 1: curr is a 2 node and a terminal node
  107. if(x>curr->data1)
  108. curr->data2=x;
  109. else if(x<curr->data1){
  110. curr->data2=curr->data1;
  111. curr->data1=x;
  112. }
  113. }
  114. else if(Is2Node(curr)){
  115. Split(curr, x);
  116.  
  117. }
  118. }
  119.  
  120. //Split Function
  121.  
  122. void Two3::Split(Two3Node *&n, int num){
  123. int a, b, c;
  124. a=Min(n->data1, n->data2, num);
  125. b=Mid(n->data1, n->data2, num);
  126. c=Max(n->data1, n->data2, num);
  127.  
  128. Two3Node *temp;
  129. n->data1=a;
  130. temp->data1=c;
  131.  
  132. }
Last edited by Umar Ali; Nov 26th, 2008 at 10:57 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 584
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 94
Murtan Murtan is offline Offline
Posting Pro

Re: Need Help asto 2-3 Trees

 
0
  #2
Nov 27th, 2008
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.

  1. class Foo
  2. {
  3. private:
  4. int mX;
  5. int my;
  6. public:
  7. Foo();
  8. int getX();
  9. int getY();
  10. void setX(int x);
  11. void setY(int y);
  12. };
  13.  
  14. 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
  1. if(root==NULL){
  2. root->data1=x;
  3. return true;
  4. }
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC