As said in the title, my inorder_two_three is not working correctly and I cant figure out the reason.
Code:
#include <iostream>
using namespace std;
struct TreeNode
{
int info;
TreeNode * left;
TreeNode * right;
};
struct Node
{
int small;
int large;
Node * left;
Node* middle;
Node* right;
};
void insert(TreeNode *& tree, int item);
void inOrder(TreeNode *tree);
void in_order_two_three(Node * node);
Node *TreeSearch_two_three(Node *node, int key);
TreeNode *TreeSearch(TreeNode *node, int key);
int main()
{
TreeNode *root = new TreeNode;
root = NULL;
Node * root_2 = new Node;
insert(root, 2);
insert(root, 7);
insert(root, 10);
insert(root, 15);
insert(root, 18);
insert(root, 20);
cout << "*******Part 1 ***************" << endl;
cout << "Inserted numbers are: ";
inOrder(root);
cout << endl << endl;
TreeSearch(root, 7);
TreeSearch(root, 13);
root_2->small = 50;
root_2->large = 90;
root_2->left = NULL;
root_2->middle = NULL;
root_2->right = NULL;
Node *two = new Node;
two->small = 20;
two->large = NULL;
two->left = NULL;
two->middle = NULL;
two->right = NULL;
Node *three = new Node;
three->small = 70;
three->large = NULL;
three->left = NULL;
three->middle = NULL;
three->right = NULL;
Node *four = new Node;
four->small = 120;
four->large = 150;
four->middle = NULL;
four->left = NULL;
four->right = NULL;
Node *five = new Node;
five->small = 10;
five->large = NULL;
five->left = NULL;
five->middle = NULL;
five->right = NULL;
Node *six = new Node;
six->small = 30;
six->large = 40;
six->left = NULL;
six->middle = NULL;
six->right = NULL;
Node *seven = new Node;
seven->small = 60;
seven->large = NULL;
seven->left = NULL;
seven->middle = NULL;
seven->right = NULL;
Node *eight = new Node;
eight->small = 80;
eight->left = NULL;
eight->middle = NULL;
eight->right = NULL;
Node *nine = new Node;
nine->small = 100;
nine->large = 110;
nine->left = NULL;
nine->middle = NULL;
nine->right = NULL;
Node *ten = new Node;
ten->small = 130;
ten->large = 140;
ten->left = NULL;
ten->middle = NULL;
ten->right = NULL;
Node *eleven = new Node;
eleven->small = 160;
eleven->large = NULL;
eleven->left = NULL;
eleven->middle = NULL;
eleven->right = NULL;
root_2->left = two;
root_2->middle = three;
root_2->right = four;
two->left = five;
two->right = six;
three->left = seven;
three->right = eight;
four->left = nine;
four->middle = ten;
four->right = eleven;
cout << "*******Part 2 ***************" << endl;
cout << "Inorder traversal for the 2-3 tree is :" << endl;
in_order_two_three(root_2);
cout << endl;
TreeSearch_two_three(root_2, 70);
TreeSearch_two_three(root_2, 200);
system("pause");
return 0;
}
void insert(TreeNode *& tree, int item)
{
if (tree == NULL)
{
tree = new TreeNode;
tree->left = NULL;
tree->right = NULL;
tree->info = item;
}
else if (item < tree->info)
{
insert(tree->left, item);
}
else
{
insert(tree->right, item);
}
}
void inOrder(TreeNode *tree)
{
if (tree != NULL)
{
inOrder(tree->left);
cout << tree->info << " ";
inOrder(tree->right);
}
}
TreeNode *TreeSearch(TreeNode *tree, int item)
{
if (tree == NULL || item == tree->info)
{
return tree;
}
if (item < tree->info)
{
return TreeSearch(tree->left, item);
}
else
{
return TreeSearch(tree->right, item);
}
}
void in_order_two_three(Node * node)
{
if (node->left == NULL && node->right == NULL)
{
cout << node->small << " ";
}
else if (node != NULL)
{
in_order_two_three(node->left);
cout << node->small << " ";
in_order_two_three(node->middle);
cout << node->large << " ";
in_order_two_three(node->right);
}
else
{
in_order_two_three(node->left);
cout << node->small << " ";
in_order_two_three(node->right);
}
}
Node *TreeSearch_two_three(Node *node, int item)
{
if (node->small == item || node->small == NULL)
{
return node;
}
else if (node->small && node->large)
{
if (item < node->small)
{
return TreeSearch_two_three(node->left, item);
}
else if (item > node->large)
{
return TreeSearch_two_three(node->right, item);
}
}
else
{
return TreeSearch_two_three(node->middle, item);
}
}