binary search tree proble. help !!
Dec 26, 2015 at 10:04am UTC
This code is about to insert number, i want to change it to search a name or alphabet according to inorder,postorder and preorder. Help me to change some syntax that i call insert name or alphabet in the code.
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 158 159
#include <iostream>
#include <cstdlib>
using namespace std;
class treeNode
{
public :
treeNode*left;
treeNode*right;
int data;
};
class BinarySearchTree
{
private :
treeNode*root;
public :
BinarySearchTree()
{root=NULL;
}
bool isEmpty () const {
return root ==NULL;}
void printInorder();
void Inorder (treeNode*);
void printPreorder();
void Preorder (treeNode*);
void printPostorder();
void Postorder (treeNode*);
void insert (int );
};
int main()
{
BinarySearchTree myTree;
int ch, inItem;
do
{
cout<<endl<<endl;
cout<<"Binary Search Tree Operations" <<endl;
cout<<"--------------------------------------" <<endl;
cout<<"1. Insertion/Creation " <<endl;
cout<<"2. Inorder Traversal " <<endl;
cout<<"3. Preorder Traversal " <<endl;
cout<<"4. Postorder Travesal " <<endl;
cout<<"5. Exit " <<endl;
cout<<"Enter Your choice :" ;
cin>>ch;
system ("cls" );
switch (ch)
{
case 1: cout<<" enter number to be inserted :" ;
cin >>inItem;
myTree.insert (inItem);
break ;
case 2: cout<<endl;
cout<<" Inorder Traversal " <<endl;
myTree.printInorder();
break ;
case 3: cout<<endl;
cout<<" Preorder travesal " <<endl;
myTree.printPreorder();
break ;
case 4: cout<<endl;
cout<<" Postorder Travesal " <<endl;
myTree.printPostorder ();
break ;
}
}while (ch!=5);
}
void BinarySearchTree::insert(int item) //insert
{
treeNode*temp=new treeNode;
treeNode*parent;
temp->data=item;
temp->left=NULL;
temp->right=NULL;
parent=NULL;
//is this new tree ?
if (isEmpty())root=temp;
else
{
//note:all insertions are as leafnode
treeNode*curr;
curr=root;
//find the node parent
while (curr)
{
parent=curr;
if (temp->data > curr->data) curr=curr->right;
else curr= curr->left;
}
if (temp->data<parent->data)
parent->left=temp;
else
parent->right=temp;
}
}
void BinarySearchTree::printInorder() //recursive
{
Inorder(root);
}
void BinarySearchTree::Inorder(treeNode*p)
{
if (p!=NULL)
{
if (p->left) Inorder (p->left); //CALL THE NODE
cout<<" " <<p->data<<" " ;
if (p->right) Inorder (p->right);
}
else return ;
}
void BinarySearchTree::printPreorder() //recursive
{
Preorder(root);
}
void BinarySearchTree::Preorder(treeNode*p)
{
if (p!=NULL)
{
cout<<" " <<p->data<<" " ;
if (p->left) Preorder (p->left); //CALL THE NODE
if (p->right) Preorder (p->right);
}
else return ;
}
void BinarySearchTree::printPostorder() //recursive
{
Postorder(root);
}
void BinarySearchTree::Postorder(treeNode*p)
{
if (p!=NULL)
{
if (p->left) Postorder (p->left); //CALL THE NODE
if (p->right) Postorder (p->right);
cout<<" " <<p->data<<" " ;
}
else return ;
}
Last edited on Dec 26, 2015 at 10:06am UTC
Dec 26, 2015 at 2:42pm UTC
Change treeNode::data to a string. Then search the code for "data" and examine each usage of the variable to see what (if anything) needs to change. This may sound a little tedious but trust me, it 's a good way to catch all the cases.
Dec 26, 2015 at 4:43pm UTC
sorry dhayden, I not understand well. can you explain and guide me
Dec 27, 2015 at 2:40am UTC
As I understand it, you need to change the tree to contain a name instead of a number. So you need to change treeNode's data member to a string:
1 2 3 4 5 6 7
class treeNode
{
public :
treeNode*left;
treeNode*right;
std::string data;
};
Then you should go through the code, looking for "data" and seeing what (if anything) needs to change because it's now a string instead of an int.
Dec 31, 2015 at 3:13am UTC
I have tried to change the code, but its not working. can you please help me :(
Dec 31, 2015 at 12:47pm UTC
Please post your modified code
Topic archived. No new replies allowed.