cout<<"1.插入(I/i)"< cout<<"2.查找(S/s)"< cout<<"3.删除(D/d)"< cout<<"4.先序输出(P/p)"< cout<<"5.中序输出(M/m)"< cout<<"6.后序输出(L/l)"< cout<<"7.退出(E/e)"< char s; cin>>s; while(1) { if(s=='E'||s=='e') break; else if(s=='I'||s=='i') { cout<<"请输入您要插入的值:"< int x; cin>>x; BSTree *p =(BSTree*)malloc(sizeof(BSTree)); p->data = x; p->left = NULL; p->right = NULL; root = insert(root,p); if(flag==false) cout<<"插入成功!"< else { cout<<"此二叉树中已存在此值!"< flag=false;//恢复原值 } } else if(s=='S'||s=='s') { cout<<"请输入您要查找的值:"< int x; cin>>x; BSTree *p=search(root,x); BSTree *pfather=getFather(root,p); cout<<"查找的值为:"< if(pfather!=NULL) cout<<"其父节点的值为:"< else cout<<"它是根节点,没有父节点!"< if(p->left==NULL&&p->right==NULL) cout<<"它是叶子节点,没有子节点"< else { if(p->left != NULL) cout<<"其左儿子节点的值为:"< else cout<<"其左儿子节点为空!"< if(p->right != NULL) cout<<"其右儿子的值为:"< else cout<<"其右儿子节点为空!"< } } else if(s=='D'||s=='d') { cout<<"请输入您要删除的节点的值:"< int value; cin>>value; cout<<"你确定要删除吗?(Yy/Nn)"< char order; cin>>order; while(1) { if(order=='Y'||order=='y') { BSTree * node; node = search(root,value); if(node==NULL) cout<<"该节点不存在!"< else BSTree *nodeDel = deleteNode(root,node); break; } else if(order=='N'||order=='n') { break; } else { cout<<"命令不正确,请重新输入!"< cin>>order; } } } else if(s=='P'||s=='p') { cout<<"其前序输出为:"< preOrder(root); cout< } else if(s=='M'||s=='m') { cout<<"其中序输出为:"< inOrder(root); cout< } else if(s=='L'||s=='l') { cout<<"其后序输出为:"< postOrder(root); cout< } else { cout<<"命令有误,请重新输入!"< } cout<<"请选择您要进行的操作:"< cin>>s; } } system("pause"); return 0; } 摘自:我和我追逐的梦
二叉排序树(二叉查找树)的各种操作C++最新实现(二)
out<<"请选择您要进行的操作:"<