Can't figure out this specific error.
Feb 1, 2013 at 8:06pm UTC
I seem to be getting an error my code on the extract function where I have my if else if and else statement is. It tells me i need a semicolon (;) before the bracket on my else statement. I just need to figure out this statement and debug it so I can have it extract a node in the binary search tree and keep the tree intact. Can you all help me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include "binarySearchTree.h"
using namespace std;
int main()
{
binarySearchTree tree;
tree.insert(53);
tree.insert(11);
tree.insert(98);
tree.insert(74);
tree.insert(7);
tree.insert(5);
tree.insert(48);
tree.insert(2);
tree.insert(23);
tree.insert(100);
tree.insert(63);
tree.display();
tree.height();
tree.extractmin();
//tree.extractmin();
//tree.extractmin();
tree.remove(63);
tree.remove(7);
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#include <iostream>
using namespace std;
class binarySearchTree
{
private :
class node
{
public :
int data;
node * left;
node * right;
node(int x)
{
data = x;
left = NULL;
right = NULL;
}
};
node * root;
//recursive methods
void recInsert(int x, node * &r)
{
if ( r == NULL )
{
r = new node(x);
}
else
{
if ( x < r->data )
{
recInsert(x, r->left);
}
else
{
recInsert(x, r->right);
}
}
}
//display items in tree rooted at r
void recDisplay(node * r)
{
if ( r != NULL )
{
recDisplay(r->left);
cout << r->data << endl;
recDisplay(r->right);
}
}
//return height of tree rooted at r
int getHeight( node * r )
{
// Max is the greatest height below (*r), temp is just used to avoid calling getHeight(r->right) twice
int max=0,temp=0;
// Recurse down lhs
if (r->left!=NULL) max=getHeight(r->left);
// Recurse down rhs
if (r->right!=NULL) temp=getHeight(r->right);
if (temp>max) max=temp;
// Return the max
return max+1;
}
int extractMin(node * &r)
{
int num=0;
node *min = NULL;
node *tmp = NULL;
min = r;
// Recurse down lhs
while (min->left!=NULL)
{
tmp = min;
min = min->left;
}
tmp->left = NULL;
if (r->right!=NULL)
tmp->left = min->right;
num = min->data;
delete min;
return num;
}
int extract(int a, node * &r)
{
int num=0;
node *min = NULL;
node *tmp = NULL;
int temp = 0;
temp = a;
min = r;
if (min->data == a)
{
if (min->right != NULL)
extractMin(min->right);
else
r = r->left;
}
else if (min->data < a && min->right != NULL)
{
extract(temp, min->right);
}
else (min->data > a && min->left != NULL)
{
extract(temp, min->left);
}
return temp;
}
public :
binarySearchTree()
{
root = NULL;
}
void insert(int x)
{
recInsert(x, root);
}
//display all items in tree
void display()
{
recDisplay(root);
}
void height()
{
cout<<getHeight(root)<<endl;
}
void extractmin()
{
cout<<extractMin(root)<<endl;
}
void remove(int z)
{
cout<<extract(z, root)<<endl;
}
};
Feb 1, 2013 at 8:31pm UTC
It's because you put a condition after your "else".
A "else" takes no condition. As a consequence your compiler thinks that what follows is simply a statement. As a statement it indeed lacks a semicolon.
Maybe you wanted to write "else if" ?
Feb 4, 2013 at 5:52pm UTC
Haven't gotten a chance to log on in days but I put and else if instead of an else like you said and it worked. I guess I haven't programmed in a while but I thought it would be in the format of
if x
then y
else if z
then a
else b
Topic archived. No new replies allowed.