Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #define NULL __null
- using namespace std;
- typedef struct treenode {
- int data;
- struct treenode* left;
- struct treenode* right;
- } treenode;
- treenode* input(treenode* node, int data)
- {
- if(node == NULL){
- treenode* num;
- //cout<<"[node]"<<endl;
- num = new treenode[100];
- num->data = data;
- num->left = NULL;
- num->right = NULL;
- return num;
- }
- else if (data < node->data){
- //cout<<"[left]"<<endl;
- node->left = input(node->left,data);
- }
- else if(data > node->data){
- //cout<<"[right]"<<endl;
- node->right = input(node->right,data);
- }
- else{
- cout<<"ERROR>Already R0egistered"<<endl;
- 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->left) lstorder(node->left);
- if (node->right) lstorder(node->right);
- cout<<node->data<<" ";
- }
- void search(treenode* node , int data)
- {
- if(node == NULL){
- cout<<"ERROR>NONE TREENODE!"<<endl;
- return;
- }
- else if(data == node->data) {
- cout<<"CENTRE NUM>"<<node->data<<endl;
- if(node->left!=NULL)cout<<"LEFT NUM>"<<node->left->data<<endl;
- else cout<<"LEFT NUM>"<<"None"<<endl;
- if(node->right!=NULL)cout<<"RIGHT NUM>"<<node->right->data<<endl;
- else cout<<"RIGHT NUM>"<<"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 , int data)
- {
- if(data == node->data) {
- if(data==5){
- cout<<"ERROR>NO! DEL 1STCTR NUMBER"<<endl;
- }
- else{
- node->data = (int)NULL;
- if(node->left!=NULL) node->left = NULL;
- if(node->right!=NULL) node->right = NULL;
- }
- }
- else if (data < node->data) {
- delnum(node->left,data);
- }
- else if (data > node->data) {
- delnum(node->right,data);
- }
- else {
- cout<<"ERROR>DELETE ERROR!"<<endl;
- }
- }
- int main(void)
- {
- int num, select;
- treenode* node;
- node=NULL;
- cout<<"1STCTR>"; cin>>num;
- node = input(node,num);
- while(1)
- {
- cout<<"Input(OUT:-1)>"; cin>>num;
- if(num==-1)
- break;
- input(node,num);
- }
- while(2)
- {
- cout<<"Menu> 1)ALLVIEW , 2)SELECT_VIEW, 3)DELETE 4)EXIT"<<endl;
- cout<<"Select>"; cin>>select;
- if(select==4) break;
- 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 NUMBER"<<endl;
- cout<<"Input>"; cin>>select;
- search(node , select);
- break;
- case 3:
- cout<<"Msg>ENTER U WANT DEL NUMBER"<<endl;
- cout<<"Input>"; cin>>select;
- delnum(node , select);
- break;
- default:
- cout<<"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