Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #define NULL __null
- using namespace std;
- typedef struct treenode {
- string data;
- struct treenode* left;
- struct treenode* right;
- } treenode;
- treenode* input(treenode* node, char* data)
- {
- if(node == NULL){
- treenode* word;
- cout<<"[node] ";
- word = new treenode[100];
- word->data = data;
- word->left = NULL;
- word->right = NULL;
- return word;
- }
- else if(node->data.compare(data) > 0){
- //else if (data < node->data){
- cout<<"[left] ";
- node->left = input(node->left,data);
- }
- else if(node->data.compare(data) < 0){
- cout<<"[right] ";
- node->right = input(node->right,data);
- }
- else{
- cout<<endl<<"ERROR>Already Reegistered";
- return node;
- }
- }
- void preorder(treenode* node)
- {
- cout<<node->data<<" ";
- if (node->left) preorder(node->left);
- if (node->right) preorder(node->right);
- }
- void ctrorder(treenode* node)
- {
- if (node->left) ctrorder(node->left);
- cout<<node->data<<" ";
- if (node->right) ctrorder(node->right);
- }
- void lstorder(treenode* node)
- {
- if (node->right) lstorder(node->right);
- cout<<node->data<<" ";
- if (node->left) lstorder(node->left);
- /*
- if (node->left) lstorder(node->left);
- if (node->right) lstorder(node->right);
- cout<<node->data<<" ";*/
- }
- void search(treenode* node , char* data)
- {
- if(node == NULL){
- cout<<"ERROR>NOT FOUND!"<<endl;
- return;
- }
- else if(data == node->data) {
- cout<<"CENTRE WORD>"<<node->data<<endl;
- cout<<"LEFT WORD>";
- if(node->left!=NULL) cout<<node->left->data<<endl;
- else cout<<"!None?"<<endl;
- cout<<"RIGHT WORD>";
- if(node->right!=NULL) cout<<node->right->data<<endl;
- else cout<<"!None?"<<endl;
- //return;
- }
- else if (data < node->data) {
- search(node->left,data);
- }
- else if (data > node->data) {
- search(node->right,data);
- }
- else {
- cout<<"Error>SEARCH ERROR!"<<endl;
- }
- }
- void delnum(treenode* node , char* data, char* key)
- {
- if(node->data.compare(data) == 0) {
- if(strcmp(data,key)==0){
- cout<<"ERROR>NO! DEL 1STCTR WORD"<<endl;
- }
- else{
- //node->data = "/0";
- node->data = "";
- if(node->left!=NULL) node->left = NULL;
- if(node->right!=NULL) node->right = NULL;
- if(node->data.empty())
- cout<<"Msg>DELETE OK!"<<endl;
- else
- cout<<"Error>NUMDEL ERROR!"<<endl;
- }
- }
- else if (node->data.empty()){
- cout<<"Error>NOT FOUND!"<<endl;
- return;
- }
- else if (node->data.compare(data) > 0) {
- delnum(node->left,data,key);
- }
- else if (node->data.compare(data) < 0) {
- delnum(node->right,data,key);
- }
- else {
- cout<<"Error>NUMDEL ERROR!"<<endl;
- }
- }
- int main(void)
- {
- char word[100]; int select;
- treenode* node;
- node=NULL;
- cout<<"1STCTR>"; cin>>word;
- char key[100]; strcpy(key,word);
- cout<<"Process>";
- node = input(node,word);
- //cout<<endl;
- while(1)
- {
- cout<<endl<<"Input(OUT:-1)>"; cin>>word;
- if(strcmp(word,"-1")==0) break;
- cout<<"Process>";
- input(node,word);
- }
- while(2)
- {
- cout<<"Menu> 1)ALLVIEW, 2)SELECT_VIEW, 3)ADDSTR, 4)DELETE, 5)EXIT"<<endl;
- cout<<"Select>"; cin>>select;
- if(cin.fail())
- {
- cout<<"ERROR>Invalid Select"<<endl;
- //cout<<"Error>PLZ ENTER NUM! NOT CHAR!"<<endl;
- cin.clear(); cin.ignore(256,'\n');
- }
- else if(select==5) break;
- else{
- cin.clear(); cin.ignore(256,'\n');
- switch(select)
- {
- case 1:
- cout<<"pre>"; preorder(node); cout<<endl;
- cout<<"ctr>"; ctrorder(node); cout<<endl;
- cout<<"lst>"; lstorder(node); cout<<endl;
- break;
- case 2:
- cout<<"Msg>ENTER U WANT VIEW WORD"<<endl;
- cout<<"Input>"; cin>>word;
- search(node , word); break;
- case 3:
- cout<<"Input>"; cin>>word;
- cout<<"Process>";
- input(node,word); cout<<endl; break;
- case 4:
- cout<<"Msg>ENTER U WANT DEL WORD"<<endl;
- cout<<"Input>"; cin>>word;
- delnum(node , word, key); break;
- default:
- cout<<"ERROR>Invalid Select"<<endl;
- break;
- }
- }
- cout<<endl;
- }
- delete[] node; node=NULL;
- cout<<"byebye"<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement